Using pdf-inspector in Rust - cargo PDF Parsing
API ReferenceRust
The Rust crate is pdf-inspector — the library's native form. Every other binding (Node/Python/WASM) compiles from it, so in Rust you get ceiling performance. The default build is pure Rust with no ML models; lopdf is the only PDF parsing dependency.
Install
cargo add pdf-inspectorFor unreleased changes:
[dependencies]
pdf-inspector = { git = "https://github.com/firecrawl/pdf-inspector" }Core APIs
use pdf_inspector::{process_pdf, detect_pdf, PdfType};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. All-in-one: detect + extract + Markdown
let result = process_pdf("document.pdf")?;
println!("Type: {:?}", result.pdf_type); // TextBased / Scanned / ImageBased / Mixed
println!("Confidence: {:.0}%", result.confidence * 100.0);
if let Some(markdown) = &result.markdown {
println!("{markdown}");
}
// 2. Classification only (no extraction)
let info = detect_pdf("document.pdf")?;
match info.pdf_type {
PdfType::TextBased => { /* extract locally */ }
_ => { /* info.pages_needing_ocr tells you which pages */ }
}
Ok(())
}Byte input: process_pdf_mem(&bytes) — no filesystem needed.
Fine-grained control with PdfOptions
use pdf_inspector::{process_pdf_with_options, PdfOptions, ProcessMode, DetectionConfig, ScanStrategy};
// Analyze layout without generating markdown
let result = process_pdf_with_options(
"document.pdf",
PdfOptions::new().mode(ProcessMode::Analyze),
)?;
// Custom detection strategy (sample 5 pages)
let result = process_pdf_with_options(
"large.pdf",
PdfOptions::new().detection(DetectionConfig {
strategy: ScanStrategy::Sample(5),
..Default::default()
}),
)?;
// Selected pages only
let result = process_pdf_with_options(
"document.pdf",
PdfOptions::new().pages([1, 3, 5]),
)?;ProcessMode has three levels: DetectOnly, Analyze, and Full.
CLI tools
Two binaries ship with the crate — cargo install pdf-inspector gets you both:
# pdf2md: PDF → Markdown
pdf2md document.pdf # stdout
pdf2md document.pdf --json # structured JSON
pdf2md document.pdf --pages 1-3 # selected pages
pdf2md document.pdf --compact # token-saving output
# detect-pdf: classification
detect-pdf document.pdf --analyze --jsonOptional: OCR capability
The native build can opt into selective OCR via feature flags without pulling an inference runtime:
[dependencies]
pdf-inspector = { version = "1", features = ["vision", "model-cache"] }- The
visionfeature provides thePageRenderer/OcrEnginetraits andOcrOptions(Off/Auto/Forcerouting) - OCR defaults to off — clean processing requests never touch models or the network
model-cache+model-downloadhandle pinned PP-OCRv6 Small artifacts with checksum verification and atomic installation; offline consumers set an explicit model directory withModelDownloadPolicy::Offline
Full contracts in the official docs/rust-api.md and the OCR runtime guide.
Tips
- Don't call
process_pdfwhen you only need the verdict —detect_pdfis an order of magnitude faster - Lower the sampling count for large files (
ScanStrategy::Sample(n)) to speed up classification noticeably - The official docs/rust-api.md is the source of truth