htmlpdf is a Go package for converting HTML into PDF.
It ships with two rendering backends:
BackendChromeuses headless Chrome or Chromium for high-fidelity HTML/CSS output.BackendNativeuses a pure-Go renderer for document-style HTML without external binaries.
By default, BackendAuto tries Chrome first and falls back to the native renderer when no supported browser is available or Chrome cannot render successfully in the current environment.
This project is pre-v1.0.0. The exported API is usable, but minor option and renderer behavior changes may still happen while the package settles into open-source development.
go get github.com/HeartBeat1608/htmlpdfpackage main
import (
"context"
"log"
"time"
"github.com/HeartBeat1608/htmlpdf"
)
func main() {
html := []byte(`<html><body><h1>Hello</h1><p>PDF output</p></body></html>`)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
pdf, err := htmlpdf.GenerateContext(ctx, html, htmlpdf.Options{
Backend: htmlpdf.BackendAuto,
Title: "Example",
})
if err != nil {
log.Fatal(err)
}
_ = pdf
}If you want something runnable right away, there is a small starter app in examples/basic.
Run it with:
go run ./examples/basicThat generates example.pdf in your current directory.
You can also choose a backend and output path:
go run ./examples/basic -backend=native -out=invoice.pdf -title="My First PDF"- Best output fidelity.
- Requires a Chrome or Chromium binary on the host.
- Does not add
--no-sandboxby default. If your container or CI environment requires it, setOptions.ChromeDisableSandbox = true.
- No external process required.
- Designed for reports, invoices, and other document-style HTML.
- Supports a focused subset of HTML and inline CSS.
Supported block elements:
div,section,article,main,header,footer,ph1toh6ul,ol,liblockquote,pre,hrtable,thead,tbody,tr,td,thfigure
Supported inline elements:
span,strong,b,em,i,code,a,brimgas inline/block fallback content usingalttext
Supported inline CSS properties:
font-sizefont-weightfont-stylefont-familycolortext-alignbackground-color
Unsupported properties are ignored.
go test ./...
go vet ./...Chrome integration tests are opt-in and run only when HTMLPDF_CHROME_BIN points to a local Chrome or Chromium executable.