kilork.github.io

pdf2

A reader, writer and text extractor for PDF 1.x and PDF 2.0.

A PDF file opened by pdf2 into a document model, feeding the info, text and render commands

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

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 everything

As 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.

Where to look

source · crate · docs · wasm demo