pdftools

module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT

README

pdftools

pdfspec turns PDFs into clean Markdown, from Go, without sending your documents to an AI model.

curl -fsSL https://raw.githubusercontent.com/model-harness/pdftools/main/install.sh | sh
pdfspec md report.pdf > report.md

On Windows, install it from PowerShell with irm https://raw.githubusercontent.com/model-harness/pdftools/main/install.ps1 | iex. Both installers download the build for your machine from the latest release and check it against the release's checksums before installing it, along with the copy of Tesseract it uses to read scans, and add pdfspec to your PATH, so it runs by name in the next terminal you open. Set PDFSPEC_NO_MODIFY_PATH=1 to do that yourself. If you have Go, you can also run go install github.com/model-harness/pdftools/cmd/pdfspec@v1.2.1, which builds pdfspec alone and reads scans with a Tesseract on your PATH.

If you've ever tried this with an ordinary PDF library, you probably got back text with words run together, two columns shuffled into one, and a page number stuck in the middle of a sentence. The library isn't being careless. The file was never built to hold those things.

A PDF is a drawing of a page

PDF was designed in the era of print, to make a page look exactly the same on every screen and every printer. It does that by describing the page as a drawing. A PDF is a list of vector instructions: draw the shape of the letter "T" here, in this font, at this size; draw an "h" just to its right; draw a line under the heading; place this photo in that box.

That's perfect for printing, and awkward for everything else, because the drawing is all there is. Nothing in the file says "this is a word", "this is a paragraph", or "these lines are a table". A few details make that worse:

  • Spaces usually aren't there. The gap between two words is just a bit of extra distance between two letter shapes. Whatever reads the file has to notice the gap and decide it's a space.
  • Reading order isn't there. A two-column page is just shapes on the left and shapes on the right. The file doesn't say to finish the left column first.
  • Tables are just lines and text. A table is some letters lined up, with or without ruled lines drawn around them.
  • Headers, footers and page numbers sit right next to the content, with nothing marking them as different.
  • Some fonts never say which letter a shape is. Sometimes the PDF draws a shape for "ti" or "fi" as one piece, with no record of the letters it stands for.
  • A scanned page holds no text at all. It's a photograph of paper.

Some PDFs carry extra notes, called tags, that describe their own headings and reading order. Most don't, so the structure has to be worked out from where things sit on the page.

Why most tools get it wrong

Most converters read the letters in the order they were drawn and stop there. The letters come out right but the document doesn't. We ran pdfspec against two other common tools using the same 17-page research paper, and found the following:

characters found words found words over 25 characters longest word
pdftools 39,049 6,343 19 (0.30%) 71
pdftotext (Poppler) 39,035 — — —
pdfplumber 0.11.9 39,089 2,392 377 (15.8%) 110

All three found the same characters, to within a fraction of a percent. The difference is in the spaces. pdfplumber found just over a third as many words, and one in six of its "words" ran past 25 characters, because it missed the gaps between them. Go libraries fare worse still: one we tested found a space in only 0.01% of its text, and produced a single "word" 4,069 letters long.

The popular fix is to skip all this and show each page to an AI vision model. That works, but it costs far more and takes far longer than reading text the file already contains. And when a model can't make something out, it tends to guess, and a confident guess looks just like the real thing.

How pdfspec does it

pdfspec trusts what the file says about itself first, and looks elsewhere only where the file has nothing to say.

First it reads what the file holds, and rebuilds the document from it. It works out the words from the gaps between letters, the reading order from the columns, the headings from size and weight, the tables from their lines or their alignment, and it sets aside the page furniture such as running headers and page numbers. If the PDF carries tags, it follows them.

Where the file holds only pictures, such as a scanned page, a screenshot of a table, or a letter whose font never said what it was, pdfspec can use Tesseract, a free text recognition program, to read the image. Where even that isn't enough, it can ask an AI vision model for a second opinion on a doubtful word, or for a description of a chart or a photo. Both are optional and run on your own machine.

