A pure-Go PDF library: no cgo, no bindings, no WebAssembly. It reads, repairs and decrypts PDFs, reads their structure and text, interprets pages into a drawing device, and writes, edits, signs, redacts, tags, converts to PDF/A and PDF/UA, and lays out documents. Files are treated as untrusted: the library is written not to panic or hang on any input, and what it allocates is bounded by limits you can change. It never runs document code and never touches the network.
go get recursiveai.org/go/onepdfGo 1.24 or later.
Packages
| Import path | What it is for |
|---|---|
recursiveai.org/go/onepdf | Open, repair and decrypt; objects, pages, metadata, outlines, forms, annotations; save, merge, split and page edits; content creation. |
.../onepdf/device | The drawing device the interpreter drives, with the geometry, path, colour and glyph types it passes. |
.../onepdf/device/displaylist | Records, replays and serialises the calls a device receives. |
.../onepdf/text | Text extraction: glyphs, words, lines and blocks in reading order, plain text, the shared JSON form. |
.../onepdf/tag | Tagging an untagged file, and reading and editing structure trees. |
.../onepdf/pdfa | PDF/A checks, preparation, conformance saves and conversion of existing files. |
.../onepdf/pdfua | PDF/UA checks, preparation and conformance saves. |
.../onepdf/redact | Removing content for good. |
.../onepdf/sign | Verifying and writing digital signatures, timestamps and two-phase signing. |
.../onepdf/layout | Laying out documents in code: text, boxes, tables, lists, pages, headers and footers, tagged PDF output. |
.../onepdf/layout/hyphen | Hyphenation patterns for the layout engine. |
.../onepdf/fonts | A nested module with substitute fonts (Liberation, Noto); see Fonts. |
Each package has two layers: a short happy path (open, extract, fill, save) built on a low layer you can use directly (objects, the content-stream interpreter and its device, the writer).
Examples
The snippets are the examples in readme_example_test.go, which go test runs. doc is an *onepdf.Document from
onepdf.OpenFile(path) or onepdf.OpenBytes(data); add onepdf.WithEditing() to change it.
Extract text
doc, err := onepdf.OpenFile("letter.pdf") if err != nil { log.Fatal(err) } s, err := text.Plain(doc) // reading order; text.Extract gives words, lines and blocks
Fill a form and save
doc, _ := onepdf.OpenFile("form.pdf", onepdf.WithEditing()) err := doc.Form().Fill(map[string]any{"name": "Ada", "agree": true}) err = doc.SaveFile("filled.pdf")
Sign
cred, err := sign.NewCredential(cert, key) // an *x509.Certificate and its RSA or ECDSA key doc, _ := onepdf.OpenFile("in.pdf", onepdf.WithEditing()) res, err := sign.Sign(doc, out, cred, sign.WithReason("I approve")) report, err := sign.Verify(signedDoc, sign.WithTrustAnchors(root)) // report.Signatures[i].IsValid
Convert to PDF/A
res, err := pdfa.Convert(ctx, dst, src, size) // PDF/A-2b; res.Changes lists what was repairedTag an untagged file
doc, _ := onepdf.OpenFile("in.pdf", onepdf.WithEditing()) pages, _ := text.Extract(doc, text.WithRetainGlyphs(true)) _, err := tag.Tag(doc, pages, func(b tag.BlockInfo) tag.BlockRole { if b.BlockIndex == 0 { return tag.BlockHeading(1) // you choose every role } return tag.BlockParagraph() }, tag.WithLanguage("en"))
Redact
doc, _ := onepdf.OpenFile("in.pdf", onepdf.WithEditing()) res, err := redact.Apply(doc, []redact.Region{{Page: 0, Rect: area}}) // glyphs, paths and pixels under area are gone err = doc.Save(out)
Lay out a document
fonts := layout.NewFonts() fonts.Add(fontBytes, layout.FontSpec{Family: "Mono"}) doc := &layout.Document{ Fonts: fonts, DefaultStyle: layout.TextStyle{Size: 12}, Sections: []*layout.Section{{ Size: layout.A4, Margin: layout.All(layout.Mm(20)), Content: layout.Text("Hello from layout."), }}, } _, err := doc.WritePDF(out, layout.WithTitle("Hello"), layout.WithLanguage("en"))
The PDF is tagged by default. layout.WithSaveOptions takes the save options of the core package, so a PDF/A or PDF/UA file
is layout.WithSaveOptions(pdfa.WithPDFA()) or layout.WithSaveOptions(pdfua.WithPDFUA()).
Fonts
A font the file does not embed is drawn, and a stand-in is embedded on request, from a font provider. There are three sources, asked in this order:
- the document's own provider (
doc.SetFontProvider); - the providers registered for the process (
onepdf.RegisterFontProvider, orfonts.Register()from therecursiveai.org/go/onepdf/fontsmodule, which carries Liberation, Noto Sans Math and Noto Sans Symbols); - the fonts installed on the machine, only if you ask:
onepdf.OpenFile(path, onepdf.WithSystemFonts()).
Installed fonts are off by default, so the same file gives the same output on every machine. WithSystemFonts()
turns them on for one document (doc.SystemFonts() reports it); the library then reads the platform's font directories and
the ones listed in ONEPDF_FONT_DIRS, and nothing else. onepdf.SystemFontProvider(dirs...) gives the same lookup as a
provider you can register or hand to one call.
Performance
Each row times the same workload on the same file, after checking that every library produced the same answer (page counts, extracted text, a reopened output). Where a pure-Go library can do the row, onepdf is compared with the fastest one; where none can, with the C# library of the same family (RecursiveAI.OnePdf 1.26.0). Ratios below 1 mean onepdf is faster. Time is the median of six runs; allocations are per operation.
| Row | onepdf | fastest pure-Go competitor | onepdf / Go | C# 1.26.0 | onepdf / C# | allocs (onepdf / competitor) |
|---|---|---|---|---|---|---|
| Open, 1 page | 2.8 µs | ledongthuc/pdf 6.0 µs | 0.47 | 4.7 µs | 0.60 | 37 / 123 |
| Open, 1,000 pages | 268.5 µs | ledongthuc/pdf 466.2 µs | 0.58 | 1.41 ms | 0.19 | 53 / 11,881 |
| Open, needs repair | 13.4 µs | pdfcpu 102.2 µs | 0.13 | 201 / 853 | ||
| Read Info, XMP and page count, 300 pages | 90.4 µs | pdfcpu 8.57 ms | 0.01 | 397.3 µs | 0.23 | 324 / 78,356 |
| Read every object, 300 pages | 5.27 ms | pdfcpu 11.30 ms | 0.47 | 15,416 / 52,892 | ||
| Read every object, 1,000 pages | 8.12 ms | pdfcpu 20.80 ms | 0.39 | 50,686 / 167,419 | ||
| Interpret, 5,000-path page | 3.03 ms | none | 3.68 ms | 0.82 | 129 | |
| Interpret, dense text page | 596.8 µs | none | 1.11 ms | 0.54 | 750 | |
| Save, 300 pages | 1.31 ms | pdfcpu 13.77 ms | 0.10 | 16,715 / 71,724 | ||
| Incremental save | 22.0 µs | none | 107 | |||
| Encrypt (AES-256 save) | 4.07 ms | pdfcpu 7.30 ms | 0.56 | 19,443 / 69,713 | ||
| Merge 100 documents | 5.74 ms | pdfcpu 12.09 ms | 0.47 | 75,442 / 105,901 | ||
| Split, 300 pages | 16.38 ms | pdfcpu 65.15 ms | 0.25 | 76,729 / 345,948 | ||
| Rotate every page | 1.59 ms | pdfcpu 10.50 ms | 0.15 | 17,685 / 193,652 | ||
| Import 100 pages | 1.18 ms | pdfcpu 9.07 ms | 0.13 | 14,769 / 99,195 | ||
| Fill 100 form fields | 1.24 ms | pdfcpu 5.18 ms | 0.24 | 12,508 / 40,404 | ||
| Flatten 100 form fields | 1.34 ms | none | 13,078 | |||
| Sign | 1.10 ms | digitorus/pdfsign 1.49 ms | 0.74 | 782 / 515 | ||
| Verify a signature | 260.8 µs | digitorus/pdfsign 382.9 µs | 0.68 | 1.08 ms | 0.24 | 374 / 716 |
| Convert to PDF/A | 622.5 µs | none | 1.01 ms | 0.62 | 2,077 |
No pure-Go library appends an incremental update to an edited document, flattens forms, converts to PDF/A, decrypts AES-256 for
extraction, or returns words with bounds, so those rows are timed alone or against C#. Competitors that fail a row's check are
listed as not applicable in the benchmark module's results/table.md rather than timed.
Text extraction
| Row | onepdf | fastest pure-Go competitor | onepdf / Go | C# 1.26.0 | onepdf / C# | allocs (onepdf / competitor) |
|---|---|---|---|---|---|---|
| Plain text, content order, 300 pages | 72.81 ms | dslipak/pdf 132.16 ms | 0.55 | 52,905 / 3,077,915 | ||
| Plain text, reading order, 300 pages | 164.41 ms | none | 178.87 ms | 0.92 | 156,208 | |
| Words with positions, 300 pages | 155.52 ms | none | 233.42 ms | 0.67 | 153,772 | |
| Plain text, reading order, 300 pages, 4 workers | 45.38 ms | none | 53.18 ms | 0.85 | 156,599 | |
| Plain text, 100 CJK pages (Type 0 font, predefined CMap) | 64.49 ms | none | 606,709 | |||
| Plain text, AES-256, 300 pages | 153.52 ms | none | 158,312 |
The pure-Go competitors (ledongthuc/pdf, dslipak/pdf) return text in content order with no reading-order analysis, so the content-order row is the like-for-like comparison with them; on the reading-order rows onepdf does more work than they do and is compared with C#. Neither competitor extracts the CJK file (ledongthuc fails, dslipak returns the wrong characters) or opens the AES-256 file. C# is the floor for words and the parallel row; its words time varied between 186 and 237 ms across runs on the same machine, so that ratio is the least stable one here.
Layout
The same documents laid out by onepdf's layout engine and by go-pdf/fpdf, signintech/gopdf and johnfercher/maroto: an invoice, a 50-page report (tables, headers, footers), a 10,000-row table, a 500-page justified book, and 32 invoices at once. Every engine embeds the same font, Liberation Sans Regular, in regular weight only, and each render parses and embeds it. Only onepdf shapes text (ligatures, kerning, bidirectional and complex scripts) and writes a tagged structure tree; the others place glyphs by advance width and write an untagged file. The onepdf column is the default, tagged output; the last column is the same layout with tagging switched off, shown for information.
| Row | onepdf (tagged) | fastest competitor | onepdf / competitor | allocs (onepdf / competitor) | onepdf, untagged |
|---|---|---|---|---|---|
| Invoice, 1 page | 0.84 ms | gopdf 2.00 ms | 0.42 | 2,908 / 7,783 | 0.72 ms |
| Report, 50 pages | 14.15 ms | fpdf 20.48 ms | 0.69 | 74,239 / 193,766 | 12.11 ms |
| Table, 10,000 rows | 55.41 ms | fpdf 63.76 ms | 0.87 | 360,965 / 654,850 | 39.37 ms |
| Book, 500 pages | 68.76 ms | maroto 158.80 ms | 0.43 | 164,807 / 2,585,708 | 67.56 ms |
| 32 invoices at once | 7.42 ms | gopdf 15.37 ms | 0.48 | 93,492 / 249,351 | 7.16 ms |
Every layout row is ahead of the fastest competitor with tagging on. The table is the closest, at 0.87 of fpdf's time; untagged onepdf takes 0.59 of fpdf's time on the report and 0.62 on the table. Tagging writes the structure tree, marked-content sequences and a parent tree, which is where the difference between the two onepdf columns goes.
Method. AMD Ryzen 9 7950X3D (16 cores, 32 threads), Linux, Go 1.26.8, .NET 10, an otherwise idle machine (each
group started at a load average below 2.0), GOMAXPROCS=8, -benchtime=1s -count=6, reporting medians. The C# library runs on
the same files with 8 processors. The inputs are generated from fixed seeds; the harness, the competitors' versions and how to rerun it
are in bench/README.md.
Licensed under MIT OR Apache-2.0, (c) Recursive AI, LLC.