pdf2
Every mature PDF implementation is a C library behind a wrapper: pdfium, poppler, MuPDF. That is the right answer until the place you need it has no C — a wasm module, a microcontroller, a sandbox where a parser bug is a security incident. This implements the format itself: four dependencies, only for DEFLATE and AES/SHA.
What changes for you
- You can see what a file contains.
pdf2 inforeports version, page count, encryption and metadata;pdf2 checkopens, decodes and extracts every page and tells you what fails instead of handing you a garbled string. - Text that comes out in reading order. Simple fonts through
their encodings, composite fonts through their
/ToUnicodemaps, including the code-space splitting those need. - Nothing panics on hostile input. Every malformed file is an
Err; the robustness tests truncate and mutate fixtures and run the whole read API on whatever still opens. - No decompression bombs. A stream decodes under a 256 MiB budget you can change, and an image is measured from its own declared geometry before a byte of it is allocated.
- Runs where C cannot.
no_stdwithcore+alloc(CI builds athumbv7emtarget), wasm for the browser and WASI, with size budgets in CI. - It writes, too.
Document::new()plussave()produce a real PDF 2.0 file: object streams, a compressed cross-reference stream, XMP metadata — and the crate reads its own output back and compares the whole object graph. - Encrypted files open. AES-256 (
/V 5 /R 6) and the legacy revisions, with user and owner passwords.
Getting it
cargo install pdf2-cli
pdf2 info report.pdf # version, pages, encryption, metadata
pdf2 pages report.pdf # object id, boxes, rotation per page
pdf2 text report.pdf --all # extracted text
pdf2 check report.pdf # decode and extract everythingAs a library:
use pdf2::Document;
let document = Document::open(std::fs::read("report.pdf")?)?;
println!("PDF {}", document.version());
for page in document.pages()? {
println!("{:?}, rotate {}", page.media_box, page.rotate);
}
Drawing is the sibling crate pdf2-render: paths, fills, strokes,
clips, transforms, text and images into RGBA8, proven page by page against
arithmetic in tests/fixtures/calibration.pdf rather than against the
last render. There is a
wasm demo if you would rather poke
at it than read about it.
Where another implementation is simply better — the strongest renderer
(hayro, pdfium) or editing existing files
(lopdf) — the README says so in a comparison table with the column
where this crate loses. That honesty is part of the design: the goal is one model
that reads, inspects, draws and writes, not a claim to beat Chromium's engine.