Skip to content

About

Zero-dependency PDF text extractor in pure Python: xref parsing, FlateDecode streams, ToUnicode CMaps, CLI

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pdftext — PDF Text Extractor

Zero-dependency PDF text extraction in pure Python. No Poppler, no PyPDF2, no MuPDF — just the standard library. It reads a PDF the way the spec describes it: xref tables, indirect objects, stream filters, font encodings, and content-stream operators.

pip install .
pdftext report.pdf
pdftext report.pdf -o report.txt --pages 1,3-5
from pdftext import extract_text

print(extract_text("report.pdf"))
print(extract_text("report.pdf", pages=[2]))

What it handles

  • Document structure — classic xref tables, /XRef streams, and a full-file scan fallback for PDFs with a damaged xref section.
  • Stream filters — FlateDecode (with PNG/TIFF predictors), ASCIIHexDecode, ASCII85Decode, RunLengthDecode, LZWDecode (with EarlyChange). Image-only filters (DCT, JPX, JBIG2) raise a clear error instead of silently producing garbage.
  • Fonts — ToUnicode CMaps (bfchar/bfrange, 1- and 2-byte codes), simple-font encodings (WinAnsi, MacRoman, Standard, PDFDoc) with /Differences, resolved through Adobe glyph names — including derived accented letters (Aacute → Á) and uniXXXX/uXXXX forms.
  • Content streams — Tj, TJ (with a word-gap heuristic for large negative kerning), ', ", line breaks from Td/TD/Tm/T*, multi-page documents and page ranges.

Design decisions

  1. Bytes stay bytes until the font is known. Literal strings are kept raw through the lexer; decoding happens per font, so a ToUnicode CMap, a /Differences array, or a WinAnsi fallback each get the same input.
  2. Best effort, never silent. A damaged xref falls back to a scan; a content stream with unparseable binary (inline images) keeps the text collected so far; an unsupported filter raises instead of guessing.
  3. No dependencies. Everything is zlib, re, and unicodedata, which keeps the install trivial and the attack surface small.

Limitations

  • Object streams (/ObjStm) are not supported; such files raise a clear error naming the object.
  • Text is extracted in content-stream order, not visual reading order; multi-column layouts may interleave lines.
  • Encrypted PDFs are not supported.

Running tests

python -m pytest

60 tests, covering the lexer, every filter (including an LZW round-trip through a test-side encoder), CMaps, xref tables/streams, the broken-xref fallback, extraction, and the CLI.

License

MIT

About

Zero-dependency PDF text extractor in pure Python: xref parsing, FlateDecode streams, ToUnicode CMaps, CLI

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages