Skip to main content

Deterministic PDF/DOCX parser for RAG — Rust core, Python & TS bindings

Project description

pdfmuse

English · 中文

Deterministic PDF/DOCX parser for RAG / LLMs — one Rust core, with Python, Node & WASM bindings that produce byte-identical output.

pdfmuse is a precision pre-layer for AI/RAG: it extracts everything a file actually contains — text with exact coordinates, fonts, vector rules, tables, links — fast, robustly, and identically across every binding. It stops cleanly at the ML boundary: OCR and visual layout inference are left to a pluggable backend, so the core stays deterministic with zero ML dependencies. It is not another probabilistic vision model.

Why pdfmuse

Complete Keeps the finest-grained chars + coordinates; never silently drops content.
Fast Zero-copy streaming Rust core with a custom O(1) object parser + content tokenizer and per-page parallelism.
Robust A broken page/object never sinks the doc — returns structured errors, never panics (fuzz-tested).
Deterministic Same input → same output. No probabilistic models, no time/RNG in the core path.
Consistent Python / Node / WASM call one Rust core; output is byte-identical (CI-enforced).
CJK first-class CID/Type0 fonts + CMap/ToUnicode in the main path; compatibility codepoints NFKC-normalized for clean search.

Performance

Measured on 22 real-world PDFs (resumes, reports, invoices; median of 7 runs, core-to-core, each returning a string):

vs result
pdfplumber (Python, common RAG choice) ~28–39× faster
PyMuPDF (mature C library) ~4× faster (wins every file in the sample)

Text output is preserved (median 100% non-whitespace coverage vs PyMuPDF). See benches/ and examples/visual_check.py. Numbers are hardware-dependent — reproduce locally.

Install

# Rust
cargo add pdfmuse-core
# Python (abi3 wheels)
pip install pdfmuse
# Node
npm install @pdfmuse/node   # native binding
# WASM (browser)
npm install @pdfmuse/core   # or build: wasm-pack build crates/pdfmuse-wasm --target web

Usage

CLI (debug/inspection):

pdfmuse parse report.pdf --format md      # structured Markdown (headings, tables)
pdfmuse parse report.pdf --format json    # full IR (chars, bboxes, blocks, warnings)

Rust:

let data = std::fs::read("report.pdf")?;
let doc = pdfmuse_core::parse(&data, None)?;                 // auto-detect PDF/DOCX
for page in &doc.pages {
    for ch in &page.chars { /* ch.text, ch.bbox {x0,y0,x1,y1}, ch.size */ }
}
let md = pdfmuse_core::to_markdown(&doc);
let chunks = pdfmuse_core::chunk(&doc);                      // RAG chunks + {page, bbox, heading_path}

Python:

import pdfmuse
doc = pdfmuse.parse(open("report.pdf", "rb").read())
text = "".join(c.text for pg in doc.pages for c in pg.chars)

Node:

const { parse_buffer } = require("@pdfmuse/node");
const doc = JSON.parse(parse_buffer(fs.readFileSync("report.pdf")));

WASM (browser — digital PDFs; scanned pages return a NeedsOcr warning to hand off server-side):

import init, { parse } from "@pdfmuse/core";
await init();
const doc = JSON.parse(parse(new Uint8Array(bytes)));

Scope boundary

In the core (deterministic): text + coordinates/font/size/color · vector rules & rects · line/paragraph/column clustering · ruled & whitespace-aligned table reconstruction · full DOCX structure · JSON / Markdown / RAG-chunk output.

Out of the core (pluggable VisionBackend): scanned-page OCR · borderless-table structure recognition · heading/body/caption classification. Text-less (scanned/image) pages are flagged NeedsOcr and left for a backend — see docs/adr/0001-pdf-engine-strategy.md.

Guarding this boundary is what keeps pdfmuse fast, stable, and distinct from vision models.

Layout

crates/
  pdfmuse-core/     pure-Rust core: PDF/DOCX → unified IR (parser, tokenizer, layout, output)
  pdfmuse-python/   PyO3 (abi3) binding
  pdfmuse-node/     napi-rs binding
  pdfmuse-wasm/     wasm-bindgen binding
  pdfmuse-cli/      debug CLI (`pdfmuse`)
tests/{corpus,snapshots}   golden corpus + insta snapshots
tests/parity/              cross-binding byte-identical gate (Python == Node == WASM)
examples/visual_check.py   render original ↔ coordinate reconstruction for QA
fuzz/                      cargo-fuzz targets (never-panic)

Testing gates

  • Snapshot tests (insta + tests/corpus)
  • Cross-binding parity CI — Python/Node/WASM output byte-identical (a red gate blocks merge)
  • Robustness — mutated/garbage input never panics (tests/robustness.rs + fuzz/)
  • CJK correctness suite

Status

Core is feature-complete (milestones M0–M4 + real-world hardening M4.5): PDF + DOCX → unified IR → JSON / Markdown / RAG chunks, three byte-identical bindings, encryption, CJK. Currently in M5 · polish & release. Roadmap and tasks live in Linear (project pdfmuse).

License

Dual-licensed under MIT or Apache-2.0, at your option.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pdfmuse-0.1.3-cp38-abi3-win_amd64.whl (728.5 kB view details)

Uploaded CPython 3.8+Windows x86-64

pdfmuse-0.1.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (889.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

pdfmuse-0.1.3-cp38-abi3-macosx_11_0_arm64.whl (783.5 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file pdfmuse-0.1.3-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: pdfmuse-0.1.3-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 728.5 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pdfmuse-0.1.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4c1be8f2aa6e203ae391df80437aa6b311f26fae787b1e553efb1923f4a12acd
MD5 a5e1e2af5f9bd097c2e7fb9b38e6b070
BLAKE2b-256 2294710f0828dfc8f6fc16519538a186b29d321645d988b08f4f249d6834b576

See more details on using hashes here.

Provenance

The following attestation bundles were made for pdfmuse-0.1.3-cp38-abi3-win_amd64.whl:

Publisher: release.yml on casperkwok/pdfmuse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pdfmuse-0.1.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pdfmuse-0.1.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e45a93b0aebc8cd27461e8bca79f7de06b97e761ad842979c2f3fbbbbfb8ca37
MD5 bd747631553ab2df3ce366eead023eb4
BLAKE2b-256 3147659486d704b207886f79f11f0648fccf3466e26da376042f931ae7ea8a57

See more details on using hashes here.

Provenance

The following attestation bundles were made for pdfmuse-0.1.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on casperkwok/pdfmuse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pdfmuse-0.1.3-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pdfmuse-0.1.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 faa39cf4b5ab5c57c7e473e52d5a1080841f6bfdcca0d844e4c7f8ae156d7836
MD5 acf7bab7ac4752bcecf41c3b44bdfa91
BLAKE2b-256 68f1755f7871d1194a3c97903bfd4898b4d0cebc83d445d29a159902fbea74e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pdfmuse-0.1.3-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on casperkwok/pdfmuse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page