Skip to main content

Crate docling_pdf

Crate docling_pdf 

Source
Expand description

PDF backend for docling.rs.

A port of docling’s standard PDF pipeline: pdfium extracts the text layer (cells with bounding boxes) and renders page images; a discriminative ONNX stack (layout detection, table structure, OCR) classifies regions; the cells are assembled in reading order into a DoclingDocument.

Current stages: pdfium text-cell extraction + page rendering (pdfium_backend) and the deterministic text/reading-order assembly (assemble). The layout, table-structure and OCR ONNX stages land behind Pipeline next.

Re-exports§

pub use pdfium_backend::PdfDocument;
pub use pdfium_backend::PdfPage;
pub use pdfium_backend::TextCell;

Modules§

assemble
Layout-driven assembly: map detected Regions + text cells to a [DoclingDocument], mirroring docling’s page-assembly + reading-order.
enrich
Optional enrichment models (docling’s do_picture_classification / do_code_enrichment / do_formula_enrichment, issue #76).
ep
Execution-provider selection for the ONNX sessions (#74).
layout
Layout detection via the RT-DETR (docling-layout-heron) model exported to ONNX, run with ort. A port of docling-ibm-models’ LayoutPredictor: resize the page image to 640×640 and rescale to [0,1] (the heron processor has do_normalize=false), run the model, then RT-DETR post_process_object_detection (sigmoid → top-k over query×class → center-to-corners boxes scaled to the page).
ocr_prep
ONNX-free half of the PP-OCRv3 recognition pipeline: everything before and after the session.run call — line segmentation, crop preparation, width-batching, CTC decoding, dictionary handling.
pdfium_backend
pdfium-based text extraction and page rendering.
resample
Pixel-exact reimplementations of the OpenCV resize kernels docling uses for TableFormer preprocessing, so the model sees byte-identical input. Verified against cv2 on docling’s own bitmaps (INTER_AREA max diff 1/255, INTER_LINEAR < 1e-4 in float).
scanned
Scanned-page assembly facade for the browser build (#157 stage 2).
tableformer
TableFormer: table-structure recovery via docling-ibm-models, exported to ONNX by scripts/install/export_tableformer.py. The image encoder + tag-transformer encoder run once to a memory tensor; the decoder is then stepped autoregressively to emit an OTSL structure-token sequence (the same model docling runs). See docs/PDF_CONFORMANCE.md.
textparse
Pure-Rust PDF text extraction (replacing pdfium’s glyph layer).
tf_core
ONNX-free half of the TableFormer pipeline: the 448 encoder-input preprocessing, the autoregressive loop’s structure corrections and bbox bookkeeping, span merging, and the OTSL→grid layout. Everything here is pure Rust (no ort), so the browser build (#157 stage 3) runs the same conformance-critical logic the native pipeline does and delegates only the three ONNX graphs (encoder / decoder / bbox) to ONNX Runtime Web. Keeping one implementation is what keeps the wasm table structure identical to the native CPU path; drift can only come from the runtime kernels.
tf_match
docling’s TableFormer cell matching, ported from docling-ibm-models (tf_cell_matcher.py + matching_post_processor.py, and the response assembly in tf_predictor.py). The predicted table cells and the page’s word cells are matched by intersection-over-word-area, then the post-processor snaps unmatched cells to their column’s median position, de-duplicates columns, assigns each word to its best cell, and finally picks up “orphan” words (no overlap with any cell — dot leaders in a TOC, stray page numbers) through row/column banding. Everything is kept bug-for-bug faithful, including duplicate good cells weighting the column medians and Python’s banker’s rounding on orphan depths.
timing
Lightweight, env-gated per-stage timing for profiling the PDF pipeline.

Structs§

EnrichmentOptions
The opt-in enrichment passes, mirroring docling’s PdfPipelineOptions flags (do_picture_classification, do_code_enrichment, do_formula_enrichment). All off by default.
Pipeline
A reusable PDF pipeline. The primary worker runs its models on every core, so a single-page / small / image / METS input is converted at full intra-op speed with no pool to load. A document with enough pages instead fans out across a pool of narrower workers processed concurrently. Both load lazily and are cached for reuse, so a one-shot conversion only pays for what it uses.

Enums§

OcrLang
OCR recognition language: which PP-OCRv3 model + dictionary pair runs.
PdfError
Errors from the PDF backend. Detailed and surfaced (never silently skipped).

Functions§

convert
Convenience one-shot conversion (loads the pipeline per call). Errors are detailed and surfaced (never silently skipped).
convert_image
Convenience one-shot image conversion (loads the pipeline per call).
convert_image_with_options
Like convert_image, but optionally skips loading/running TableFormer (see Pipeline::no_table_former) and/or layout+OCR+TableFormer entirely (see Pipeline::no_ocr), and/or enables the enrichment passes.
convert_mets_gbs
convert_mets_gbs_with_options
Like convert_mets_gbs, but optionally skips loading/running TableFormer (see crate::Pipeline::no_table_former) and/or layout+OCR+TableFormer entirely (see crate::Pipeline::no_ocr), and/or enables the enrichment passes (see crate::Pipeline::enrichments).
convert_pages
Convert pre-segmented pages (image + already-known text cells, e.g. METS/hOCR scans) through the shared layout + assembly pipeline.
convert_pages_with_options
Like convert_pages, but optionally skips loading/running TableFormer (see Pipeline::no_table_former) and/or layout+OCR+TableFormer entirely (see Pipeline::no_ocr), and/or enables the enrichment passes.
convert_text_layer
Convert a PDF’s embedded text layer only — no pdfium, no ONNX, no threads: the pure-Rust content-stream parser (textparse) feeds the same orphan-region assembly the no_ocr pipeline flag uses, so text-layer PDFs come out identical to --no-ocr (flat, line-grouped paragraphs in reading order; no headings/lists/tables/pictures, and no hyperlink recovery).
convert_text_layer_pages
convert_text_layer restricted to a 1-based inclusive page window (issue #80’s --pages); None converts everything. The window is validated the same way as Pipeline::pages: first <= last, 1-based, and it must select at least one existing page.
convert_with_options
Like convert, but optionally skips loading/running TableFormer (see Pipeline::no_table_former) and/or layout+OCR+TableFormer entirely (see Pipeline::no_ocr), and/or enables the enrichment passes (see Pipeline::enrichments).