Security

Password-Protected PDF

Create a PDF that requires a password to open.

doc := gpdf.NewDocument(
    gpdf.WithPageSize(gpdf.A4),
    gpdf.WithMargins(document.UniformEdges(document.Mm(20))),
    gpdf.WithEncryption(
        encrypt.WithOwnerPassword("owner-pass"),
        encrypt.WithUserPassword("user-pass"),
    ),
)

page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("This document is password protected", template.FontSize(18))
    })
})

data, _ := doc.Generate()

Restricted Permissions

Allow only printing and copying — no editing allowed.

doc := gpdf.NewDocument(
    gpdf.WithPageSize(gpdf.A4),
    gpdf.WithEncryption(
        encrypt.WithOwnerPassword("owner"),
        encrypt.WithUserPassword("user"),
        encrypt.WithPermissions(encrypt.PermPrint|encrypt.PermCopy|encrypt.PermPrintHighRes),
    ),
)

PDF/A Archive Document

Generate a PDF/A-2b compliant document for long-term archival.

doc := gpdf.NewDocument(
    gpdf.WithPageSize(gpdf.A4),
    gpdf.WithMargins(document.UniformEdges(document.Mm(20))),
    gpdf.WithPDFA(
        pdfa.WithLevel(pdfa.LevelA2b),
        pdfa.WithMetadata(pdfa.MetadataInfo{
            Title:  "Archived Report",
            Author: "ACME Corp",
        }),
    ),
)

page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("This document conforms to PDF/A-2b", template.FontSize(18))
    })
})

data, _ := doc.Generate()

Digitally Signed PDF

Sign a PDF with a digital signature.

// 1. Generate the PDF
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("Signed Document", template.FontSize(24), template.Bold())
    })
})

data, _ := doc.Generate()

// 2. Sign it
signed, err := gpdf.SignDocument(data, signature.Signer{
    Certificate: cert,
    PrivateKey:  key,
},
    signature.WithReason("Document approved"),
    signature.WithLocation("Tokyo"),
)