firecrawl / pdf-inspector
firecrawl / pdf-inspector
pdf-inspector is a fast Rust library for PDF classification and text extraction. It detects whether a PDF is text-based or scanned, extracts text with position awareness, and converts it to clean Markdown — all without OCR. It includes bindings for Python, Node.js, and browser WebAssembly. Built by Firecrawl, it handles text-based PDFs locally in under 200ms, skipping expensive OCR services for the ~54% of PDFs that don’t need them.
pdf-inspector 是一个用于 PDF 分类和文本提取的高性能 Rust 库。它能够检测 PDF 是基于文本的还是扫描件,在具备位置感知能力的前提下提取文本,并将其转换为整洁的 Markdown 格式——全程无需 OCR。该库提供了 Python、Node.js 和浏览器 WebAssembly 的绑定。由 Firecrawl 开发,旨在本地处理基于文本的 PDF,耗时不到 200 毫秒,从而为约 54% 不需要 OCR 的 PDF 节省了昂贵的 OCR 服务成本。
Features / 功能特性
- Smart classification: Detect TextBased, Scanned, ImageBased, or Mixed PDFs in ~10-50ms by sampling content streams. Returns a confidence score (0.0-1.0) and per-page OCR routing. 智能分类: 通过采样内容流,在约 10-50 毫秒内检测 PDF 是基于文本、扫描件、图像还是混合类型。返回置信度分数(0.0-1.0)及每页的 OCR 路由建议。
- Text extraction: Position-aware extraction with font info, X/Y coordinates, and automatic multi-column reading order. 文本提取: 具备位置感知能力的提取功能,包含字体信息、X/Y 坐标以及自动多栏阅读顺序识别。
- Markdown conversion: Headings (H1-H4 via font size ratios), bullet/numbered/letter lists, code blocks (monospace font detection), tables (rectangle-based and heuristic), bold/italic formatting, URL linking, and page breaks. Markdown 转换: 支持标题(通过字体大小比例识别 H1-H4)、项目符号/编号/字母列表、代码块(等宽字体检测)、表格(基于矩形和启发式算法)、粗体/斜体格式、URL 链接以及分页符。
- Table detection: Dual-mode: rectangle-based detection from PDF drawing ops, plus heuristic detection from text alignment. Handles financial tables, footnotes, and continuation tables across pages. 表格检测: 双模式检测:基于 PDF 绘图操作的矩形检测,以及基于文本对齐的启发式检测。可处理财务表格、脚注以及跨页的续表。
- CID font support: ToUnicode CMap decoding for Type0/Identity-H fonts, UTF-16BE, UTF-8, and Latin-1 encodings. CID 字体支持: 支持 Type0/Identity-H 字体的 ToUnicode CMap 解码,以及 UTF-16BE、UTF-8 和 Latin-1 编码。
- Multi-column layout: Automatic detection of newspaper-style columns, sequential reading order, and RTL text support. 多栏布局: 自动检测报纸风格的分栏、顺序阅读逻辑,并支持从右向左(RTL)的文本。
- Encoding issue detection: Automatically flags broken font encodings so callers can fall back to OCR. 编码问题检测: 自动标记损坏的字体编码,以便调用者可以回退到 OCR 处理。
- Single document load: The document is parsed once and shared between detection and extraction, avoiding redundant I/O. 单次文档加载: 文档仅解析一次并在检测与提取之间共享,避免了冗余的 I/O 操作。
- Browser WebAssembly: Run the same Rust parser locally in browsers and Web Workers, with embedded CMaps and no server round trip. 浏览器 WebAssembly: 在浏览器和 Web Workers 中本地运行相同的 Rust 解析器,内置 CMap,无需服务器往返。
- Lightweight: Pure Rust, no ML models, no external services. Single dependency on
lopdffor PDF parsing. 轻量级: 纯 Rust 编写,无机器学习模型,无外部服务依赖。仅依赖lopdf进行 PDF 解析。
Benchmark / 基准测试
Evaluated on the opendataloader-bench corpus (200 PDFs). Only local engines without model-based PDF parsing are shown; OCR was disabled. Scores are 0-1, higher is better.
基于 opendataloader-bench 语料库(200 个 PDF)进行评估。仅展示不依赖模型解析的本地引擎;OCR 已禁用。分数为 0-1,分数越高越好。
| Engine | Overall | Reading Order (NID) | Tables (TEDS) | Headings (MHS) | Speed (200 docs) |
|---|---|---|---|---|---|
| pdf-inspector | 0.875 | 0.915 | 0.814 | 0.788 | 0.470s |
| liteparse | 0.873 | 0.913 | 0.693 | 0.811 | 0.750s |
| opendataloader | 0.831 | 0.902 | 0.489 | 0.739 | 2.569s |
| pymupdf4llm | 0.735 | 0.886 | 0.401 | 0.424 | 17.117s |
| markitdown | 0.589 | 0.844 | 0.273 | 0.000 | 16.165s |
Results were refreshed on July 31, 2026, on an Apple M4 Pro. 结果于 2026 年 7 月 31 日在 Apple M4 Pro 上刷新。
Best fit: Native-text PDFs where speed, reading order, and table structure matter. In this comparison, pdf-inspector delivered the higher overall, reading-order, and table scores, along with the fastest complete run. That makes it a strong local default for reports, research papers, financial documents, invoices, and legal PDFs that need clean, structured Markdown without adding OCR latency or infrastructure.
适用场景: 对速度、阅读顺序和表格结构有要求的原生文本 PDF。在此次对比中,pdf-inspector 在整体评分、阅读顺序和表格评分上均表现最优,且运行速度最快。这使其成为处理报告、研究论文、财务文档、发票和法律 PDF 的强力本地首选方案,无需增加 OCR 延迟或额外基础设施即可获得整洁、结构化的 Markdown。
Quick Start / 快速上手
Python
pip install maturin
maturin develop --release
import pdf_inspector
result = pdf_inspector.process_pdf("document.pdf")
print(result.pdf_type) # "text_based", "scanned", "image_based", "mixed"
print(result.markdown) # Markdown string or None
Node.js
npm install @firecrawl/pdf-inspector
import { readFileSync } from 'fs';
import { processPdf } from '@firecrawl/pdf-inspector';
const result = processPdf(readFileSync('document.pdf'));
console.log(result.pdfType);
console.log(result.markdown);
Browser WebAssembly
npm install @firecrawl/pdf-inspector-wasm
import init, { processPdf } from '@firecrawl/pdf-inspector-wasm';
await init();
const response = await fetch('/document.pdf');
const pdf = new Uint8Array(await response.arrayBuffer());
const result = processPdf(pdf);
Rust
[dependencies]
pdf-inspector = "0.1"
use pdf_inspector::process_pdf;
let result = process_pdf("document.pdf")?;