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/pdfkitQuick 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, thenSave,BytesorWriteemit 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 viaStandardFont, with built-in AFM widths - Text drawing & layout —
SetFont,DrawText,DrawLinesand greedy word-wrappingDrawParagraph, withFont.Width/TextWidthfor accurate measurement - Vector paths —
MoveTo,LineTo,CurveToBézier,Rect,Circle,EllipseandDrawLine, painted withStroke,FillorFillStroke - Colors & state — RGB
ColorfromRGB/Gray, applied viaSetFillColor,SetStrokeColorandSetLineWidth, withSave/Restoregraphics state - Image embedding —
LoadImage(auto-detect),LoadPNG(FlateDecode + soft mask) andLoadJPEG(DCTDecode) placed withDrawImageas image XObjects - Page geometry — standard sizes
A4,Letter,Legal,Tabloid…, plusCustomandPageSize.Landscape/Portrait - Metadata & read-back —
SetTitle/SetAuthor/SetSubject/SetKeywordsvia/Info, plus a minimal xrefReader; zero dependencies, pure Go stdlib