pdfboss.dev

Rust

PDF parsing and rendering in pure Rust.

pdfboss is a workspace of focused crates sharing one version: a clean-room parser built from ISO 32000, layout analysis to text and Markdown, an anti-aliased rasterizer, in-tree JPEG 2000, JBIG2, CCITT and ICC codecs, async range-fetching I/O and a deterministic PDF writer. Safe Rust, no C dependencies, no bindings to another engine.

Install

Add only what you use.

pdfboss-core alone parses; the others build on it. Optional features stay off by default in the library crates: substitute-fonts on pdfboss-render bundles replacement faces for PDFs that do not embed theirs, predefined-cmaps on pdfboss-core compiles in the CJK CMap set, and http on pdfboss-aio enables remote documents over HTTP range requests.

shell
$ cargo add pdfboss-core pdfboss-output pdfboss-render
# optional: pdfboss-text pdfboss-write pdfboss-markdown pdfboss-aio
$ cargo add pdfboss-aio --features http

Quickstart

Open, extract, rasterize.

render_page returns an RGBA Pixmap; save_png and encode_png turn it into a file or bytes. render_page_reporting returns the same pixels plus every dropped or approximated item. Page indexes are 0-based.

main.rs
use pdfboss_core::Document;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let doc = Document::open("report.pdf")?;
    println!("{} pages", doc.page_count());

    let page = doc.page(0)?;
    let text = pdfboss_output::extract_text(&doc, &page)?;
    println!("{text}");

    let pixmap = pdfboss_render::render_page(&doc, &page, 2.0)?;
    pixmap.save_png("page.png")?;
    Ok(())
}

Design

What the crates promise.

  • Lenient reading: broken cross-reference tables are reconstructed, wrong stream lengths tolerated, garbage operators skipped, and every dropped or approximated item is reported.
  • Lazy page trees: opening a document reads its declared page count instead of parsing every page dictionary up front.
  • Deterministic output from pdfboss-write: the same composition always serializes to the same bytes.
  • Clean-room codecs implemented from their specifications: ITU-T T.800 for JPEG 2000, T.88 for JBIG2, T.4 and T.6 for CCITT, ICC.1:2010 for color.