PDFKit for Go

github.com/malcolmston/pdfkit

PDFKit-style PDF generation in pure Go.

 GitHubports foliojs/pdfkit

A from-scratch, standard-library-only Go take on Node's PDFKit: build a document, add pages, draw text, vector graphics and images, and serialize valid %PDF-1.7 bytes. Everything sits on a real PDF object model — indirect objects, a /Catalog, a /Pages tree, per-page content streams, an xref table, a trailer and %%EOF — so the output is a conforming PDF that opens anywhere. It ships built-in Adobe Font Metrics for all 14 standard Type1 fonts, so text is measured and word-wrapped accurately without embedding font files, plus PNG and JPEG image embedding, vector paths with stroke/fill, standard and custom page sizes, and /Info metadata. No cgo, no third-party modules — just the Go standard library.

Install

shell
$ go get github.com/malcolmston/pdfkit

Quick start

main.go
import "github.com/malcolmston/pdfkit"

doc := pdfkit.New()
page := doc.AddPage(pdfkit.A4)

page.SetFont(pdfkit.Helvetica, 24)
page.DrawText(72, 720, "Hello, PDF!")

page.Rect(72, 600, 200, 80)
page.Stroke()

doc.Save("hello.pdf")

Features

  • Document / Page model — New, AddPage, then Save, Bytes or Write emit a conforming PDF-1.7 catalog, page tree, xref and trailer
  • The 14 standard Type1 fonts as package vars (Helvetica, TimesRoman, Courier, Symbol, ZapfDingbats …) or by name via StandardFont, with built-in AFM widths
  • Text drawing & layout — SetFont, DrawText, DrawLines and greedy word-wrapping DrawParagraph, with Font.Width / TextWidth for accurate measurement
  • Vector paths — MoveTo, LineTo, CurveTo Bézier, Rect, Circle, Ellipse and DrawLine, painted with Stroke, Fill or FillStroke
  • Colors & state — RGB Color from RGB / Gray, applied via SetFillColor, SetStrokeColor and SetLineWidth, with Save / Restore graphics state
  • Image embedding — LoadImage (auto-detect), LoadPNG (FlateDecode + soft mask) and LoadJPEG (DCTDecode) placed with DrawImage as image XObjects
  • Page geometry — standard sizes A4, Letter, Legal, Tabloid …, plus Custom and PageSize.Landscape / Portrait
  • Metadata & read-back — SetTitle/SetAuthor/SetSubject/SetKeywords via /Info, plus a minimal xref Reader; zero dependencies, pure Go stdlib
PDFKit