Help us shape the future of Go! Take the 2026 Go Developer Survey

pagecraft

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: GPL-3.0

README

Pagecraft

Pagecraft turns HTML and a practical subset of CSS into paginated PDF documents. It is built for things like invoices, reports, forms, catalogs, and shipping labels: documents where page size, pagination, and readable output matter.

The rendering engine is written in Go and is inspired by WeasyPrint. Pagecraft offers two ways to use the same renderer: a command-line program for any language that can start a process, and a Go package for applications written in Go. The CLI is a small wrapper around the library, so both produce PDFs through the same engine.

Install

Install the command with Go:

go install github.com/robtrivi/pagecraft/cmd/pagecraft@latest

This places the pagecraft executable in your Go binary directory. You can also build it from a checkout:

go build -o pagecraft ./cmd/pagecraft

Use the command

Render an HTML file and choose where to write the PDF:

pagecraft -o report.pdf report.html

With no input file, Pagecraft reads HTML from standard input and writes the PDF to standard output. This makes it easy to connect to other tools:

cat report.html | pagecraft > report.pdf

The output file is optional. Use -o - to write to standard output explicitly. Errors go to standard error and return a non-zero exit code.

Usage: pagecraft [options] [input.html]

Options:
  -o path    write PDF to this file (default: stdout; use - for stdout)

When the input is a file, relative stylesheets, images, and fonts are resolved from that file's directory. An explicit HTML <base> element takes precedence. For HTML from stdin, relative resources are resolved from the process working directory.

Call it from another language

For example, a Python application can invoke Pagecraft and pass HTML through stdin:

import subprocess

html = b"<h1>Monthly report</h1><p>Generated by Pagecraft.</p>"
with open("report.pdf", "wb") as pdf:
    subprocess.run(["pagecraft"], input=html, stdout=pdf, check=True)

The same process interface works from JavaScript, Ruby, PHP, Rust, shell scripts, and other languages. Applications that need a persistent service or a language-specific binding can build those around the executable or the Go package.

Use from Go

Add Pagecraft to a Go project:

go get github.com/robtrivi/pagecraft

The Go API returns the PDF as a byte slice:

package main

import (
 "os"

 "github.com/robtrivi/pagecraft/render"
)

func main() {
 pdf, err := render.RenderHTMLToPDF(`<style>@page { size: A4; margin: 20mm; }</style>
<h1>Monthly report</h1><p>Generated by Pagecraft.</p>`)
 if err != nil {
  panic(err)
 }
 if err := os.WriteFile("report.pdf", pdf, 0o644); err != nil {
  panic(err)
 }
}

Supported layout

Pagecraft supports HTML stylesheets and inline styles, physical page sizes and margins through @page, page footers with page counters, text wrapping, lists, tables, floats, flexbox, grid, and multi-page layout. It can render borders, backgrounds, common typography, PNG and JPEG images, SVG graphics, links, metadata, and TrueType fonts declared with @font-face.

Pagecraft implements a document-focused CSS subset, not a browser. Unsupported CSS may be ignored or render differently from Chromium. The examples/ directory has complete HTML documents for an invoice, an operations report, a product catalog, a shipping label, and a regional expansion plan with landscape appendices.

Example previews

Each preview shows the first page rendered by Pagecraft. Select a preview to open its HTML source.

Preview of the Northstar Studio invoice
Invoice
Preview of the Meridian Services operations report
Operations report
Preview of the North House Goods product catalog
Product catalog
Preview of the Northstar Logistics shipping label
Shipping label
Preview of the Meridian Services regional expansion plan
Regional expansion plan

Development

Run the test suite and static checks:

go test ./...
go vet ./...

License

GNU GPL version 3 or later (GPL-3.0-or-later). See LICENSE.

Directories

Path Synopsis
Package boxes builds the box tree and resolves the CSS subset used by layout.
Package boxes builds the box tree and resolves the CSS subset used by layout.
cmd
pagecraft command
Package css parses the CSS properties supported by the engine.
Package css parses the CSS properties supported by the engine.
Package fonts loads TrueType fonts and provides metrics, shaping, and PDF subsetting.
Package fonts loads TrueType fonts and provides metrics, shaping, and PDF subsetting.
Package images decodes PNG and JPEG images for inclusion in PDF files.
Package images decodes PNG and JPEG images for inclusion in PDF files.
Package layout resolves the supported visual formatting rules and paginates content while preserving computed styles until each box is placed.
Package layout resolves the supported visual formatting rules and paginates content while preserving computed styles until each box is placed.
Package render converts HTML documents to PDF files.
Package render converts HTML documents to PDF files.
Package text provides advance and kerning metrics for the base-14 fonts.
Package text provides advance and kerning metrics for the base-14 fonts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL