docuparse
PDF processing for Rust - text extraction, page rendering, and pluggable OCR.
Backed by Pdfium, the C++ PDF engine used by Google Chrome. Supports pluggable OCR backends for scanned documents. The Rust layer adds negligible overhead - >99% of execution time is inside pdfium and ONNX Runtime.
Features
- Native text extraction from PDF text layers
- Page rendering to images at configurable DPI
- Automatic detection of scanned vs native-text pages
- PDF validation - magic bytes, size guards, path checks
- Document metadata - title, author, page count, file size, PDF version
- Pluggable OCR backends via
Arc<dyn OcrBackend> - Zero temp file I/O - everything in memory
Setup
Docuparse links against Pdfium at runtime. Set PDFIUM_LIB_PATH to the directory containing the Pdfium shared library
before running:
Pre-built Pdfium binaries for all platforms are available at
github.com/bblanchon/pdfium-binaries.
In this repo, mise run pdfium fetches the pinned build per platform and
syncs it into pdfium-lib/, and mise run models fetches the checksum-pinned
PP-OCR model pair used by the ONNX backend.
CLI
docuparse-cli (in crates/docuparse-cli) exposes text extraction, page
rendering, and OCR-fallback extraction on the command line. Prebuilt binaries
for macOS (Intel and Apple Silicon), Linux (x86_64 and aarch64), and Windows
(x86_64) are attached to releases
under cli-v* tags, each with a .sha256 checksum.
Install the binary via mise's HTTP backend:
[]
= "0.1.0"
[]
= { = "https://github.com/asincole/docuparse/releases/download/cli-v{{version}}/docuparse-cli-aarch64-apple-darwin.tar.gz", = "sha256:<from SHA256SUMS>" }
= { = "https://github.com/asincole/docuparse/releases/download/cli-v{{version}}/docuparse-cli-x86_64-apple-darwin.tar.gz", = "sha256:<from SHA256SUMS>" }
= { = "https://github.com/asincole/docuparse/releases/download/cli-v{{version}}/docuparse-cli-x86_64-unknown-linux-gnu.tar.gz", = "sha256:<from SHA256SUMS>" }
= { = "https://github.com/asincole/docuparse/releases/download/cli-v{{version}}/docuparse-cli-aarch64-unknown-linux-gnu.tar.gz", = "sha256:<from SHA256SUMS>" }
The crate is also published to crates.io: cargo install docuparse-cli. The
linux x86_64 build requires glibc >= 2.39 (the bundled ONNX Runtime's floor).
Usage
Text extraction
use ;
Page rendering
use ;
OCR
OCR is opt-in via feature flags. Two backends are provided:
The CLI can preserve native text and OCR only pages without a text layer:
Set PDFIUM_LIB_PATH, OCR_DET_MODEL_PATH, OCR_REC_MODEL_PATH, and OCR_DICT_PATH before running. The flag is opt-in; native-text extraction errors still stop the command. The ONNX backend is initialized only when a selected page has no native text.
--endpoint <url> routes the fallback through an OpenAI-compatible vision endpoint instead of the local models (it implies --ocr-fallback):
ONNX (local inference)
Runs inference locally using ONNX Runtime. No network calls, no API keys.
[]
= { = "*", = ["ocr-onnx"] }
Requires three model artifacts set via environment variables:
use ;
use ;
use Arc;
Enable the parallel feature to run inference across rayon's thread pool:
= { = "*", = ["ocr-onnx", "parallel"] }
OpenAI-compatible HTTP (vision API)
Sends rendered page images to any OpenAI-compatible vision endpoint - llama.cpp server, Ollama, vLLM, or the OpenAI API itself.
[]
= { = "*", = ["ocr-openai"] }
use ;
use ;
use Arc;
Custom backends
Implement OcrBackend to plug in any inference engine:
use ;
use DynamicImage;
;
Error handling
Each subsystem has its own error type. Import only what you use:
use ;
// with ocr feature:
use OcrError;
use ;
Performance
Benchmarked on Apple Silicon (M4), release build, synthetic fixture PDFs:
| Operation | Pages | Time | Per page |
|---|---|---|---|
| Text extraction | 10 | ~3.4 ms | ~340 µs |
| Page rendering (150 DPI) | 5 | ~77 ms | ~15 ms |
| OCR - ONNX pipeline | 10 | ~6.6 s | ~660 ms |
| OCR - ONNX parallel (chunk=8) | 10 | ~4.8 s | ~480 ms |
OCR throughput is dominated by model inference time. The parallel feature provides meaningful speedup on documents
with many pages where rayon can distribute inference across cores.
License
MIT