onepdf for Go
A free, open-source PDF library in pure Go, MIT or Apache-2.0. It reads, repairs and decrypts PDFs, extracts their text, edits, signs and redacts them, converts them to PDF/A and PDF/UA, and lays out new ones.
No third-party dependencies: it needs only our onefont module, which uses only the Go standard library.
Fixed in v1.0.1
- Repair
“A damaged file whose objects name their type through a reference no longer causes a panic while the document is repaired; the file opens repaired or returns an error.”
A quick look
The README’s text extraction example as a complete program. letter.pdf is the two-page letter that getting started lays out with the layout package.
package main import ( "fmt" "log" "recursiveai.org/go/onepdf" "recursiveai.org/go/onepdf/text" ) func main() { doc, err := onepdf.OpenFile("letter.pdf") if err != nil { log.Fatal(err) } // Reading order; text.Extract gives words, lines and blocks. s, err := text.Plain(doc) if err != nil { log.Fatal(err) } fmt.Println(s) }
Letter Account 1234 is secret. Terms Payment is due in 30 days.
Install
Go 1.24 or newer. The substitute fonts, for laying out text without a font file of your own and for drawing fonts a PDF does not embed, are a separate module.
Substitute fontsgo get recursiveai.org/go/onepdfgo get recursiveai.org/go/onepdf/fonts@v1.0.1Packages
- onepdf
recursiveai.org/go/onepdf: open, repair and decrypt; objects, pages, metadata, outlines, forms, annotations; save, merge, split and page edits; content creation. - device
The drawing device the interpreter drives, with the geometry, path, color and glyph types it passes.
- device/displaylist
Records, replays and serializes the calls a device receives.
- text
Text extraction: glyphs, words, lines and blocks in reading order, plain text, the shared JSON form.
- tag
Tagging an untagged file, and reading and editing structure trees.
- pdfa
PDF/A checks, preparation, conformance saves and conversion of existing files.
- pdfua
PDF/UA checks, preparation and conformance saves.
- redact
Removing content for good.
- sign
Verifying and writing digital signatures, timestamps and two-phase signing.
- layout
Laying out documents in code: text, boxes, tables, lists, pages, headers and footers, tagged PDF output.
- layout/hyphen
Hyphenation patterns for the layout engine.
- fonts
recursiveai.org/go/onepdf/fonts, a separate module: Liberation Sans, Serif and Mono, Noto Sans Math, Noto Sans Symbols and Symbols 2, under the SIL Open Font License 1.1.
Layout speed
The README’s layout benchmark: an invoice, a 50-page report, a 10,000-row table, a 500-page book and 32 invoices at once, laid out by onepdf and three other Go PDF libraries with the same font. onepdf writes tagged PDF; the others write untagged files. Medians of six runs on one machine with Go 1.26.8.
The layout rows and the methodEvery layout row is ahead of the fastest competitor with tagging on.
What it does
- Version
onepdf v1.0.1; the fonts module v1.0.1. Go 1.24 or newer.
- Reading
Opens a file, a byte slice or an
io.ReaderAt, repairs damaged files, and decrypts RC4 and AES up to AES-256. Text comes out as glyphs, words, lines and blocks in reading order, from the structure tree when the file is tagged and from page geometry otherwise. - Editing
Saves a new file or an incremental update, with AES or RC4 encryption. Merges, splits, reorders, rotates and imports pages; fills and flattens forms; adds text, images, stamps and watermarks; redacts text, paths and images under an area.
- Signatures
Verifies signatures against trust anchors you supply, and signs with PAdES baseline or
adbe.pkcs7.detached, with timestamps and two-phase signing for keys that live elsewhere. - PDF/A, PDF/UA
Checks, prepares and saves PDF/A-2 and -3 and PDF/UA-1 and -2, and converts existing files to PDF/A. Tags untagged files from roles you supply.
- Layout
Builds documents in code with text shaped through onefont-go, tables with repeated headers, lists, images, headers, footers, page numbers, barcodes and SVG. Output is tagged PDF.
- Dependencies
None from third parties. The library needs only our
recursiveai.org/go/onefontmodule, which uses only the Go standard library. No cgo. - Safety
No panic and no hang on hostile input; allocations are bounded by limits you can change. Never runs document scripts, never touches the network.
- License
MIT or Apache-2.0. The fonts in the fonts module are under the SIL Open Font License 1.1.