Document

Multi-Page Documents

Create documents with multiple pages, each with its own content.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(20))),
)

for i := 1; i <= 5; i++ {
    page := doc.AddPage()
    page.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            c.Text("Multi-Page Document", template.FontSize(20), template.Bold())
            c.Spacer(document.Mm(5))
            c.Line()
            c.Spacer(document.Mm(10))
        })
    })

    // Fill the page with some content
    for j := 1; j <= 10; j++ {
        page.AutoRow(func(r *template.RowBuilder) {
            r.Col(12, func(c *template.ColBuilder) {
                c.Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                    "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
            })
        })
    }
}

Headers & Footers

Define headers and footers that repeat on every page.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(20))),
)

// Header: company name on left, document title on right
doc.Header(func(p *template.PageBuilder) {
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(6, func(c *template.ColBuilder) {
            c.Text("ACME Corporation", template.Bold(), template.FontSize(10))
        })
        r.Col(6, func(c *template.ColBuilder) {
            c.Text("Confidential Report", template.AlignRight(), template.FontSize(10),
                template.TextColor(pdf.Gray(0.5)))
        })
    })
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            c.Line(template.LineColor(pdf.RGBHex(0x1565C0)), template.LineThickness(document.Pt(2)))
            c.Spacer(document.Mm(5))
        })
    })
})

// Footer: centered text with line separator
doc.Footer(func(p *template.PageBuilder) {
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            c.Spacer(document.Mm(5))
            c.Line(template.LineColor(pdf.Gray(0.7)))
            c.Spacer(document.Mm(2))
        })
    })
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(6, func(c *template.ColBuilder) {
            c.Text("Generated by gpdf", template.FontSize(8), template.TextColor(pdf.Gray(0.5)))
        })
        r.Col(6, func(c *template.ColBuilder) {
            c.Text("Confidential", template.FontSize(8),
                template.AlignRight(), template.TextColor(pdf.Gray(0.5)))
        })
    })
})

// Three pages of content
for range 3 {
    page := doc.AddPage()
    page.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            c.Text("Main Content Area", template.FontSize(16), template.Bold())
            c.Spacer(document.Mm(5))
            c.Text("This page demonstrates header and footer repeated on every page. " +
                "The header contains the company name and document title. " +
                "The footer contains generation info and a confidentiality notice.")
        })
    })
}

Metadata

Embed document metadata (title, author, subject, creator) in the PDF.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(20))),
    template.WithMetadata(document.DocumentMetadata{
        Title:   "Annual Report 2026",
        Author:  "gpdf Library",
        Subject: "Example of document metadata",
        Creator: "gpdf example_test.go",
    }),
)

page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Document with Metadata", template.FontSize(20), template.Bold())
        c.Spacer(document.Mm(5))
        c.Text("This PDF has the following metadata set:")
        c.Spacer(document.Mm(3))
        c.Text("Title: Annual Report 2026")
        c.Text("Author: gpdf Library")
        c.Text("Subject: Example of document metadata")
        c.Text("Creator: gpdf example_test.go")
        c.Text("Producer: gpdf (set automatically)")
        c.Spacer(document.Mm(5))
        c.Text("Open the PDF properties in your viewer to verify.", template.Italic())
    })
})

Page Numbering

Automatic page number and total page count in headers and footers.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(20))),
    template.WithMetadata(document.DocumentMetadata{
        Title:  "Page Number Demo",
        Author: "gpdf",
    }),
)

// Header with total pages
doc.Header(func(p *template.PageBuilder) {
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(6, func(c *template.ColBuilder) {
            c.Text("Page Number Demo", template.Bold(), template.FontSize(10))
        })
        r.Col(6, func(c *template.ColBuilder) {
            c.TotalPages(template.AlignRight(), template.FontSize(9),
                template.TextColor(pdf.Gray(0.5)))
        })
    })
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            c.Line(template.LineColor(pdf.RGBHex(0x1565C0)))
            c.Spacer(document.Mm(3))
        })
    })
})

// Footer with page number
doc.Footer(func(p *template.PageBuilder) {
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            c.Spacer(document.Mm(3))
            c.Line(template.LineColor(pdf.Gray(0.7)))
            c.Spacer(document.Mm(2))
        })
    })
    p.AutoRow(func(r *template.RowBuilder) {
        r.Col(6, func(c *template.ColBuilder) {
            c.Text("Generated by gpdf", template.FontSize(8),
                template.TextColor(pdf.Gray(0.5)))
        })
        r.Col(6, func(c *template.ColBuilder) {
            c.PageNumber(template.AlignRight(), template.FontSize(8),
                template.TextColor(pdf.Gray(0.5)))
        })
    })
})

// Create 4 pages of content
for i := range 4 {
    page := doc.AddPage()
    page.AutoRow(func(r *template.RowBuilder) {
        r.Col(12, func(c *template.ColBuilder) {
            titles := []string{
                "Introduction",
                "Background",
                "Analysis",
                "Conclusion",
            }
            c.Text(titles[i], template.FontSize(18), template.Bold())
            c.Spacer(document.Mm(5))
            c.Text("This is the content of the page. The footer displays the " +
                "current page number, and the header shows the total number " +
                "of pages in the document. Both are automatically updated " +
                "after pagination, including on overflow pages.")
        })
    })
}