What it can't read, it doesn't guess. A letter nobody could read comes out as [err], and an image nobody could describe comes out as [image]. Each run also tells you which pages they were on, so you know where to look.

Getting started

Convert a file:

pdfspec md report.pdf > report.md

If you're not sure what's inside a PDF, ask probe first. It describes the file without converting it, including how pdfspec plans to read it and what it would need help with:

$ pdfspec probe autotagPDFInput.pdf
  pages       4
  ...
  structure   untagged
  ...
  path        layout
  engines     tesseract 5.5.3 eng; gemma-4-e4b via inferd
  needs       images: p2 2, p3 4, p4 2; glyphs with no value: p1 30, p2 24, p3 1, p4 3

Here needs says that pages 2 to 4 have pictures worth reading, and that every page has some letters whose font never said what they were.

There are a few other commands for related jobs: md -split writes one file per page, okf writes one file per section of a long specification, images pulls out the embedded pictures, render turns pages into PNG or JPEG files, and ocr gives you more control over reading scanned pages. Run pdfspec <command> -h for the options.

Reading scans and pictures

You don't need to install anything else to read scans. Each release of pdfspec ships with its own copy of Tesseract, the open-source text recognizer, built from this repository with the English model. The installers put it beside pdfspec, so a scanned page, a screenshot of a table, or a letter whose font never said what it was gets read without anything else to install.

Describing pictures and charts needs one more piece. For that, pdfspec asks a vision model you run on your own machine, through inferd. The model also gives a second opinion on any word Tesseract isn't sure of. To install inferd along with pdfspec, set PDFSPEC_WITH_INFERD=1:

curl -fsSL https://raw.githubusercontent.com/model-harness/pdftools/main/install.sh | PDFSPEC_WITH_INFERD=1 sh

On Windows, run $env:PDFSPEC_WITH_INFERD = '1' before the PowerShell command above. inferd runs as your own background service and works on an ordinary CPU, no graphics card needed. The first time it starts, it downloads its model, Gemma 4 (gemma-4-e4b), which is about 6 GB. You can also install it yourself by following inferd.io. On Linux, inferd needs a recent system, such as Ubuntu 24.04 or Debian 13; on an older one, the installer says so and installs pdfspec alone.

Without inferd, pdfspec reads everything else and writes [image] where a picture would have been described. pdfspec probe tells you which engines it found.

If you'd rather use your own Tesseract, pdfspec md -h shows how to point at it. You can also build the one pdfspec ships yourself, with native/tesseract/build.sh --portable or native/tesseract/build.ps1 -Portable.

How we know it's good

We keep a set of reference PDFs, each one built to test a single thing, such as two columns, a table, a footnote, or a scanned page. For each one we wrote down by hand exactly what the Markdown should be. Every change to pdfspec is scored against that set, and the build fails if accuracy drops below the targets we set for this release: at most 3 wrong characters in a hundred, and at least 97% of words found correctly, among others. Every one of those targets passes today, on Linux and on Windows.

go test ./...      # runs the whole suite, with nothing else installed

Using it from your own code

The command is a thin layer over a Go package, github.com/model-harness/pdftools/pdfspec, which does the whole conversion. You get the same Markdown pdfspec md writes:

err := pdfspec.Convert(ctx, os.Stdout, "report.pdf", pdfspec.DefaultOptions)

pdfspec.DefaultOptions reads scans and pictures with Tesseract and a vision model when it finds them, just as the command does. The package reads no environment variables, so your program decides: set Tesseract to the path of an executable, or to "off", and Inferd to an address, or to "off". The warnings the command prints go to Options.Log, which is discarded unless you give it a writer.

For more control, Open reads the file's own text, Read runs the engines, and Markdown writes the result. In between, Document.Doc holds everything that was found: pages, blocks, tables, images, and what was read off the pixels.

Using it with coding agents

