Quickstart
Installation
go get github.com/gpdf-dev/gpdf
gpdf has zero external dependencies — it only uses the Go standard library.
Hello World
package main
import (
"os"
"github.com/gpdf-dev/gpdf/document"
"github.com/gpdf-dev/gpdf/template"
)
func main() {
doc := template.New(
template.WithPageSize(document.A4),
template.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)
}
┌─ A4 ──────────────────────────────────┐
│ │
│ Hello, World! ← 24pt, Bold │
│ │
│ │
└───────────────────────────────────────┘
Three Ways to Create PDFs
gpdf offers three approaches to PDF generation:
1. Builder API (Recommended)
The most common approach — programmatic control with a fluent builder pattern:
doc := template.New(template.WithPageSize(document.A4))
page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
r.Col(6, func(c *template.ColBuilder) {
c.Text("Left column")
})
r.Col(6, func(c *template.ColBuilder) {
c.Text("Right column")
})
})
data, err := doc.Generate()
2. JSON Schema
Define documents declaratively in JSON — ideal for dynamic content and API-driven generation:
schema := []byte(`{
"page": {"size": "A4", "margins": "20mm"},
"body": [
{"row": {"cols": [
{"span": 12, "text": "Hello from JSON!", "style": {"size": 24, "bold": true}}
]}}
]
}`)
doc, err := template.FromJSON(schema, nil)
data, err := doc.Generate()
3. Reusable Components
Pre-built components for common business documents:
doc := template.Invoice(template.InvoiceData{
Number: "#INV-2026-001",
Date: "March 1, 2026",
DueDate: "March 31, 2026",
From: template.InvoiceParty{Name: "ACME Corp", Address: []string{"123 Main St"}},
To: template.InvoiceParty{Name: "Client Inc", Address: []string{"456 Oak Ave"}},
Items: []template.InvoiceItem{
{Description: "Web Development", Quantity: "40 hrs", UnitPrice: 150.00, Amount: 6000.00},
},
TaxRate: 10,
Currency: "$",
})
data, err := doc.Generate()
Output Methods
// Generate returns PDF as []byte
data, err := doc.Generate()
// Render writes PDF to any io.Writer
err := doc.Render(w)
Next Steps
- Installation — Detailed setup instructions
- Basic Usage — Learn the builder API with examples
- Grid Layout — Master the 12-column layout system
- Elements — All available content elements
- Components — Pre-built document templates