Expand description
Smart PDF detection and text extraction using lopdf
§Quick start
// Full processing (detect + extract + markdown) with defaults
let result = pdf_inspector::process_pdf("document.pdf").unwrap();
println!("type: {:?}, pages: {}", result.pdf_type, result.page_count);
if let Some(md) = &result.markdown {
println!("{md}");
}
// Fast metadata-only detection (no text extraction)
let info = pdf_inspector::detect_pdf("document.pdf").unwrap();
println!("type: {:?}, pages: {}", info.pdf_type, info.page_count);
// Custom options via builder
use pdf_inspector::{PdfOptions, ProcessMode};
let result = pdf_inspector::process_pdf_with_options(
"document.pdf",
PdfOptions::new().mode(ProcessMode::Analyze),
).unwrap();Re-exports§
pub use detector::detect_pdf_type;pub use detector::detect_pdf_type_mem;pub use detector::detect_pdf_type_mem_with_config;pub use detector::detect_pdf_type_with_config;pub use detector::DetectionConfig;pub use detector::PdfType;pub use detector::PdfTypeResult;pub use detector::ScanStrategy;pub use extractor::extract_text;pub use extractor::extract_text_with_positions;pub use extractor::extract_text_with_positions_and_rotations_mem;pub use extractor::extract_text_with_positions_and_rotations_mem_in_frame;pub use extractor::extract_text_with_positions_and_rotations_mem_with_options;pub use extractor::extract_text_with_positions_mem;pub use extractor::extract_text_with_positions_mem_in_frame;pub use extractor::extract_text_with_positions_mem_with_options;pub use extractor::extract_text_with_positions_pages;pub use extractor::extract_text_with_positions_pages_with_password;pub use extractor::PositionFrame;pub use extractor::PositionOptions;pub use markdown::to_markdown;pub use markdown::to_markdown_from_items;pub use markdown::to_markdown_from_items_with_rects;pub use markdown::to_markdown_from_items_with_rects_and_page_count;pub use markdown::MarkdownOptions;pub use markdown::MarkdownProfile;pub use process_mode::ProcessMode;pub use types::BoldSource;pub use types::LayoutComplexity;pub use types::PdfLine;pub use types::PdfRect;pub use types::TextItem;
Modules§
- adobe_
korea1 - Adobe-Korea1 CID-to-Unicode mapping table.
- detector
- Smart PDF type detection without full document load
- extractor
- Text extraction from PDF using lopdf
- glyph_
names - Glyph name to Unicode mapping
- markdown
- Markdown conversion with structure detection.
- process_
mode - structure_
tree - Tagged PDF structure tree parser.
- tables
- Table detection and formatting.
- text_
utils - Character classification and text utility functions.
- tounicode
- ToUnicode CMap parsing for PDF text extraction
- types
- Shared types used across the extraction and markdown pipelines.
- vision
- Optional native vision primitives used by OCR pipelines.
Structs§
- FontC
MapGaps - A font whose ToUnicode CMap (or, for a font without one, the embedded program’s own cmap table) had no entry for some of the codes the document shows through it, and what became of those codes.
- Page
Markdown - Per-page markdown extraction result.
- Page
OcrReasons - OCR reasons for a single 1-indexed page.
- Page
Region Result - Result for a page’s region extractions.
- Pages
Extraction Result - Combined per-page markdown extraction and layout classification result.
- PdfClassification
- Lightweight classification result for routing decisions.
- PdfOptions
- Configuration for
process_pdf_with_optionsand friends. - PdfProcess
Result - High-level PDF processing result.
- Region
Text - Result for a single region’s text extraction.
- Structure
Element - One structure-tree element reference from a tagged PDF, resolved to a page and Marked Content ID.
- Table
Extraction Result - Markdown for one extracted table plus a diagnostic flag describing which path produced it.
- TsrTable
Input - Input for
extract_tables_with_structure_mem: one cropped table region plus the raw structure-recovery output for it. - Vector
Grid Detection - Region-scoped vector grid detection result for TSR-compatible callers.
Enums§
- Page
Rotation - Which way a page’s coordinate frame was turned so that predominantly rotated text reads along +x.
- PdfError
Constants§
- OCR_
REASON_ INVISIBLE_ TEXT_ LAYER - OCR reason: every text-showing operator on the page leaves nothing to see — text render mode 3 (invisible), or mode 7 (clip only) with nothing painted through the clip — while an image covers at least half of the page: a scan carrying a text layer nobody sees. What that layer says is not what the page shows, so the page is read from its raster.
- OCR_
REASON_ NO_ TEXT - OCR reason: the page has no extractable text and no image to OCR — blank, or content the parser cannot reach.
- OCR_
REASON_ SCANNED - OCR reason: the page is a scanned image (a full-page raster / image-only page) with no usable text layer.
- OCR_
REASON_ SUSPECTED_ GARBLED_ TEXT - OCR reason emitted when the extracted text layer appears garbled due to broken font decoding or mojibake.
- OCR_
REASON_ VECTOR_ TEXT - OCR reason: the page’s text is drawn as vector outlines (path operators) rather than real text operators, so it cannot be extracted as characters.
Functions§
- classify_
pdf_ mem - Classify a PDF from a memory buffer without extracting text. Returns the PDF type and which pages need OCR (~10-50ms).
- collect_
text_ in_ region - Collect text items that fall within a region bbox (top-left origin, PDF points) and return them as a single string in reading order.
- collect_
text_ in_ region_ in_ frame collect_text_in_regionwith the page’s coordinate frame given explicitly instead of inferred:rotationis the turnextract_text_with_positions_and_rotations_memreported for the items’ page (PageRotation::Uprightfor pages absent from that map). The region bbox stays in top-left page coordinates and is turned to match.- detect_
pdf - Fast metadata-only detection — no text extraction or markdown generation.
- detect_
pdf_ mem - Fast metadata-only detection from a memory buffer.
- detect_
vector_ grid_ in_ region_ mem - Detect a vector ruled-line / rectangle grid inside one page region.
- extract_
pages_ markdown - Path-based wrapper for
extract_pages_markdown_mem. - extract_
pages_ markdown_ mem - Extract formatted markdown for pages of a PDF, with layout classification metadata.
- extract_
structure_ elements - Path-based wrapper for
extract_structure_elements_mem. - extract_
structure_ elements_ mem - Extract structure-tree element references from a tagged PDF in memory.
- extract_
tables_ in_ regions_ mem - Extract tables within bounding-box regions from a PDF in memory.
- extract_
tables_ in_ regions_ mem_ in_ frame extract_tables_in_regions_memwith the frame of the region rects given explicitly — seeextract_text_in_regions_mem_in_frame.- extract_
tables_ in_ regions_ mem_ with_ options extract_tables_in_regions_mem_in_framewith every option given as aPositionOptions— seeextract_text_in_regions_mem_with_options.- extract_
tables_ with_ structure_ auto_ mem - Auto-fallback variant of
extract_tables_with_structure_mem: runs the TSR-hybrid path, checks the resulting cells for known SLANet detection pathologies (phantom rows, multi-row-in-cell text), and falls back to the heuristicextract_tables_in_regions_memfor any input where the TSR path looks compromised. - extract_
tables_ with_ structure_ cells_ mem - Extract structured cells using externally-supplied structure recovery.
- extract_
tables_ with_ structure_ mem - Extract markdown tables using externally-supplied structure recovery.
- extract_
text_ in_ regions_ mem - Extract text within bounding-box regions from a PDF in memory.
- extract_
text_ in_ regions_ mem_ in_ frame extract_text_in_regions_memwith the frame of the region rects given explicitly.PositionFrame::Sheetreads them as that function does;PositionFrame::Displayreads them on the rendered page — the visible page box turned clockwise by the page’s inheritable/Rotate, top-left origin,ydown — the frame a layout model working on a page image reports boxes in. Pages whose text is predominantly rotated are handled the same way in both frames.- extract_
text_ in_ regions_ mem_ with_ options extract_text_in_regions_mem_in_framewith every option given as aPositionOptions: the frame the region rects are read in, and whether bold is also read from the font’s weight class (bold_from_weight: a weight class ofbold_weight_thresholdor more, 600 by default, is bold, and a run the weight makes bold is then its own item while the region’s lines are assembled). The default options areextract_text_in_regions_mem.- process_
pdf - Process a PDF file with full extraction (detect → extract → markdown).
- process_
pdf_ mem - Process a PDF from a memory buffer with full extraction.
- process_
pdf_ mem_ with_ config Deprecated - Process PDF from memory buffer with custom detection and markdown configuration.
- process_
pdf_ mem_ with_ options - Process a PDF from a memory buffer with custom options.
- process_
pdf_ with_ config Deprecated - Process a PDF file with custom detection and markdown configuration.
- process_
pdf_ with_ config_ pages Deprecated - Process a PDF file with custom configuration and optional page filter.
- process_
pdf_ with_ options - Process a PDF file with custom options.
- widen_
degenerate_ form_ bboxes_ mem - The PDF written back out with the
/BBoxof its Form XObjects repaired — a zero-area box widened, numerals too large for any parser saturated — for callers that render the document with their own renderer. Rust only: the Python, Node.js and WebAssembly bindings do not expose it.