Expand description
spectre_pdf — Native Rust PDF extraction engine.
Read-only PDF parsing + extraction. Every surface — the persistent
Document handle and the free functions — runs on the in-repo
spectre_parse lazy parser. No C dependencies. Full PDF Standard
Security Handler decryption (RC4 40/128-bit + AES-128/256 R=5/R=6).
Works as a pure Rust library or as a Python extension when built
with the python feature (the Python package is published as
spectre-rs).
§Quickstart (Rust)
use std::fs;
let bytes = fs::read("document.pdf").unwrap();
let text = spectre_pdf::extract_text(&bytes).unwrap();
let pages = spectre_pdf::extract_pages(&bytes).unwrap();
let meta = spectre_pdf::extract_metadata(&bytes).unwrap();
let tables = spectre_pdf::extract_tables(&bytes, None).unwrap();
let score = spectre_pdf::score_text(&text); // 0.0 garbage .. 1.0 clean§Quickstart (Python, requires --features python)
maturin develop --release --features pythonimport spectre_rs
text = spectre_rs.extract_text(open("document.pdf", "rb").read())
pages = spectre_rs.extract_pages(open("document.pdf", "rb").read())Re-exports§
pub use search::SearchHit;pub use search::SearchOptions;
Modules§
- geom
- Shared geometry types used by the positional-extraction surfaces.
PDF user space: points (1/72“), origin bottom-left.
f32throughout — enough precision for any page-sized coordinate (max ~14400 pt). - search
- Text search returning bounding boxes.
Structs§
- Annotation
- One annotation.
subtypeis the raw PDF subtype string (Link,Highlight,FreeText,Stamp,Widget,Ink, …) — unnormalized so downstream filters can match the exact set they care about. - Decoded
Image - One image’s decoded byte buffer.
- Document
- A parsed PDF document. Open once, query many times.
- Document
Info - Form
Field - Re-export of
spectre_parse::FormFieldfor tests/internal callers. One AcroForm field returned byDocument::get_form_fields. Dot-separated partial names per PDF spec §12.7.4.2 — a nested field “address” → “street” → “city” lands asaddress.street.city. - Image
Info - Image XObject inventory entry. We deliberately do not return the decoded image — that would pull in JBIG2/JPEG2000/Flate/DCTDecode (large C deps). The metadata is enough to drive routing (“is this page a scan we should OCR?”).
- Link
- One hyperlink. External
URIactions populateuri; intra-documentGoToactions populatetarget_page(1-indexed). - Page
Info - Positioned
Page - Per-page positioned spans plus a re-assembled plain-text string.
- Text
Block - Cluster of
TextSpans grouped by spatial proximity. - Text
Line - Text
Span - One positioned text fragment emitted by a single text-showing operator
(
Tj/TJ/'/"). - TocEntry
- One entry in the document outline.
levelis 1-indexed depth;pageis the 1-indexed target page, orNonefor unresolvable destinations (usually externalGoToR). - Widget
- One AcroForm field.
nameis the fully qualified, dot-separated path from the field tree root.valueis the current field value as a PDF text string; for choice fields with multi-select, comma-joined. - Word
- Whitespace-delimited token with its own bbox. Bbox is built by proportioning the containing span’s bbox by character count.
Enums§
- Extract
Error - Errors emitted by
spectre_pdfextraction functions. - Form
Field Type - AcroForm field types (PDF spec §12.7.4
/FT). - Image
Container - Output container for
Document::image_bytes.
Constants§
- MAX_
OUTPUT_ BYTES - Maximum byte length of the concatenated output produced by
extract_text. Refuses pathological documents whose extracted text would exhaust available memory. - MAX_
PAGES - Maximum number of pages a single extraction call will process. Crafted PDFs can claim millions of pages; we refuse before doing the work.
- MAX_
TABLES - Maximum number of tables
extract_tableswill return per call. The detector occasionally over-segments on noisy input; this cap keeps the returnedVec<Vec<Vec<String>>>bounded.
Functions§
- extract_
annotations - Extract annotations (read-only).
- extract_
blocks - Extract text blocks (paragraph-sized clusters) with bounding boxes.
- extract_
document_ info - Document-level summary: page count, PDF version, encryption + linearization flags, xref size, trailer ID. Cheap; doesn’t decode any content streams.
- extract_
images - Inventory of embedded images (size, colorspace, filter chain) — equivalent. Image bytes are not decoded; the inventory itself covers routing and observability.
- extract_
links - Extract hyperlinks. Pass
pageto filter to one page (1-indexed). - extract_
metadata - PDF
/Infodictionary plus page count and PDF version. Keys:pages,pdf_version, optionallytitle,author,subject,keywords,creator,producer,creation_date,mod_date. - extract_
page_ info - Per-page geometry: mediabox, cropbox, rotation, width, height, rolled into one cheap walk over the page tree.
- extract_
pages - Extract text per page, in document order. Image-only pages return
empty strings so indices line up. Per-page extraction failures
surface as
ExtractError::PageExtractFailed. Refuses documents claiming more thanMAX_PAGES. - extract_
pages_ lenient - Best-effort variant: pages that fail to extract (commonly CID fonts
missing
/ToUnicode) return empty strings rather than erroring the whole document. Document-level errors still surface. - extract_
tables - Whitespace-column table extraction tuned for borderless tables.
pageis 1-indexed matching the otherextract_*functions;Nonescans every page. - extract_
text - Extract all text from PDF bytes as a single string.
- extract_
text_ lenient - Best-effort variant of
extract_text: failing pages are silently dropped (no error, no placeholder). Output size limit still enforced. - extract_
text_ positioned - Extract text per page with bounding boxes. Returns one
PositionedPageper page in document order; each carries a re-assembled plain-text string and the underlyingTextSpans with PDF user-space rects. Passpage(1-indexed) for one page only. - extract_
toc - Extract the document outline (bookmarks / TOC). Returns an empty Vec for outline-less PDFs.
- extract_
words - Extract words with bounding boxes. Each word carries
(block_no, line_no, word_no)so callers can reconstruct paragraph structure cheaply. - score_
batch - Score a batch of texts in parallel via
rayon. - score_
text - Text-quality score: 0.0 (binary noise) to 1.0 (clean prose). Useful for filtering pages where extraction returned a CID dump.
- search
- Search for a string and return one rect per match.
Case-insensitive and flexible-whitespace by default; see
SearchOptions.