gpdf English | 日本語 | 中文 | 한국어 | Español | Português A pure Go, zero-dependency PDF generation library with a layered architecture and decla…
gpdf English | 日本語 | 中文 | 한국어 | Español | Português A pure Go, zero-dependency PDF generation library with a layered architecture and decla…
English | 日本語 | 中文 | 한국어 | Español | Português
A pure Go, zero-dependency PDF generation library with a layered architecture and declarative builder API.
Comparison with go-pdf/fpdf, signintech/gopdf, and maroto v2. Median of 5 runs, 100 iterations each. Apple M1, Go 1.25.
Execution time (lower is better):
| Benchmark | gpdf | fpdf | gopdf | maroto v2 |
|---|---|---|---|---|
| Single page | 13 µs | 132 µs | 423 µs | 237 µs |
| Table (4x10) | 108 µs | 241 µs | 835 µs | 8.6 ms |
| 100 pages | 683 µs | 11.7 ms | 8.6 ms | 19.8 ms |
| Complex document | 133 µs | 254 µs | 997 µs | 10.4 ms |
Memory usage (lower is better):
| Benchmark | gpdf | fpdf | gopdf | maroto v2 |
|---|---|---|---|---|
| Single page | 16 KB | 1.2 MB | 1.8 MB | 61 KB |
| Table (4x10) | 209 KB | 1.3 MB | 1.9 MB | 1.6 MB |
| 100 pages | 909 KB | 121 MB | 83 MB | 4.0 MB |
| Complex document | 246 KB | 1.3 MB | 2.0 MB | 2.0 MB |
interface{} boxing), so the document tree is built with minimal heap allocations.strings.Builder buffer. No per-cell object wrapping or repeated font lookups; the font is resolved once per document.Run benchmarks yourself:
cd _benchmark && go test -bench=. -benchmem -count=5┌─────────────────────────────────────┐
│ gpdf (entry point) │
├─────────────────────────────────────┤
│ template — Builder API, Grid │ Layer 3
├─────────────────────────────────────┤
│ document — Nodes, Style, Layout │ Layer 2
├─────────────────────────────────────┤
│ pdf — Writer, Fonts, Streams │ Layer 1
└─────────────────────────────────────┘go get github.com/gpdf-dev/gpdfpackage main
import (
"os"
"github.com/gpdf-dev/gpdf"
"github.com/gpdf-dev/gpdf/document"
"github.com/gpdf-dev/gpdf/template"
)
func main() {
doc := gpdf.NewDocument(
gpdf.WithPageSize(gpdf.A4),
gpdf.WithMargins(document.UniformEdges(document.Mm(20))),
)
page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Hello, World!", template.FontSize(24), template.Bold())
})
})
data, _ := doc.Generate()
os.WriteFile("hello.pdf", data, 0644)
}Font size, weight, style, color, background color, and alignment:
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Large bold title", template.FontSize(24), template.Bold())
c.Text("Italic text", template.Italic())
c.Text("Bold + Italic", template.Bold(), template.Italic())
c.Text("Red text", template.TextColor(pdf.Red))
c.Text("Custom color", template.TextColor(pdf.RGBHex(0x336699)))
c.Text("With background", template.BgColor(pdf.Yellow))
c.Text("Centered", template.AlignCenter())
c.Text("Right aligned", template.AlignRight())
})
})Embed TrueType fonts for CJK text rendering. Each language needs its own Noto Sans font:
fontData, _ := os.ReadFile("NotoSansJP-Regular.ttf")
doc := gpdf.NewDocument(
gpdf.WithPageSize(gpdf.A4),
gpdf.WithFont("NotoSansJP", fontData),
gpdf.WithDefaultFont("NotoSansJP", 12),
)
page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("こんにちは世界", template.FontSize(18))
})
})For multi-language documents, register multiple fonts and switch with FontFamily():
…Recommended fonts (all free, OFL license):
| Font | Language |
|---|---|
| Noto Sans JP | Japanese |
| Noto Sans SC | Simplified Chinese |
| Noto Sans KR | Korean |
Build layouts using a Bootstrap-style 12-column grid:
…Use Row() with a specific height, or AutoRow() for content-based height:
// Fixed height: 30mm
page.Row(document.Mm(30), func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("This row is 30mm tall")
})
})
// Auto height (fits content)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Height adjusts to content")
})
})Basic table:
c.Table(
[]string{"Name", "Age", "City"},
[][]string{
{"Alice", "30", "Tokyo"},
{"Bob", "25", "New York"},
{"Charlie", "35", "London"},
},
)Styled table with header colors, column widths, and stripe rows:
c.Table(
[]string{"Product", "Category", "Qty", "Unit Price", "Total"},
[][]string{
{"Laptop Pro 15", "Electronics", "2", "$1,299.00", "$2,598.00"},
{"Wireless Mouse", "Accessories", "10", "$29.99", "$299.90"},
{"USB-C Hub", "Accessories", "5", "$49.99", "$249.95"},
},
template.ColumnWidths(30, 20, 10, 20, 20),
template.TableHeaderStyle(
template.TextColor(pdf.White),
template.BgColor(pdf.RGBHex(0x1A237E)),
),
template.TableStripe(pdf.RGBHex(0xF5F5F5)),
)Table borders — outer frame, per-cell grid, or both:
…Embed JPEG and PNG images with optional fit options:
// Default size (original dimensions)
c.Image(imgData)
// Fit to specific width (maintains aspect ratio)
c.Image(imgData, template.FitWidth(document.Mm(80)))
// Fit to specific height (maintains aspect ratio)
c.Image(imgData, template.FitHeight(document.Mm(30)))Images in grid columns:
page.AutoRow(func(r *template.RowBuilder) {
r.Col(6, func(c *template.ColBuilder) {
c.Text("Image 1")
c.Image(pngData)
})
r.Col(6, func(c *template.ColBuilder) {
c.Text("Image 2")
c.Image(jpegData)
})
})Image with a border and a solid backdrop (handy for transparent PNGs):
c.Image(pngData,
template.FitWidth(document.Mm(60)),
template.WithImageBorder(template.Border(
template.BorderWidth(document.Pt(2)),
template.BorderColor(pdf.RGBHex(0xE53935)),
)),
template.WithImageBackground(pdf.RGBHex(0xFFF8E1)),
)Wrap arbitrary column content in a styled rectangular container with a border, fill, and padding:
c.Box(func(c *template.ColBuilder) {
c.Text("Inside a box")
c.Text("with two lines of body copy")
},
template.WithBoxBorder(template.Border(
template.BorderWidth(document.Pt(1)),
template.BorderColor(pdf.RGBHex(0x1A237E)),
)),
template.WithBoxBackground(pdf.RGBHex(0xE8EAF6)),
template.WithBoxPadding(document.UniformEdges(document.Mm(4))),
)Horizontal rules with color and thickness:
c.Line() // Default (gray, 1pt)
c.Line(template.LineColor(pdf.Red)) // Colored
c.Line(template.LineThickness(document.Pt(3))) // Thick
c.Line(template.LineColor(pdf.Blue), // Combined
template.LineThickness(document.Pt(2)))Vertical spacing:
c.Spacer(document.Mm(5)) // 5mm gap
c.Spacer(document.Mm(15)) // 15mm gapBulleted and numbered lists:
// Bulleted list
c.List([]string{"First item", "Second item", "Third item"})
// Numbered list
c.OrderedList([]string{"Step one", "Step two", "Step three"})Generate QR codes with configurable size and error correction:
// Basic QR code
c.QRCode("https://gpdf.dev")
// Custom size and error correction level
c.QRCode("https://gpdf.dev",
template.QRSize(document.Mm(30)),
template.QRErrorCorrection(qrcode.LevelH))Generate Code 128 barcodes:
// Basic barcode
c.Barcode("INV-2026-0001")
// Custom width
c.Barcode("INV-2026-0001", template.BarcodeWidth(document.Mm(80)))Automatic page numbers and total page count:
doc.Footer(func(p *template.PageBuilder) {
p.AutoRow(func(r *template.RowBuilder) {
r.Col(6, func(c *template.ColBuilder) {
c.Text("Generated by gpdf", template.FontSize(8))
})
r.Col(6, func(c *template.ColBuilder) {
c.PageNumber(template.AlignRight(), template.FontSize(8))
})
})
})
doc.Header(func(p *template.PageBuilder) {
p.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.TotalPages(template.AlignRight(), template.FontSize(9))
})
})
})Underline, strikethrough, letter spacing, and text indent:
c.Text("Underlined text", template.Underline())
c.Text("Strikethrough text", template.Strikethrough())
c.Text("Wide spacing", template.LetterSpacing(3))
c.Text("Indented paragraph...", template.TextIndent(document.Pt(24)))Define headers and footers that repeat on every page:
…for i := 1; i <= 5; i++ {
page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Page content here")
})
})
}Open an existing PDF and overlay content using the same builder API:
…Flatten interactive AcroForm fields into static page content. Non-
No open issues yet, or sync has not completed.