PDF API
Piko renders PDF output from PK templates using a fluent builder. The render pipeline supports metadata, watermarks, PDF/A conformance, accessibility tagging via TaggedPDF(), and custom page labels. It also supports additional CSS stylesheets and font-size and line-height overrides. The pipeline also supports native SVG-to-PDF vector rendering and post-processing transformers for encryption and signing. For the design rationale see about PDF. For task recipes see how to PDF generation. Source of truth: wdk/pdf/facade.go.
Service
| Function | Returns |
|---|---|
pdf.GetDefaultService() (Service, error) | Returns the bootstrap-configured service. |
pdf.NewRenderBuilder(service) (*RenderBuilder, error) | Constructs a render builder bound to the given service. |
pdf.NewRenderBuilderFromDefault() (*RenderBuilder, error) | Shortcut for the default service. |
pdf.NewServiceFromManifest(manifestPath string, opts ...ServiceOption) (*ManifestService, error) | Builds a standalone service for tests and CLI tools that render without the server. Construction does no I/O. |
(*ManifestService).Load(ctx context.Context) error | Reads the compiled manifest through a read-only sandbox rooted at its directory. Rendering requires a successful load. Subsequent calls replace the loaded manifest. |
See render outside the server for the loading and rendering sequence.
Service options
NewServiceFromManifest accepts these ServiceOption values:
| Option | Purpose |
|---|---|
WithFont(family string, weight int, style int, data []byte) | Registers an extra static TTF or OTF font. NotoSans regular is always registered. |
WithVariableFont(family string, weightMin, weightMax int, style int, data []byte) | Registers an OpenType variable font covering a weight range. |
WithExcludeDefaultBold() | Skips the default NotoSans bold, so the painter synthesises bold. |
WithSVGVectorRendering() | Renders SVG data URIs as native PDF vector paths instead of raster images. |
WithLayoutLimits(limits LayoutLimits) | Service-wide layout limits. Unset fields keep the defaults. See Limits. |
WithMaxImagePixels(pixels int) | Service-wide cap on the pixel area of any embedded image. Non-positive values keep the default. |
Builder
The fluent RenderBuilder composes a single render operation. Chain setters, then call Do(ctx) to execute.
| Method group | Methods |
|---|---|
| Template binding | Template(path), Request(r), Props(propsStruct) |
| Output structure | Page(cfg PageConfig), PageLabels(ranges ...PageLabelRange), FontSize(size float64), LineHeight(height float64) |
| Stylesheets | Stylesheet(css string) (callable multiple times) |
| Metadata | Metadata(meta Metadata), ViewerPreferences(prefs ViewerPreferences) |
| Watermarks | Watermark(text string), WatermarkConfig(cfg WatermarkConfig) |
| Compliance | PdfA(level PdfALevel), TaggedPDF() |
| SVG rendering | SVGWriter(writer SVGWriterPort, data SVGDataPort) |
| Post-processing | Transformations(registry *TransformerRegistry, config TransformConfig) |
| Limits | WithLayoutLimits(limits LayoutLimits), WithMaxImagePixels(pixels int), WithEmbeddedDataLimits(limits EmbeddedDataLimits) |
| Execution | Do(ctx) (*Result, error) |
Watermark(text) is the convenience form using default styling (60pt, light grey, 45 degrees). Use WatermarkConfig for full control. PdfA(PdfA2A) enables tagged-PDF accessibility output automatically. The builder has no Fonts setter - the service configures fonts at construction time, not per render.
Types
| Type | Purpose |
|---|---|
Service | Orchestrates the render pipeline. |
ManifestService | Standalone Service returned by NewServiceFromManifest. Adds Load(ctx). |
ServiceOption | Configures a service built by NewServiceFromManifest. |
LayoutLimits | Bounds the work of one layout (alias of layouter_dto.LayoutLimits). See Limits. |
Result | Rendered output. Fields: Content []byte (the PDF bytes), PageCount int (number of pages), and LayoutDump string (debug-only human-readable serialisation of the layout box tree, empty when not requested). |
Config | Full render configuration. |
Metadata | PDF info dictionary (title, author, subject, keywords, creator, producer, creation and modification dates). |
ViewerPreferences | Controls reader display flags (full-screen mode, hide-menubar, page layout). |
WatermarkConfig | Builder-level watermark settings (text, opacity, rotation, placement) used by WatermarkConfig(cfg). |
PdfAConfig | Builder-level PDF/A target level. |
PdfALevel | Enum (PdfA2B, PdfA2U, PdfA2A). |
PageLabelRange | Custom labelling for a range of pages. |
PageLabelStyle | Enum (LabelDecimal, LabelRomanUpper, LabelRomanLower, LabelAlphaUpper, LabelAlphaLower, LabelNone). |
PageConfig | Page dimensions and margins (alias of layouter_dto.PageConfig). |
FontEntry | Font embedding descriptor used at service construction (alias of layouter_dto.FontEntry). |
PainterConfig | Low-level painter tuning. |
SVGWriterPort | Renders SVG markup as native PDF vector commands. |
SVGDataPort | Provides raw SVG markup for a given source. |
TransformerRegistry | Holds available PDF post-processing transformers. |
TransformerPort | Interface implemented by post-processing transformers. |
TransformerType | Enum of transformer categories. |
TransformConfig | Specifies which post-processing transformers to apply. |
Constants
| Group | Values |
|---|---|
| PDF/A level | PdfA2B (basic), PdfA2U (Unicode), PdfA2A (accessible, auto-enables tagged PDF) |
| Page label style | LabelDecimal, LabelRomanUpper, LabelRomanLower, LabelAlphaUpper, LabelAlphaLower, LabelNone |
| Page size | PageA4, PageA3, PageLetter |
| Transformer type | TransformerContent, TransformerCompliance, TransformerDelivery, TransformerSecurity, TransformerCompression |
| Compression algorithm | CompressZstd |
Pagination CSS
| Property | Values |
|---|---|
break-before, break-after (alias page-break-before, page-break-after) | auto, avoid, avoid-page, page, always, left, right, recto, verso. column and avoid-column act as auto. |
break-inside (alias page-break-inside) | auto, avoid, avoid-page. |
orphans, widows | An integer, default 2. |
box-decoration-break | slice. clone acts as slice. |
The default stylesheet gives h1 to h6 break-after: avoid and break-inside: avoid. Replaced elements, inline blocks, table rows and cells, scrolling, transformed, clipped, or masked boxes, floats, absolutely and fixed-positioned boxes, and multi-column containers never break inside. See control page breaks for behaviour and limits.
Limits
Layout limits bound raw HTML expansion, document structure, and pagination. Exceeding a limit returns an error, except for MaxColspan and MaxRowspan, which clamp table-cell spans to their configured maxima.
LayoutLimits defines the following fields and errors.
| Field | Default | Error |
|---|---|---|
MaxRawHTMLBytes | 4 MiB (4,194,304 bytes) of raw HTML expanded in one layout | ErrRawHTMLTooLarge |
MaxNestingDepth | 512 levels of element nesting | ErrNestingTooDeep |
MaxBoxNodes | 200,000 document nodes and layout boxes | ErrTooManyBoxes |
MaxColspan | 1,000 | None. Clamps larger colspan values. |
MaxRowspan | 65,534 | None. Clamps larger rowspan values. |
MaxTableColumns | 10,000 columns in one table | ErrTooManyTableColumns |
MaxGridTracks | 10,000 tracks on one grid axis | ErrTooManyGridTracks |
MaxGridCells | 10,000,000 grid cells examined across one layout | ErrGridTooLarge |
MaxRepeatCount | 10,000 for a CSS repeat() count | ErrRepeatCountTooLarge |
MaxPages | 10,000 pages | ErrTooManyPages |
Every layout sentinel wraps ErrLayoutLimitExceeded, so one errors.Is(err, pdf.ErrLayoutLimitExceeded) check detects any of them. Embedded images have a separate cap on pixel area (width times height), 100,000,000 pixels by default. An image above it fails the render with ErrImageDimensionsTooLarge.
Limits resolve per field in the following order. Non-positive values fall through to the next level.
- The render builder's
WithLayoutLimitsandWithMaxImagePixelssettings. - The service settings from
piko.WithPdfLayoutLimitsandpiko.WithPdfMaxImagePixels, or the equivalentNewServiceFromManifestoptions. - The built-in defaults above.
Errors
| Sentinel | Returned when |
|---|---|
ErrManifestNotLoaded | A ManifestService renders before Load has succeeded. |
ErrLayoutLimitExceeded | Parent of every layout limit error in Limits. |
ErrImageDimensionsTooLarge | An embedded image exceeds the pixel-area cap. |
ErrTooManyEmbeddedFiles, ErrEmbeddedFileTooLarge, ErrStructuredMetadataTooLarge | A payload passed to EmbedData or structured metadata exceeds its EmbeddedDataLimits field. |
ErrEmbeddedFileMetadataTooLong, ErrEmbeddedMIMETypeTooLong | An embedded file's name, description, or MIME type is too long. |
Post-processing transformers
Transformers run after the initial render pass. Register them through a TransformerRegistry, then attach the registry to the builder via Transformations(registry, config). Each transformer has a paired option struct that tunes its behaviour.
| Constructor | Purpose |
|---|---|
NewEncryptTransformer(opts ...EncryptOption) TransformerPort | AES-256 CBC encryption of the output PDF. Pass WithEncryptRandomSource(r) to override the cryptographic randomness source. Production callers can omit options. |
NewPadesSignTransformer() TransformerPort | PAdES digital signature. |
Option structs
Transformer options live in pdfwriter_dto, and the wdk/pdf facade aliases them. The fields below match the source structs exactly.
EncryptionOptions
| Field | Type | Purpose |
|---|---|---|
Algorithm | string | Encryption algorithm: "aes-256", "aes-128", or "rc4-128". |
OwnerPassword | string | Owner (permissions) password. |
UserPassword | string | User (open) password. |
Permissions | uint32 | PDF permission flags bitmask (PDF spec table 22). Use the bitmask form instead of separate booleans. |
PadesSignOptions
| Field | Type | Purpose |
|---|---|---|
PrivateKey | crypto.Signer | Signing key. |
Level | string | PAdES conformance level: "b-b", "b-t", "b-lt", or "b-lta". |
TimestampURL | string | Time Stamping Authority URL (required for "b-t" and above). |
Reason | string | Stated reason for signing. |
Location | string | Stated location of signing. |
ContactInfo | string | Contact information for the signer. |
CertificateChain | [][]byte | Signing certificate chain in DER encoding, ordered from end-entity to root. |
OCSPResponses | [][]byte | Pre-fetched OCSP responses for long-term validation ("b-lt" and above). |
CRLs | [][]byte | Pre-fetched CRLs for long-term validation ("b-lt" and above). |
WatermarkOptions
| Field | Type | Purpose |
|---|---|---|
Text | string | Watermark text. Empty when using image-only watermarks. |
ImageData | []byte | Raw image bytes for an image watermark. Nil for text-only watermarks. |
Pages | []int | Page indices the watermark applies to. Empty means all pages. |
FontSize | float64 | Font size in points for text watermarks. |
ColourR | float64 | Red channel in [0, 1]. |
ColourG | float64 | Green channel in [0, 1]. |
ColourB | float64 | Blue channel in [0, 1]. |
Angle | float64 | Rotation angle in degrees. |
Opacity | float64 | Opacity in [0, 1]. |
PdfAOptions
| Field | Type | Purpose |
|---|---|---|
Level | string | PDF/A conformance level. Allowed values: "1b", "2b", "3b". |
The string Level is the transformer-side configuration. The builder's PdfA(level PdfALevel) setter takes the typed PdfALevel constants (PdfA2B, PdfA2U, PdfA2A) instead.
PdfUAOptions
Empty struct. The PDF/UA transformer takes no configuration. Passing it enables PDF/UA enhancement.
LineariseOptions
Empty struct. The linearisation transformer takes no configuration.
ObjStmOptions
Empty struct. The object-stream compression transformer takes no configuration.
FlattenOptions
| Field | Type | Purpose |
|---|---|---|
FormFields | bool | Flatten interactive form fields into static content. |
Annotations | bool | Flatten annotations into page content. |
Transparency | bool | Flatten transparency groups. |
RedactionOptions
| Field | Type | Purpose |
|---|---|---|
TextPatterns | []string | Regular expression patterns to match and redact. |
Regions | []RedactionRegion | Page-specific rectangular regions to redact. |
StripMetadata | bool | Strip document metadata (author, title, etc.). |
CompressOptions
| Field | Type | Purpose |
|---|---|---|
Algorithm | CompressAlgorithm | Compression algorithm. Defaults to CompressZstd when empty. |
RedactionRegion
| Field | Type | Purpose |
|---|---|---|
Page | int | Zero-based page index. |
X | float64 | Left edge in points. |
Y | float64 | Bottom edge in points. |
Width | float64 | Width in points. |
Height | float64 | Height in points. |
Registry
func NewTransformerRegistry() *TransformerRegistry
*TransformerRegistry is an alias for *pdfwriter_domain.PdfTransformerRegistry. It exposes four methods:
| Method | Purpose |
|---|---|
Register(transformer TransformerPort) error | Adds a transformer. Returns an error when the transformer is nil, has an empty Name(), or another transformer already holds the same name. |
Get(name string) (TransformerPort, error) | Looks up a transformer by name. Returns an error wrapping ErrTransformerNotFound when no transformer holds that name. |
Has(name string) bool | Reports whether the registry holds a transformer with the given name. |
GetNames() []string | Returns all registered transformer names in alphabetical order. |
Transformer interface
TransformerPort is the interface implementations satisfy:
type TransformerPort interface {
Name() string
Type() pdfwriter_dto.TransformerType
Priority() int
Transform(ctx context.Context, pdf []byte, options any) ([]byte, error)
}
| Method | Purpose |
|---|---|
Name() | Unique identifier (for example "watermark", "aes-256", "pades-b-b", "linearise"). |
Type() | Category used for grouping and chain validation. |
Priority() | Execution order. Lower values run first. Recommended ranges: 100-199 for content, 200-299 for compliance, 300-399 for delivery, 400-499 for security. |
Transform(ctx, pdf, options) | Apply the transformation. TransformConfig.TransformerOptions[Name()] provides the options argument. |
TransformConfig is the bundle passed to the builder through Transformations(registry, config). It has two fields:
| Field | Type | Purpose |
|---|---|---|
EnabledTransformers | []string | Names of transformers to apply. The chain sorts them by priority; this list is a declaration of intent, not an ordering directive. |
TransformerOptions | map[string]any | Maps transformer names to their option structs. Each transformer casts the entry to its expected concrete type. |
pdfwriter_dto.DefaultTransformConfig() returns a TransformConfig with no transformers enabled and an empty options map.
Rendering SVG inside a PDF
SVGWriterPort renders SVG content as native PDF vector commands. SVGDataPort provides raw SVG markup. Users who need custom SVG-to-PDF conversion implement one of these ports and plug it into the painter configuration.
Bootstrap
Piko constructs the default service from the generated PDF manifest when the project has a pdfs/ directory. piko.WithPdfLayoutLimits sets layout limits, and piko.WithPdfMaxImagePixels sets the image pixel cap. PdfLayoutLimits aliases LayoutLimits. See Limits for defaults and precedence.
The default service registers the bundled Noto Sans regular and bold, followed by any fonts passed to piko.New:
| Option | Purpose |
|---|---|
piko.WithPdfFont(family string, weight int, italic bool, data []byte) | Registers a static TrueType or OpenType face under a CSS font-family name. |
piko.WithPdfVariableFont(family string, weightMin, weightMax int, italic bool, data []byte) | Registers an OpenType variable font covering a weight range. |
See set the PDF in your own font for how a font-family list resolves.
ssr := piko.New(
piko.WithPdfLayoutLimits(piko.PdfLayoutLimits{
MaxPages: 500,
MaxRawHTMLBytes: 1 << 20,
}),
piko.WithPdfMaxImagePixels(40_000_000),
)
Custom transformers register through the TransformerRegistry:
registry := pdf.NewTransformerRegistry()
if err := registry.Register(pdf.NewEncryptTransformer()); err != nil {
return err
}
if err := registry.Register(pdf.NewPadesSignTransformer()); err != nil {
return err
}
Attach the registry and selection config to the builder via Transformations(registry, config). The config is a TransformConfig describing which transformers to enable and their options.
See also
- About PDF for why PDFs sit on the same template substrate and when to reach for PDF/A or digital signatures.
- How to PDF generation for rendering a PK template, adding metadata, watermarks, and encryption.
- Scenario 027: PDF invoice for a runnable example.