1. Products
  2.   PDF
  3.   Rust
  4.   pdf-inspector

pdf-inspector

 
 

Open Source PDF Inspection & Text Extraction Library

Detect Scanned vs Text-Based PDFs, Extract Text with Position Awareness, and Convert to Markdown via Rust, Python, Node.js & WebAssembly.

What is pdf-inspector?

pdf-inspector is a high-performance, open-source Rust library designed for fast PDF inspection, classification, and text extraction. It intelligently detects whether a PDF is text-based, scanned, image-based, or mixed—enabling smart routing decisions for downstream processing. By default, it extracts text with position awareness, font metrics, and automatic multi-column reading order—without requiring OCR for text-based PDFs. Built by Firecrawl, it processes documents locally in under 200ms, skipping expensive OCR services for the ~54% of PDFs that don’t need them.

The library supports native Rust, Python, Node.js, and browser WebAssembly bindings, with selective OCR only when needed. It includes robust table detection (rectangle-based and heuristic), CID font support, RTL text handling, and encoding issue detection. pdf-inspector powers scalable PDF pipelines by routing documents to optimal processing paths: fast local extraction for text-based files and OCR fallback for scanned ones. Its architecture avoids redundant parsing by loading the document once and sharing state between detection and extraction stages.

Previous Next

Getting Started with pdf-inspector

Install pdf-inspector via pip (Python), npm (Node.js), or cargo (Rust). The library is available on PyPI, npm, and crates.io, with prebuilt binaries for major platforms. For Python, run `pip install pdf-inspector`; for Node.js, `npm install @firecrawl/pdf-inspector`; and for Rust, add `pdf-inspector = "1"` to your `Cargo.toml`. The default builds focus on fast local extraction—OCR is only loaded when explicitly routed. Full documentation, including API references for all bindings, is available in the [pdf-inspector](https://github.com/firecrawl/pdf-inspector) repository.

Install pdf-inspector via pip, npm, or cargo

# Python
pip install pdf-inspector

# Node.js
npm install @firecrawl/pdf-inspector

# Rust
cargo add pdf-inspector

Smart PDF Classification

pdf-inspector uses a lightweight sampling strategy to classify PDFs as TextBased, Scanned, ImageBased, or Mixed in ~10–50ms. It parses only the xref table and page tree—no full document load—and examines content streams for text operators (Tj/TJ) and image operators (Do). By default, it samples 8 evenly distributed pages, but you can configure the scan strategy (EarlyExit, Full, Sample(n), or Pages) for precision vs speed trade-offs. The result includes `pages_needing_ocr`, enabling per-page routing instead of all-or-nothing decisions. This intelligent classification saves cost and latency for the majority of text-based PDFs (reports, invoices, legal docs), routing only scanned or image-heavy files to OCR services.

Classify a PDF and Route to OCR Only When Needed

import pdf_inspector

# Fast classification
result = pdf_inspector.process_pdf("document.pdf")
print(result.pdf_type)  # "text_based", "scanned", "image_based", or "mixed"

# Selective OCR: OCR runtime loads only for routed pages
ocr = pdf_inspector.process_pdf_with_ocr("document.pdf")
print(ocr.pages_routed_to_ocr)  # e.g., [3, 7, 12]

Position-Aware Text Extraction

pdf-inspector extracts text with full positional metadata: X/Y coordinates, font metrics, rotation angles, and paint colors. It handles rotated runs (e.g., margin stamps, chart titles) by preserving true axis-aligned bounding boxes and reporting rotation separately—avoiding collapsed dimensions. The extractor walks PDF content streams, processes XObjects, and resolves CID fonts via ToUnicode CMap decoding for UTF-16BE, UTF-8, and Latin-1 encodings. It automatically detects multi-column layouts, RTL text, and encoding issues, flagging problematic fonts for OCR fallback. This makes it ideal for structured data extraction where layout fidelity matters, such as financial tables, legal documents, and research papers.

Extract Positioned Text Items with Font and Rotation Info

# CLI: Output JSON with positioned text items
pdf2md document.pdf --items-json

# Output includes:
# - axis-aligned box (x0, y0, x1, y1)
# - rotation angle (0–360°)
# - font name, size, weight
# - paint (fill/stroke color, render mode)

Markdown Conversion with Structure Detection

pdf-inspector converts PDFs to clean, structured Markdown with intelligent formatting: headings (H1–H4 via font size tiers), bold/italic (font name patterns), bullet/numbered/letter lists, code blocks (monospace detection), and tables. It joins hyphenated words, filters page numbers, merges drop caps, and collapses dot leaders. Headings use 0.5pt clustering for robust tier assignment, while tables leverage dual-mode detection—rectangle-based from drawing ops and heuristic from text alignment—to handle financial tables, footnotes, and continuation tables. URLs are converted to Markdown links, and page breaks are preserved as `` markers for multi-page documents.

Convert PDF to Structured Markdown

# Convert to Markdown with page markers
pdf2md document.pdf --pages

# Compact output (collapses dot leaders)
pdf2md document.pdf --compact

# JSON output with document metadata
pdf2md document.pdf --json

Table Detection and Layout Analysis

pdf-inspector provides dual-mode table detection: rectangle-based (from PDF drawing operations using union-find) and heuristic (from text alignment and spacing). It handles complex cases like financial tables with token splitting, continuation tables across pages, and nested structures. The layout analyzer detects newspaper-style columns, assigns reading order, and groups lines into paragraphs. It also classifies captions ("Figure", "Table", "Source:"), identifies code blocks via font keywords, and processes sub/superscript using Y-offset analysis. This makes it uniquely suited for documents where table integrity and reading order are critical—such as research papers, invoices, and legal filings.

Run Detection + Layout Analysis via CLI

# Detect PDF type and analyze layout (tables, columns)
detect-pdf document.pdf --analyze --json

# Output includes:
# - PdfType classification
# - Page count, dimensions
# - Table regions (bounding boxes)
# - Column count per page
 English