If you work with Claude Code, Codex or another coding agent, you can teach it to reach for pdfspec whenever it needs what's inside a PDF, and to treat [err] and [image] as gaps rather than guess at them. The skill is in skills/, along with where each agent expects to find it.

Licence and further reading

pdfspec is MIT-licensed; see LICENSE. Some of the test PDFs come from other projects under their own licences, and docs/test.docs.md explains where each one came from.

If you want to know how it works inside, docs/DESIGN.md explains the architecture, docs/adr/ records each significant decision with the measurements behind it, and CHANGELOG.md lists what changed in each release.

Directories

Path Synopsis
Package bench scores a converter's Markdown against a gold Markdown file, so a change to extraction, layout, or the sink can be judged by a number instead of a diff.
Package bench scores a converter's Markdown against a gold Markdown file, so a change to extraction, layout, or the sink can be judged by a number instead of a diff.
Package bits reads a bit stream MSB-first or LSB-first.
Package bits reads a bit stream MSB-first or LSB-first.
cmd
pdfspec command
Command pdfspec is the CLI for the pdftools toolkit.
Command pdfspec is the CLI for the pdftools toolkit.
Package content reads PDF content streams: the token stream, and the graphics and text state machine that turns operators into positioned glyphs.
Package content reads PDF content streams: the token stream, and the graphics and text state machine that turns operators into positioned glyphs.
Package doc is the domain model every other package converges on: what a document looks like after it has been read and before it has been written out.
Package doc is the domain model every other package converges on: what a document looks like after it has been read and before it has been written out.
Package extract turns a page's content stream into positioned text.
Package extract turns a page's content stream into positioned text.
Package filter decodes PDF stream filters (ISO 32000-2 §7.4).
Package filter decodes PDF stream filters (ISO 32000-2 §7.4).
Package font reads font dictionaries and answers the two questions text extraction asks of a font: what does this character code mean, and how far does it advance the text position (ISO 32000-2 §9.5 through §9.7).
Package font reads font dictionaries and answers the two questions text extraction asks of a font: what does this character code mean, and how far does it advance the text position (ISO 32000-2 §9.5 through §9.7).
cmap
Package cmap reads PDF CMaps: the code-to-CID mappings of composite fonts and the code-to-Unicode mappings of /ToUnicode streams (ISO 32000-2 §9.7.5, §9.10.3).
Package cmap reads PDF CMaps: the code-to-CID mappings of composite fonts and the code-to-Unicode mappings of /ToUnicode streams (ISO 32000-2 §9.7.5, §9.10.3).
encoding
Package encoding maps simple-font character codes to glyph names and Unicode.
Package encoding maps simple-font character codes to glyph names and Unicode.
Package function evaluates the PDF functions (ISO 32000-2 §7.10) that an axial shading reads, exactly as pdfium does: cpdf_function.cpp for parsing and dispatch, cpdf_expintfunc.cpp for type 2 (exponential interpolation), cpdf_stitchfunc.cpp for type 3 (stitching).
Package function evaluates the PDF functions (ISO 32000-2 §7.10) that an axial shading reads, exactly as pdfium does: cpdf_function.cpp for parsing and dispatch, cpdf_expintfunc.cpp for type 2 (exponential interpolation), cpdf_stitchfunc.cpp for type 3 (stitching).
Package geom holds PDF user-space geometry: rectangles, transformation matrices, and the tolerance policy shared by every consumer.
Package geom holds PDF user-space geometry: rectangles, transformation matrices, and the tolerance policy shared by every consumer.
Package icc converts through the one family of ICC profile that a PDF actually carries in practice: a matrix/TRC profile, gray or RGB, with an XYZ connection space.
Package icc converts through the one family of ICC profile that a PDF actually carries in practice: a matrix/TRC profile, gray or RGB, with an XYZ connection space.
Package image reads image XObjects out of a PDF (ISO 32000-2 §8.9).
Package image reads image XObjects out of a PDF (ISO 32000-2 §8.9).
internal
cffbuild
Package cffbuild assembles a bare CFF font program from nothing, for tests.
Package cffbuild assembles a bare CFF font program from nothing, for tests.
iccbuild
Package iccbuild assembles minimal ICC profiles, for tests.
Package iccbuild assembles minimal ICC profiles, for tests.
inferd
Package inferd is a client for the inferd inference daemon's IPC wire protocol (v2, inferd ADR 0021), speaking the wire directly as the protocol document defines it.
Package inferd is a client for the inferd inference daemon's IPC wire protocol (v2, inferd ADR 0021), speaking the wire directly as the protocol document defines it.
liberation
Package liberation supplies substitute font programs for documents that embed none.
Package liberation supplies substitute font programs for documents that embed none.
oneline
Package oneline folds a message onto one line, for a warning or a report row that quotes an error.
Package oneline folds a message onto one line, for a warning or a report row that quotes an error.
pdfbuild
Package pdfbuild assembles a minimal PDF file from a list of objects, for tests.
Package pdfbuild assembles a minimal PDF file from a list of objects, for tests.
t1build
Package t1build assembles a Type 1 font program in the PFA form from nothing, for tests.
Package t1build assembles a Type 1 font program in the PFA form from nothing, for tests.
ttfbuild
Package ttfbuild assembles a TrueType font from nothing, for tests.
Package ttfbuild assembles a TrueType font from nothing, for tests.
Package layout infers roles from geometry and typography, for the documents that declare none.
Package layout infers roles from geometry and typography, for the documents that declare none.
Package objects defines the PDF object model this repository owns, plus the Store interface for reading it out of a file.
Package objects defines the PDF object model this repository owns, plus the Store interface for reading it out of a file.
pdfcpu
Package pdfcpu adapts github.com/pdfcpu/pdfcpu (Apache-2.0, pure Go) to the objects.Store interface.
Package pdfcpu adapts github.com/pdfcpu/pdfcpu (Apache-2.0, pure Go) to the objects.Store interface.
ocr
Package ocr recovers text from pages whose content stream does not carry it.
Package ocr recovers text from pages whose content stream does not carry it.
gemma
Package gemma asks inferd's vision model what only a model can answer: a second reading of a word Tesseract is unsure of, a description of a picture, and the values of a chart (ADR 0044).
Package gemma asks inferd's vision model what only a model can answer: a second reading of a word Tesseract is unsure of, a description of a picture, and the values of a chart (ADR 0044).
tesseract
Package tesseract reads page images with the tesseract CLI, run as a subprocess when it is installed (ADR 0043).
Package tesseract reads page images with the tesseract CLI, run as a subprocess when it is installed (ADR 0043).
Package pdfspec converts a PDF to Markdown.
Package pdfspec converts a PDF to Markdown.
Package render turns a page into pixels.
Package render turns a page into pixels.
native
Package native rasterizes a page in Go, with no borrowed engine.
Package native rasterizes a page in Go, with no borrowed engine.
pdfium
Package pdfium adapts github.com/klippa-app/go-pdfium (MIT) over pdfium compiled to WebAssembly to the render.Rasterizer interface.
Package pdfium adapts github.com/klippa-app/go-pdfium (MIT) over pdfium compiled to WebAssembly to the render.Rasterizer interface.
Package sectionize reconstructs a document's clause hierarchy.
Package sectionize reconstructs a document's clause hierarchy.
sink
markdown
Package markdown writes a doc.Document as Markdown.
Package markdown writes a doc.Document as Markdown.
okf
Package okf writes a doc.Outline as an Open Knowledge Format bundle.
Package okf writes a doc.Outline as an Open Knowledge Format bundle.
Package tag reads a PDF's logical structure tree: the /StructTreeRoot graph described in ISO 32000-2 §14.7.
Package tag reads a PDF's logical structure tree: the /StructTreeRoot graph described in ISO 32000-2 §14.7.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL