docling_pdf/lib.rs
1//! PDF backend for docling.rs.
2//!
3//! A port of docling's standard PDF pipeline: pdfium extracts the text layer
4//! (cells with bounding boxes) and renders page images; a discriminative ONNX
5//! stack (layout detection, table structure, OCR) classifies regions; the cells
6//! are assembled in reading order into a [`DoclingDocument`].
7//!
8//! Current stages: pdfium text-cell extraction + page rendering ([`pdfium_backend`])
9//! and the deterministic text/reading-order assembly ([`assemble`]). The layout,
10//! table-structure and OCR ONNX stages land behind [`Pipeline`] next.
11
12// Without `ml` only the text-layer path runs; the shared assembly/label
13// helpers it doesn't exercise stay compiled for API stability (the full
14// build still flags genuinely dead code).
15#![cfg_attr(not(feature = "ml"), allow(dead_code))]
16
17// Reading-order assembly. Public under `ocr-prep` so the browser pipeline can
18// reuse the geometric table reconstruction and its reliability gate (#157).
19#[cfg(feature = "ocr-prep")]
20pub mod assemble;
21#[cfg(not(feature = "ocr-prep"))]
22mod assemble;
23mod dp_lines;
24#[cfg(feature = "ml")]
25pub mod enrich;
26// Public so sibling crates (e.g. docling-rag's ONNX embedder) can route their
27// own `ort` sessions through the same `DOCLING_RS_EP` selection.
28#[cfg(feature = "ml")]
29pub mod ep;
30pub mod layout;
31#[cfg(feature = "ml")]
32mod mets;
33#[cfg(feature = "ml")]
34mod ocr;
35#[cfg(feature = "ocr-prep")]
36pub mod ocr_prep;
37pub mod pdfium_backend;
38#[cfg(feature = "ml")]
39pub mod quality;
40mod reading_order;
41// Pure-Rust region resampling (page→1024px box-average, crop→448 bilinear) —
42// available to the browser TableFormer path (#157 stage 3), not just `ml`.
43#[cfg(feature = "ocr-prep")]
44pub mod resample;
45#[cfg(feature = "ocr-prep")]
46pub mod scanned;
47// Built-in standard-14 font metrics for the pure-Rust text parser (#187) —
48// no feature gate: the wasm/pdf-text path needs them like the native one.
49mod std14;
50#[cfg(feature = "ml")]
51pub mod tableformer;
52pub mod textparse;
53#[cfg(feature = "ocr-prep")]
54pub mod tf_core;
55// docling's TableFormer cell matcher — pure Rust, shared with the browser
56// TableFormer path (#157 stage 3).
57#[cfg(feature = "ocr-prep")]
58pub mod tf_match;
59pub mod timing;
60
61#[cfg(feature = "ml")]
62use std::collections::BTreeMap;
63use std::fmt;
64#[cfg(feature = "ml")]
65use std::sync::mpsc::{sync_channel, Receiver};
66#[cfg(feature = "ml")]
67use std::sync::{Arc, Mutex};
68
69use docling_core::DoclingDocument;
70#[cfg(feature = "ml")]
71use docling_core::Node;
72
73#[cfg(feature = "ml")]
74pub use mets::{convert_mets_gbs, convert_mets_gbs_with_options};
75#[cfg(feature = "ml")]
76pub use ocr::OcrLang;
77#[cfg(feature = "ml")]
78pub use pdfium_backend::PdfDocument;
79pub use pdfium_backend::{PdfPage, TextCell};
80
81/// Errors from the PDF backend. Detailed and surfaced (never silently skipped).
82#[derive(Debug)]
83pub enum PdfError {
84 /// pdfium failed to bind, open, or read the document.
85 Pdfium(String),
86 /// The layout ONNX model failed to load or run.
87 Layout(String),
88 /// The OCR ONNX model failed to load or run.
89 Ocr(String),
90}
91
92impl fmt::Display for PdfError {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 match self {
95 PdfError::Pdfium(m) => write!(f, "pdf: pdfium error: {m}"),
96 PdfError::Layout(m) => write!(f, "pdf: {m}"),
97 PdfError::Ocr(m) => write!(f, "pdf: {m}"),
98 }
99 }
100}
101
102impl std::error::Error for PdfError {}
103
104#[cfg(feature = "ml")]
105impl From<pdfium_render::prelude::PdfiumError> for PdfError {
106 fn from(e: pdfium_render::prelude::PdfiumError) -> Self {
107 PdfError::Pdfium(e.to_string())
108 }
109}
110
111/// Convert a PDF's **embedded text layer only** — no pdfium, no ONNX, no
112/// threads: the pure-Rust content-stream parser ([`textparse`]) feeds the same
113/// orphan-region assembly the `no_ocr` pipeline flag uses, so text-layer PDFs
114/// come out identical to `--no-ocr` (flat, line-grouped paragraphs in reading
115/// order; no headings/lists/tables/pictures, and no hyperlink recovery).
116///
117/// This is the only conversion entry compiled without the `ml` feature (it is
118/// what a wasm32 build runs). A scanned/image-only PDF (no embedded text
119/// layer) yields an empty document rather than an error, same as `no_ocr` —
120/// callers can detect that and fall back to an OCR-capable build.
121pub fn convert_text_layer(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
122 convert_text_layer_pages(bytes, name, None)
123}
124
125/// [`convert_text_layer`] restricted to a **1-based inclusive** page window
126/// (issue #80's `--pages`); `None` converts everything. The window is
127/// validated the same way as [`Pipeline::pages`]: `first <= last`, 1-based,
128/// and it must select at least one existing page.
129pub fn convert_text_layer_pages(
130 bytes: &[u8],
131 name: &str,
132 pages: Option<(usize, usize)>,
133) -> Result<DoclingDocument, PdfError> {
134 if let Some((first, last)) = pages {
135 if first == 0 || last < first {
136 return Err(PdfError::Pdfium(format!(
137 "invalid page range {first}-{last} (pages are 1-based, first <= last)"
138 )));
139 }
140 }
141 let mut doc = DoclingDocument::new(name);
142 let mut total = 0usize;
143 let parsed = textparse::pdf_text_pages(bytes);
144 // A vestigial layer (a few typed-in form fields over scanned pages) is not
145 // the document's text: return the empty document, which callers already
146 // report as "no text layer" — so an OCR-capable caller falls back to OCR
147 // instead of proudly extracting thirteen characters.
148 if textparse::text_layer_is_vestigial(&parsed) {
149 return Ok(doc);
150 }
151 for (i, page) in parsed.into_iter().enumerate() {
152 total += 1;
153 if let Some((first, last)) = pages {
154 if i + 1 < first || i + 1 > last {
155 continue;
156 }
157 }
158 let mut regions = Vec::new();
159 assemble::add_orphan_regions(&mut regions, &page.cells);
160 let table_rows = vec![None; regions.len()];
161 let enrich_out = vec![None; regions.len()];
162 let (mut nodes, links) = assemble::assemble_page(&page, regions, &table_rows, &enrich_out);
163 assemble::stamp_page_no(&mut nodes, i + 1);
164 doc.nodes.extend(nodes);
165 doc.links.extend(links);
166 }
167 if let Some((first, last)) = pages {
168 if first > total {
169 return Err(PdfError::Pdfium(format!(
170 "page range {first}-{last} is outside the document ({total} page(s))"
171 )));
172 }
173 }
174 assemble::merge_continuations(&mut doc.nodes);
175 Ok(doc)
176}
177
178/// Threads ONNX inference may use, capped by `DOCLING_RS_PDF_THREADS` if set.
179/// Defaults to the available parallelism (ort otherwise picks a low number).
180#[cfg(feature = "ml")]
181pub(crate) fn intra_threads() -> usize {
182 if let Some(n) = std::env::var("DOCLING_RS_PDF_THREADS")
183 .ok()
184 .and_then(|v| v.parse::<usize>().ok())
185 .filter(|&n| n > 0)
186 {
187 return n;
188 }
189 std::thread::available_parallelism()
190 .map(|n| n.get())
191 .unwrap_or(1)
192}
193
194#[cfg(feature = "ml")]
195/// True when `DOCLING_RS_FP32` (any value but `0`) forces the full-precision
196/// models even where an INT8 variant sits next to the fp32 default.
197pub(crate) fn fp32_forced() -> bool {
198 std::env::var("DOCLING_RS_FP32")
199 .map(|v| v != "0")
200 .unwrap_or(false)
201}
202
203#[cfg(feature = "ml")]
204/// Should the int8 model defaults be skipped in favor of fp32? Either the
205/// user said so (`DOCLING_RS_FP32`), or a GPU execution provider is selected
206/// (#74) — the int8 exports are QDQ graphs calibrated for CPU kernels and
207/// only conformance-validated there. An explicit `DOCLING_*_ONNX` path
208/// override still wins over this at every call site.
209pub(crate) fn prefer_fp32() -> bool {
210 fp32_forced() || ep::prefers_fp32()
211}
212
213#[cfg(feature = "ml")]
214/// Resolve a default (CWD-relative) asset path. If it doesn't exist relative
215/// to the current directory, try next to the executable and one level above
216/// it (following symlinks — the layout `scripts/install/install.sh` produces:
217/// `/usr/local/bin/docling-rs` → `/usr/local/docling.rs/bin/docling-rs`
218/// with `models/` and `.pdfium/` in `/usr/local/docling.rs`). Lets an
219/// installed binary run from any working directory with no env vars; explicit
220/// env overrides never reach this. Returns `rel` unchanged when nothing
221/// exists anywhere, so callers' error messages keep the familiar path.
222pub(crate) fn resolve_asset(rel: &str) -> String {
223 if std::path::Path::new(rel).exists() {
224 return rel.to_string();
225 }
226 if let Some(dir) = std::env::current_exe()
227 .ok()
228 .and_then(|p| p.canonicalize().ok())
229 .and_then(|p| p.parent().map(std::path::Path::to_path_buf))
230 {
231 for base in [Some(dir.as_path()), dir.parent()].into_iter().flatten() {
232 let p = base.join(rel);
233 if p.exists() {
234 return p.to_string_lossy().into_owned();
235 }
236 }
237 }
238 rel.to_string()
239}
240
241/// One resolved runtime asset — which file a stage would load right now,
242/// given the CWD, the env overrides and the int8/fp32 preference.
243#[cfg(feature = "ml")]
244#[derive(Debug, Clone)]
245pub struct ModelEntry {
246 /// Pipeline stage, e.g. `layout`, `tableformer.decoder`, `ocr.rec`.
247 pub stage: &'static str,
248 /// The resolved path (absolute or CWD-relative, as it will be opened).
249 pub path: String,
250 /// Whether the file exists right now.
251 pub found: bool,
252 /// File size in bytes (0 when missing) — enough to tell an int8 quant
253 /// from an fp32 graph, or a stale model from a re-published one, at a
254 /// glance without hashing gigabytes per request.
255 pub bytes: u64,
256}
257
258/// Resolve the whole runtime model set **without loading anything** — the
259/// exact selection each stage performs at load time (layout honors the
260/// int8/fp32 preference, TableFormer its decoder ranking, OCR the language
261/// pair), plus the pdfium library. docling-serve exposes this at
262/// `/v1/config` and logs it at startup, so "the server picked up different
263/// models" is one `curl` away instead of a mystery of dissolved tables.
264/// Resolution is CWD-relative with an exe-dir fallback, so the answer can
265/// legitimately differ between two working directories.
266#[cfg(feature = "ml")]
267pub fn model_inventory() -> Vec<ModelEntry> {
268 fn entry(stage: &'static str, path: String) -> ModelEntry {
269 let meta = std::fs::metadata(&path).ok();
270 ModelEntry {
271 stage,
272 found: meta.is_some(),
273 bytes: meta.map(|m| m.len()).unwrap_or(0),
274 path,
275 }
276 }
277 let (enc, dec, bbx) = tableformer::resolved_paths();
278 let (rec, dict) = ocr::resolve_rec_pair(ocr::OcrLang::from_env());
279 let pdfium =
280 std::env::var("PDFIUM_DYNAMIC_LIB_PATH").unwrap_or_else(|_| resolve_asset(".pdfium/lib"));
281 vec![
282 entry(
283 "layout",
284 model_path(
285 "DOCLING_LAYOUT_ONNX",
286 "models/layout_heron.onnx",
287 "models/layout_heron_int8.onnx",
288 ),
289 ),
290 entry("tableformer.encoder", enc),
291 entry("tableformer.decoder", dec),
292 entry("tableformer.bbox", bbx),
293 entry("ocr.rec", rec),
294 entry("ocr.dict", dict),
295 entry("pdfium", pdfium),
296 ]
297}
298
299/// Resolve a model path: an explicit env override always wins; otherwise the
300/// INT8 variant of the default path when it exists on disk (the quantized
301/// models are conformance-validated — see docs/PDF_CONFORMANCE.md — and load/run
302/// markedly faster on CPU), unless `DOCLING_RS_FP32` opts back into full
303/// precision; else the fp32 default.
304#[cfg(feature = "ml")]
305pub(crate) fn model_path(env: &str, fp32_default: &str, int8_default: &str) -> String {
306 if let Ok(p) = std::env::var(env) {
307 return p;
308 }
309 if !prefer_fp32() {
310 let p = resolve_asset(int8_default);
311 if std::path::Path::new(&p).exists() {
312 return p;
313 }
314 }
315 resolve_asset(fp32_default)
316}
317
318/// Decode a standalone image with hard resource limits. A crafted image can
319/// declare enormous dimensions in a few-KB file; `image::load_from_memory`
320/// then tries to allocate the full pixel buffer (e.g. 60000×60000 → ~10 GB),
321/// and allocation failure aborts the whole process, bypassing the per-request
322/// panic catch. The 256 MiB alloc / 30000-px caps below turn that into a
323/// recoverable decode error instead. `DOCLING_RS_MAX_IMAGE_PIXELS` overrides
324/// the per-side pixel cap for the rare legitimately-huge scan.
325///
326/// Gated on `ml`: the only callers (`convert_image`, the METS backend) are
327/// ML-only, and the `image` crate is an `ml`-feature dependency — the
328/// text-layer wasm build has neither.
329#[cfg(feature = "ml")]
330pub(crate) fn decode_image_limited(bytes: &[u8]) -> Result<image::RgbImage, PdfError> {
331 let max_side: u32 = std::env::var("DOCLING_RS_MAX_IMAGE_PIXELS")
332 .ok()
333 .and_then(|v| v.parse().ok())
334 .unwrap_or(30_000);
335 decode_image_with_max_side(bytes, max_side)
336}
337
338#[cfg(feature = "ml")]
339fn decode_image_with_max_side(bytes: &[u8], max_side: u32) -> Result<image::RgbImage, PdfError> {
340 use image::ImageReader;
341 use std::io::Cursor;
342
343 let mut limits = image::Limits::default();
344 limits.max_image_width = Some(max_side);
345 limits.max_image_height = Some(max_side);
346 limits.max_alloc = Some(256 * 1024 * 1024);
347
348 let mut reader = ImageReader::new(Cursor::new(bytes))
349 .with_guessed_format()
350 .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?;
351 reader.limits(limits);
352 Ok(reader
353 .decode()
354 .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?
355 .into_rgb8())
356}
357
358#[cfg(feature = "ml")]
359/// One page's assembled output: typed nodes plus the page's hyperlinks (kept
360/// separate so pages processed out of order can be stitched back in page
361/// order) and its confidence scores (#183).
362type PageOut = (
363 Vec<Node>,
364 Vec<(String, String)>,
365 docling_core::confidence::PageConfidence,
366);
367
368#[cfg(feature = "ml")]
369/// The pool-wide TableFormer slot: one instance shared by every worker, loaded
370/// lazily on the first table region any worker sees. Tables appear on a
371/// minority of pages, so per-worker copies mostly multiplied ~0.4 GB of
372/// weights+arenas by the pool size for nothing; a single shared instance keeps
373/// the peak flat regardless of pool width, and a table's structure prediction
374/// is independent of which worker runs it, so output is byte-identical. The
375/// mutex serialises concurrent tables — the shared instance is loaded with the
376/// full intra-op thread budget to compensate (one wide TableFormer instead of
377/// several narrow ones).
378enum TfSlot {
379 /// Not attempted yet (no table seen so far).
380 Unloaded,
381 /// Load attempted, graphs absent — geometric fallback (warned once).
382 Missing,
383 Ready(tableformer::TableFormer),
384}
385
386#[cfg(feature = "ml")]
387type SharedTables = Arc<Mutex<TfSlot>>;
388
389#[cfg(feature = "ml")]
390/// The same lazy shared-slot pattern for the (rarer still) enrichment models:
391/// one instance per pipeline, loaded on the first region that needs it.
392enum EnrichSlot<T> {
393 Unloaded,
394 /// Load attempted, model files absent — enrichment skipped (warned once).
395 Missing,
396 Ready(T),
397}
398
399#[cfg(feature = "ml")]
400type SharedClassifier = Arc<Mutex<EnrichSlot<enrich::PictureClassifier>>>;
401#[cfg(feature = "ml")]
402type SharedCodeFormula = Arc<Mutex<EnrichSlot<enrich::CodeFormula>>>;
403
404#[cfg(feature = "ml")]
405/// The opt-in enrichment passes, mirroring docling's `PdfPipelineOptions`
406/// flags (`do_picture_classification`, `do_code_enrichment`,
407/// `do_formula_enrichment`). All off by default.
408#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
409pub struct EnrichmentOptions {
410 /// Classify each picture with DocumentFigureClassifier (26 classes).
411 pub picture_classification: bool,
412 /// Rewrite code blocks (and detect their language) with CodeFormulaV2.
413 pub code: bool,
414 /// Decode display formulas to LaTeX with CodeFormulaV2.
415 pub formula: bool,
416}
417
418#[cfg(feature = "ml")]
419impl EnrichmentOptions {
420 fn any(&self) -> bool {
421 self.picture_classification || self.code || self.formula
422 }
423}
424
425#[cfg(feature = "ml")]
426/// The layout model's input for a page: the docling-exact scale-1.0 page
427/// image when the renderer produced one, else the legacy stretch of the 2×
428/// bitmap (browser / METS paths) — see [`layout::LayoutSrc`]. Public so the
429/// diagnostic examples feed [`layout::LayoutModel::predict`] the same input
430/// the pipeline does.
431pub fn layout_src(page: &PdfPage) -> layout::LayoutSrc<'_> {
432 match &page.image_layout {
433 Some(img) => layout::LayoutSrc::PageImage(img),
434 None => layout::LayoutSrc::Raw(&page.image),
435 }
436}
437
438#[cfg(feature = "ml")]
439/// A self-contained set of the per-page models (layout, OCR). Each parallel
440/// page-worker owns its own `Worker` so inference runs concurrently without
441/// sharing an ONNX session (`ort`'s `Session::run` is `&mut self`); only the
442/// rarely-hit TableFormer is shared (see [`TfSlot`]).
443struct Worker {
444 /// `None` when `no_ocr` skips layout entirely — no model load, no inference.
445 layout: Option<layout::LayoutModel>,
446 ocr: Option<ocr::OcrModel>,
447 /// Shared TableFormer slot; `None` when `no_table_former`/`no_ocr` skip it.
448 tables: Option<SharedTables>,
449 /// Shared enrichment slots; `None` unless the corresponding flag is on.
450 classifier: Option<SharedClassifier>,
451 code_formula: Option<SharedCodeFormula>,
452 enrich: EnrichmentOptions,
453 /// Skip layout, OCR, and TableFormer; reconstruct text purely from the PDF's
454 /// embedded text layer. See [`Pipeline::no_ocr`].
455 no_ocr: bool,
456 /// Discard the embedded text layer and OCR every page. See
457 /// [`Pipeline::force_full_page_ocr`].
458 force_full_page_ocr: bool,
459 /// Keep text-panel pictures as pictures instead of demoting them to
460 /// paragraphs. See [`Pipeline::no_text_panels`].
461 no_text_panels: bool,
462 /// Which recognition model [`Self::ocr`] loads. See [`Pipeline::ocr_lang`].
463 ocr_lang: ocr::OcrLang,
464}
465
466#[cfg(feature = "ml")]
467impl Worker {
468 #[allow(clippy::too_many_arguments)] // mirrors the Pipeline's option set
469 fn load(
470 intra: usize,
471 tables: Option<SharedTables>,
472 enrich_slots: (Option<SharedClassifier>, Option<SharedCodeFormula>),
473 enrich: EnrichmentOptions,
474 no_ocr: bool,
475 force_full_page_ocr: bool,
476 no_text_panels: bool,
477 ocr_lang: ocr::OcrLang,
478 ) -> Result<Self, PdfError> {
479 Ok(Self {
480 layout: if no_ocr {
481 None
482 } else {
483 Some(layout::LayoutModel::load_with(intra).map_err(PdfError::Layout)?)
484 },
485 ocr: None,
486 tables,
487 classifier: enrich_slots.0,
488 code_formula: enrich_slots.1,
489 enrich,
490 no_ocr,
491 force_full_page_ocr,
492 no_text_panels,
493 ocr_lang,
494 })
495 }
496
497 /// Run layout (+ OCR for cell-less pages) + TableFormer and assemble page `n`
498 /// into its nodes and links. Pure given the page (mutates only the worker's
499 /// lazily-loaded OCR model), so it is safe to run concurrently across pages.
500 fn process(&mut self, n: usize, page: &mut PdfPage) -> Result<PageOut, PdfError> {
501 if self.no_ocr {
502 // Fastest path: no layout/OCR/TableFormer inference at all. The PDF's
503 // embedded text cells (if any) become flat, line-grouped paragraphs in
504 // reading order via the same orphan-region machinery that normally
505 // rescues text the detector missed — here it rescues *all* of it.
506 // Pages with no embedded text layer (scanned/image-only) yield nothing;
507 // convert those without `no_ocr`.
508 let parse = quality::parse_score(&page.cells);
509 let mut regions = Vec::new();
510 assemble::add_orphan_regions(&mut regions, &page.cells);
511 let table_rows = vec![None; regions.len()];
512 let enrich_out = vec![None; regions.len()];
513 let conf = quality::page_confidence(parse, ®ions, &[]);
514 let (nodes, links) = timing::timed("assemble_page", || {
515 assemble::assemble_page(page, regions, &table_rows, &enrich_out)
516 });
517 return Ok((nodes, links, conf));
518 }
519 let regions = timing::timed("layout.predict", || {
520 self.layout
521 .as_mut()
522 .expect("layout model loaded unless no_ocr")
523 .predict(layout_src(page), page.width, page.height)
524 })
525 .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
526 self.finish_page(n, page, regions)
527 }
528
529 /// Layout-detect a whole batch of pages with one inference call (issue #73),
530 /// then run each page's remaining stages (OCR / TableFormer / enrichment /
531 /// assembly) per page. Index-aligned with `items`; a layout failure fails
532 /// every page in the batch (they shared the one inference call).
533 fn process_batch(&mut self, items: &mut [(usize, PdfPage)]) -> Vec<Result<PageOut, PdfError>> {
534 if self.no_ocr {
535 // No layout model to batch — the text-layer-only path is per page.
536 return items
537 .iter_mut()
538 .map(|(n, page)| {
539 let n = *n;
540 self.process(n, page)
541 })
542 .collect();
543 }
544 let inputs: Vec<(layout::LayoutSrc<'_>, f32, f32)> = items
545 .iter()
546 .map(|(_, page)| (layout_src(page), page.width, page.height))
547 .collect();
548 let batched = timing::timed("layout.predict", || {
549 self.layout
550 .as_mut()
551 .expect("layout model loaded unless no_ocr")
552 .predict_batch(&inputs)
553 });
554 match batched {
555 Ok(all) => items
556 .iter_mut()
557 .zip(all)
558 .map(|((n, page), regions)| self.finish_page(*n, page, regions))
559 .collect(),
560 Err(e) => items
561 .iter()
562 .map(|(n, _)| Err(PdfError::Layout(format!("page {}: {e}", n + 1))))
563 .collect(),
564 }
565 }
566
567 /// Everything after layout detection: per-label confidence thresholds,
568 /// overlap resolution, orphan-text recovery, OCR for cell-less pages,
569 /// TableFormer, enrichment, and page assembly.
570 fn finish_page(
571 &mut self,
572 n: usize,
573 page: &mut PdfPage,
574 regions: Vec<layout::Region>,
575 ) -> Result<PageOut, PdfError> {
576 // Force-OCR is exactly "pretend the text layer is not there": clear
577 // every cell kind the extractors produced before anything reads them,
578 // and the ordinary no-text-layer machinery below — full-page OCR,
579 // OCR-fed TableFormer matching — takes over unchanged. (`no_ocr` wins
580 // when both are set, mirroring docling, where `force_full_page_ocr`
581 // is a sub-option of `do_ocr`; the no-ocr path never reaches here.)
582 // Done here rather than in `process` so the batched layout path
583 // (`process_batch` → `finish_page`) honors the flag too.
584 // Parse quality is scored on the extracted text layer before force-OCR
585 // discards it (docling's page-preprocessing stage runs before OCR too,
586 // so its parse_score also reflects the original text layer).
587 let parse = quality::parse_score(&page.cells);
588 // Recognition confidences of every OCR'd cell on this page → ocr_score.
589 let mut ocr_confs: Vec<f32> = Vec::new();
590 if self.force_full_page_ocr {
591 page.cells.clear();
592 page.code_cells.clear();
593 page.word_cells.clear();
594 }
595 // Quant-robustness guard: the default int8 layout graph keeps its
596 // confidences near the 0.5 label thresholds, and a different CPU's
597 // quantized kernels can flip a whole page's detections under them —
598 // tables and paragraphs then dissolve into orphan one-liners while the
599 // same build converts the page perfectly elsewhere. When a dense
600 // digital page ends up with detections covering almost none of its
601 // text cells, re-run that one page on the fp32 graph (lazy-loaded,
602 // auto-int8 selection only) and keep whichever detections cover more.
603 let mut regions = regions;
604 if !page.cells.is_empty() {
605 let thresholded = |rs: &[layout::Region]| -> Vec<layout::Region> {
606 rs.iter()
607 .filter(|r| r.score >= layout::label_threshold(r.label))
608 .cloned()
609 .collect()
610 };
611 let text_cells = page
612 .cells
613 .iter()
614 .filter(|c| !c.text.trim().is_empty())
615 .count();
616 let cov = assemble::layout_cell_coverage(&thresholded(®ions), &page.cells);
617 if text_cells >= 15 && cov < 0.5 {
618 let retry = self
619 .layout
620 .as_mut()
621 .expect("layout model loaded unless no_ocr")
622 .predict_fp32_fallback(layout_src(page), page.width, page.height)
623 .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
624 if let Some(retry) = retry {
625 let cov2 = assemble::layout_cell_coverage(&thresholded(&retry), &page.cells);
626 if cov2 > cov {
627 eprintln!(
628 "docling-pdf: page {}: int8 layout covered {:.0}% of the text \
629 cells; the fp32 retry covers {:.0}% — using it",
630 n + 1,
631 cov * 100.0,
632 cov2 * 100.0
633 );
634 regions = retry;
635 }
636 }
637 }
638 }
639 // docling's LayoutPostprocessor drops each detection below its label's
640 // confidence threshold (stricter than the 0.3 base the predictor keeps),
641 // before any overlap resolution. This removes the low-confidence tables /
642 // pictures / list-items that otherwise double-emit or mis-classify.
643 if std::env::var("DOCLING_RS_DEBUG_REGIONS").is_ok() {
644 for r in ®ions {
645 eprintln!(
646 "DBG raw {} {:.2} [{:.0},{:.0},{:.0},{:.0}]",
647 r.label, r.score, r.l, r.t, r.r, r.b
648 );
649 }
650 }
651 regions.retain(|r| r.score >= layout::label_threshold(r.label));
652 // docling's same-label picture dedup runs on the thresholded
653 // detections, before overlap resolution: a figure proposed both whole
654 // and as sub-panels collapses to one box (see `dedup_pictures`).
655 assemble::dedup_pictures(&mut regions);
656 // Resolve overlapping detections once, before OCR.
657 let mut regions = assemble::resolve(regions);
658 // Emit text the detector missed as orphan text regions (docling parity).
659 assemble::add_orphan_regions(&mut regions, &page.cells);
660 // Drop phantom empty low-confidence picture boxes (docling parity).
661 assemble::drop_false_pictures(&mut regions, &page.cells, page.width, page.height);
662 // A regular region fully inside a surviving table/index/picture is that
663 // special's child (a cell / in-figure label), not a separate block —
664 // remove it so it isn't emitted twice (docling parity).
665 assemble::drop_contained_regulars(&mut regions);
666 // No text layer → recognise text from the page image via OCR.
667 let ocred = page.cells.is_empty();
668 if ocred {
669 if self.ocr.is_none() {
670 self.ocr = Some(ocr::OcrModel::load(self.ocr_lang).map_err(PdfError::Ocr)?);
671 }
672 let cells = timing::timed("ocr.page", || {
673 self.ocr
674 .as_mut()
675 .unwrap()
676 .ocr_page(&page.image, ®ions, page.scale)
677 })
678 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
679 ocr_confs.extend(cells.iter().map(|(_, conf)| conf));
680 page.cells = cells.into_iter().map(|(cell, _)| cell).collect();
681 // Table interiors carry no words yet: region-scoped OCR skips
682 // table labels, and a scanned page has no pdfium text layer — so
683 // TableFormer's cell matcher got an empty word list and the table
684 // dissolved (#173). Recognize the table regions' word crops
685 // (mirroring the browser scanned path): `word_cells` feeds the
686 // matcher, and the same cells join `cells` so the geometric
687 // fallback and the table's region text see them too.
688 if regions.iter().any(|r| assemble::is_table_like(r.label)) {
689 let words = timing::timed("ocr.table_words", || {
690 self.ocr
691 .as_mut()
692 .unwrap()
693 .ocr_table_words(&page.image, ®ions, page.scale)
694 })
695 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
696 ocr_confs.extend(words.iter().map(|(_, conf)| conf));
697 let words: Vec<_> = words.into_iter().map(|(cell, _)| cell).collect();
698 page.cells.extend(words.iter().cloned());
699 page.word_cells = words;
700 }
701 }
702 // Region-scoped OCR skips `picture` interiors, and a digital page's
703 // text layer cannot see into an embedded raster either — so a figure
704 // that is really a text box (terms-and-conditions exported as an
705 // image) lost its words on every page kind. Python docling OCRs the
706 // bitmap-covered areas of *every* page — even digital ones — once they
707 // exceed `bitmap_area_threshold` (5 % of the page); the browser paths
708 // already do. Recognize the big text-less crops here too; the panel
709 // demotion / orphan recovery below place the lines.
710 let mut pic_cells: Vec<pdfium_backend::TextCell> = Vec::new();
711 {
712 let page_area = (page.width * page.height).max(1.0);
713 let has_text = |r: &layout::Region| {
714 page.cells.iter().any(|c| {
715 let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
716 let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
717 let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
718 !c.text.trim().is_empty() && ix * iy / ca > 0.5
719 })
720 };
721 // A captioned picture can never demote to a text panel (see
722 // recover_text_panels), and on digital pages its speculative OCR
723 // would be discarded anyway — don't pay for it.
724 let captioned = |r: &layout::Region| {
725 regions.iter().any(|c| {
726 c.label == "caption"
727 && c.r.min(r.r) - c.l.max(r.l) > 0.0
728 && ((c.t >= r.b && c.t - r.b <= 25.0) || (r.t >= c.b && r.t - c.b <= 25.0))
729 })
730 };
731 let bare: Vec<layout::Region> = regions
732 .iter()
733 .filter(|r| {
734 r.label == "picture"
735 && (r.r - r.l) * (r.b - r.t) / page_area >= 0.05
736 && !has_text(r)
737 && (ocred || !captioned(r))
738 })
739 .map(|r| layout::Region {
740 label: "text",
741 ..r.clone()
742 })
743 .collect();
744 if !bare.is_empty() {
745 if self.ocr.is_none() {
746 self.ocr = Some(ocr::OcrModel::load(self.ocr_lang).map_err(PdfError::Ocr)?);
747 }
748 let scored = timing::timed("ocr.pictures", || {
749 self.ocr
750 .as_mut()
751 .unwrap()
752 .ocr_page(&page.image, &bare, page.scale)
753 })
754 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
755 // Speculative in-picture OCR counts toward ocr_score only on
756 // OCR'd pages, where the recognized lines actually join the
757 // output; on a digital page they may be discarded below.
758 if ocred {
759 ocr_confs.extend(scored.iter().map(|(_, conf)| conf));
760 }
761 pic_cells = scored.into_iter().map(|(cell, _)| cell).collect();
762 page.cells.extend(pic_cells.iter().cloned());
763 }
764 }
765 let cells_before_pic_ocr = page.cells.len() - pic_cells.len();
766 // A "picture" that is really a colored text panel — dense, wide,
767 // multi-line — reads out as paragraphs instead of shipping as pixels;
768 // sparse in-picture text (a chart's labels) keeps the crop and stays
769 // inside it as the picture's silent children (docling parity, #200).
770 // `no_text_panels` (#173) opts out entirely for image-extraction
771 // workflows.
772 if !self.no_text_panels {
773 assemble::recover_text_panels(&mut regions, &page.cells);
774 }
775 // On an OCR'd page, in-picture text that did NOT demote its picture
776 // mostly stays silent, exactly as in docling: its postprocess step
777 // "Remove regular clusters that are included in wrappers" walks
778 // SPECIAL_TYPES — which includes PICTURE — so an orphan text cluster
779 // >80 % contained in a kept picture becomes that picture's child and
780 // never reaches the serializer. Only border-straddlers (≤80 %
781 // containment) survive as text. Emitting *everything* here used to
782 // splice a chart's OCR'd axis ticks into the body text right next to
783 // the image chunk (#200) — so the orphan pass places the recognized
784 // lines, then the same containment drop that handled the first wave
785 // re-runs to swallow the in-picture ones.
786 if ocred && !pic_cells.is_empty() {
787 // Pictures (and wrappers) no longer count as claimers (#165), so
788 // the plain orphan pass places the recognized lines directly.
789 assemble::add_orphan_regions(&mut regions, &pic_cells);
790 assemble::drop_contained_regulars(&mut regions);
791 } else if !ocred && !pic_cells.is_empty() {
792 // Digital page, picture kept: its speculative OCR cells must not
793 // linger in the text-cell set (they were appended at the tail).
794 let kept: Vec<layout::Region> = regions
795 .iter()
796 .filter(|r| r.label == "picture")
797 .cloned()
798 .collect();
799 let tail = page.cells.split_off(cells_before_pic_ocr);
800 page.cells.extend(tail.into_iter().filter(|c| {
801 !kept.iter().any(|r| {
802 let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
803 let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
804 let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
805 ix * iy / ca > 0.5
806 })
807 }));
808 }
809 // A text-less *table* detected inside a picture on a digital page — a
810 // screenshot of a table (2203's Figure 10) — has no text layer and no
811 // scanned-path OCR to feed it, so its grid used to serialize empty and
812 // the whole element vanished. docling OCRs bitmap-covered areas on
813 // every page kind and its table cluster collects those cells; mirror
814 // the scanned path for exactly these tables: recognize word crops and
815 // feed them to the TableFormer matcher and the cell set.
816 if !ocred {
817 let has_text = |t: &layout::Region| {
818 page.cells.iter().any(|c| {
819 let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
820 let ix = (t.r.min(c.r) - t.l.max(c.l)).max(0.0);
821 let iy = (t.b.min(c.b) - t.t.max(c.t)).max(0.0);
822 !c.text.trim().is_empty() && ix * iy / ca > 0.5
823 })
824 };
825 let in_picture = |t: &layout::Region| {
826 regions.iter().any(|r| {
827 r.label == "picture" && {
828 let ta = ((t.r - t.l) * (t.b - t.t)).max(1.0);
829 let ix = (r.r.min(t.r) - r.l.max(t.l)).max(0.0);
830 let iy = (r.b.min(t.b) - r.t.max(t.t)).max(0.0);
831 ix * iy / ta > 0.5
832 }
833 })
834 };
835 let pic_tables: Vec<layout::Region> = regions
836 .iter()
837 .filter(|t| assemble::is_table_like(t.label) && !has_text(t) && in_picture(t))
838 .cloned()
839 .collect();
840 if !pic_tables.is_empty() {
841 if self.ocr.is_none() {
842 self.ocr = Some(ocr::OcrModel::load(self.ocr_lang).map_err(PdfError::Ocr)?);
843 }
844 let words = timing::timed("ocr.table_words", || {
845 self.ocr
846 .as_mut()
847 .unwrap()
848 .ocr_table_words(&page.image, &pic_tables, page.scale)
849 })
850 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
851 ocr_confs.extend(words.iter().map(|(_, conf)| conf));
852 let words: Vec<_> = words.into_iter().map(|(cell, _)| cell).collect();
853 page.cells.extend(words.iter().cloned());
854 page.word_cells.extend(words);
855 }
856 }
857 // TableFormer structure per table region (else geometric fallback). The
858 // shared slot is only locked (and lazily loaded) when the page actually
859 // has a table, so table-free documents never pay for TableFormer at all.
860 let mut table_rows: Vec<Option<Vec<Vec<String>>>> = vec![None; regions.len()];
861 if let Some(slot) = self.tables.as_ref() {
862 if regions.iter().any(|r| assemble::is_table_like(r.label)) {
863 timing::timed("tableformer", || {
864 let mut guard = slot.lock().unwrap();
865 if matches!(*guard, TfSlot::Unloaded) {
866 // Full intra-op width: tables serialise on this mutex, so
867 // the one instance gets the whole thread budget.
868 *guard = match tableformer::TableFormer::load_with(intra_threads()) {
869 Some(tf) => TfSlot::Ready(tf),
870 None => TfSlot::Missing,
871 };
872 }
873 if let TfSlot::Ready(tf) = &mut *guard {
874 for (i, r) in regions.iter().enumerate() {
875 if assemble::is_table_like(r.label) {
876 table_rows[i] = tf.predict_table_rows(
877 &page.image,
878 [r.l, r.t, r.r, r.b],
879 &page.word_cells,
880 );
881 }
882 }
883 }
884 });
885 }
886 }
887 if std::env::var("DOCLING_RS_DEBUG_REGIONS").is_ok() {
888 for (i, r) in regions.iter().enumerate() {
889 eprintln!(
890 "DBG final {} {:.2} [{:.0},{:.0},{:.0},{:.0}] rows={:?}",
891 r.label,
892 r.score,
893 r.l,
894 r.t,
895 r.r,
896 r.b,
897 table_rows[i]
898 .as_ref()
899 .map(|t| (t.len(), t.first().map(|r| r.len())))
900 );
901 }
902 eprintln!(
903 "DBG cells={} words={}",
904 page.cells.len(),
905 page.word_cells.len()
906 );
907 }
908 // Enrichment passes (opt-in): DocumentPictureClassifier over picture
909 // regions, CodeFormulaV2 over code/formula regions. Same shared-slot
910 // shape as TableFormer — one lazily-loaded instance per pipeline, only
911 // ever locked when a page actually has a matching region.
912 let mut enrich_out: Vec<Option<assemble::Enrichment>> = vec![None; regions.len()];
913 if let Some(slot) = self.classifier.as_ref() {
914 if regions.iter().any(|r| r.label == "picture") {
915 timing::timed("picture_classifier", || {
916 let mut guard = slot.lock().unwrap();
917 if matches!(*guard, EnrichSlot::Unloaded) {
918 *guard = match enrich::PictureClassifier::load_with(intra_threads()) {
919 Some(m) => EnrichSlot::Ready(m),
920 None => EnrichSlot::Missing,
921 };
922 }
923 if let EnrichSlot::Ready(model) = &mut *guard {
924 for (i, r) in regions.iter().enumerate() {
925 if r.label != "picture" {
926 continue;
927 }
928 let Some(crop) = assemble::crop_region_scaled(
929 page,
930 [r.l, r.t, r.r, r.b],
931 enrich::CLASSIFIER_SCALE,
932 ) else {
933 continue;
934 };
935 match model.classify(&crop) {
936 Ok(classes) => {
937 enrich_out[i] =
938 Some(assemble::Enrichment::PictureClasses(classes));
939 }
940 Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
941 }
942 }
943 }
944 });
945 }
946 }
947 if let Some(slot) = self.code_formula.as_ref() {
948 let wants = |label: &str| {
949 (label == "code" && self.enrich.code) || (label == "formula" && self.enrich.formula)
950 };
951 if regions.iter().any(|r| wants(r.label)) {
952 timing::timed("code_formula", || {
953 let mut guard = slot.lock().unwrap();
954 if matches!(*guard, EnrichSlot::Unloaded) {
955 *guard = match enrich::CodeFormula::load_with(intra_threads()) {
956 Some(m) => EnrichSlot::Ready(m),
957 None => EnrichSlot::Missing,
958 };
959 }
960 if let EnrichSlot::Ready(model) = &mut *guard {
961 for (i, r) in regions.iter().enumerate() {
962 if !wants(r.label) {
963 continue;
964 }
965 // docling crops the postprocessed cluster box — the
966 // union of the region's text cells, not the raw
967 // detector box — expanded by 18% per side, at
968 // ~120 dpi.
969 let [bl, bt, br, bb] = assemble::region_cell_bbox(r, &page.cells)
970 .unwrap_or([r.l, r.t, r.r, r.b]);
971 let (w, h) = (br - bl, bb - bt);
972 let ex = enrich::CODE_FORMULA_EXPANSION;
973 let bbox = [bl - w * ex, bt - h * ex, br + w * ex, bb + h * ex];
974 let Some(crop) = assemble::crop_region_scaled(
975 page,
976 bbox,
977 enrich::CODE_FORMULA_SCALE,
978 ) else {
979 continue;
980 };
981 let kind = if r.label == "code" {
982 enrich::CodeFormulaKind::Code
983 } else {
984 enrich::CodeFormulaKind::Formula
985 };
986 match model.predict(&crop, kind) {
987 Ok(text) => {
988 enrich_out[i] = Some(match kind {
989 enrich::CodeFormulaKind::Code => {
990 let (code, language) =
991 enrich::extract_code_language(&text);
992 assemble::Enrichment::Code {
993 language,
994 text: code,
995 }
996 }
997 enrich::CodeFormulaKind::Formula => {
998 assemble::Enrichment::Formula { latex: text }
999 }
1000 });
1001 }
1002 Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
1003 }
1004 }
1005 }
1006 });
1007 }
1008 }
1009 // Score the final region set (docling assigns layout_score over the
1010 // postprocessed clusters — the same set assemble_page consumes).
1011 let conf = quality::page_confidence(parse, ®ions, &ocr_confs);
1012 let (nodes, links) = timing::timed("assemble_page", || {
1013 assemble::assemble_page(page, regions, &table_rows, &enrich_out)
1014 });
1015 Ok((nodes, links, conf))
1016 }
1017}
1018
1019#[cfg(feature = "ml")]
1020/// Per-worker ONNX intra-op threads. The layout model is memory-bandwidth bound,
1021/// so on a typical machine two threads per worker (sharing one in-cache copy of
1022/// the weights) extracts more throughput than one fat model or many single-thread
1023/// workers. `DOCLING_RS_PDF_INTRA` overrides for per-machine tuning.
1024fn pdf_intra() -> usize {
1025 if let Some(n) = std::env::var("DOCLING_RS_PDF_INTRA")
1026 .ok()
1027 .and_then(|v| v.parse::<usize>().ok())
1028 .filter(|&n| n > 0)
1029 {
1030 return n;
1031 }
1032 if intra_threads() >= 2 {
1033 2
1034 } else {
1035 1
1036 }
1037}
1038
1039#[cfg(feature = "ml")]
1040/// How many page-workers to spin up for a multi-page PDF. `DOCLING_RS_PDF_WORKERS`
1041/// overrides; otherwise size the pool so `workers × intra ≈ cores`, capped at 4 so
1042/// a worst-case pool holds a bounded amount of model memory (~0.4 GB per worker)
1043/// and does not oversaturate the memory bus with model-weight traffic.
1044fn pdf_worker_count() -> usize {
1045 if let Some(n) = std::env::var("DOCLING_RS_PDF_WORKERS")
1046 .ok()
1047 .and_then(|v| v.parse::<usize>().ok())
1048 .filter(|&n| n > 0)
1049 {
1050 return n;
1051 }
1052 (intra_threads() / pdf_intra()).clamp(1, 4)
1053}
1054
1055#[cfg(feature = "ml")]
1056/// Max pages a worker layout-detects with one batched inference call (issue
1057/// #73). Workers drain the work channel opportunistically up to this size —
1058/// whatever is already rendered gets batched, so batching never *waits* for
1059/// pages and adds no latency when rendering is the bottleneck.
1060///
1061/// Default: 4 on 8+ cores, 1 (per-page) below. Measured on a 4-core box the
1062/// batch only adds cache pressure and costs pipeline overlap (2 workers × 2
1063/// threads: 8.1 s/conv at batch=1 vs 9.3 s at batch=4 on the 9-page
1064/// 2206.01062 fixture); the single-session amortization it buys needs the
1065/// wider thread budget of a many-core machine. Output is bit-identical at
1066/// every batch size, so this is purely a throughput knob.
1067/// `DOCLING_RS_PDF_LAYOUT_BATCH` overrides; `1` restores per-page inference.
1068fn pdf_layout_batch() -> usize {
1069 std::env::var("DOCLING_RS_PDF_LAYOUT_BATCH")
1070 .ok()
1071 .and_then(|v| v.parse::<usize>().ok())
1072 .filter(|&n| n > 0)
1073 .unwrap_or_else(|| if intra_threads() >= 8 { 4 } else { 1 })
1074}
1075
1076#[cfg(feature = "ml")]
1077/// Minimum page count before a PDF is worth the parallel worker pool. Below this,
1078/// the serial primary (running its model on every core) is faster than fanning out
1079/// — the helper pool's one-time model-load cost only pays off once enough pages
1080/// share it. `DOCLING_RS_PDF_PARALLEL_MIN` overrides.
1081fn pdf_parallel_min() -> usize {
1082 std::env::var("DOCLING_RS_PDF_PARALLEL_MIN")
1083 .ok()
1084 .and_then(|v| v.parse::<usize>().ok())
1085 .filter(|&n| n > 0)
1086 .unwrap_or(6)
1087}
1088
1089#[cfg(feature = "ml")]
1090/// A reusable PDF pipeline. The **primary** worker runs its models on every core,
1091/// so a single-page / small / image / METS input is converted at full intra-op
1092/// speed with no pool to load. A document with enough pages instead fans out
1093/// across a **pool** of narrower workers processed concurrently. Both load lazily
1094/// and are cached for reuse, so a one-shot conversion only pays for what it uses.
1095pub struct Pipeline {
1096 /// Full-intra worker for the serial path; loaded on first serial use.
1097 primary: Option<Worker>,
1098 /// Narrower workers (≈cores/`target_workers` threads each) for the parallel
1099 /// path; loaded on first multi-page use and cached.
1100 pool: Vec<Worker>,
1101 /// The single TableFormer instance every worker shares (see [`TfSlot`]).
1102 tables: SharedTables,
1103 /// The shared enrichment-model slots (same pattern as [`TfSlot`]).
1104 classifier: SharedClassifier,
1105 code_formula: SharedCodeFormula,
1106 /// Desired pool size for multi-page documents.
1107 target_workers: usize,
1108 /// Page count at/above which the parallel pool is worth its load cost.
1109 parallel_min: usize,
1110 /// Skip loading/running TableFormer; table regions fall back to geometric
1111 /// reconstruction. See [`Pipeline::no_table_former`].
1112 no_table_former: bool,
1113 /// Skip layout, OCR, and TableFormer entirely. See [`Pipeline::no_ocr`].
1114 no_ocr: bool,
1115 /// OCR every page even when it carries a text layer. See
1116 /// [`Pipeline::force_full_page_ocr`].
1117 force_full_page_ocr: bool,
1118 /// Never demote text-panel pictures. See [`Pipeline::no_text_panels`].
1119 no_text_panels: bool,
1120 /// Opt-in enrichment passes. See [`Pipeline::enrichments`].
1121 enrich: EnrichmentOptions,
1122 /// 1-based inclusive page window to convert. See [`Pipeline::pages`].
1123 page_range: Option<(usize, usize)>,
1124 /// OCR recognition language. See [`Pipeline::ocr_lang`].
1125 ocr_lang: ocr::OcrLang,
1126}
1127
1128#[cfg(feature = "ml")]
1129impl Pipeline {
1130 /// Construct the pipeline. Models load lazily on first use (full-intra primary
1131 /// for serial inputs, the helper pool for multi-page PDFs), so nothing is
1132 /// loaded that a given document doesn't need.
1133 pub fn new() -> Result<Self, PdfError> {
1134 Ok(Self {
1135 primary: None,
1136 pool: Vec::new(),
1137 tables: Arc::new(Mutex::new(TfSlot::Unloaded)),
1138 classifier: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
1139 code_formula: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
1140 target_workers: pdf_worker_count(),
1141 parallel_min: pdf_parallel_min(),
1142 no_table_former: false,
1143 no_ocr: false,
1144 force_full_page_ocr: false,
1145 no_text_panels: false,
1146 enrich: EnrichmentOptions::default(),
1147 page_range: None,
1148 ocr_lang: ocr::OcrLang::from_env(),
1149 })
1150 }
1151
1152 /// Convert only pages `first..=last` (**1-based**, like the page numbers a
1153 /// PDF viewer shows — issue #80's `--pages A-B`). Out-of-range pages are
1154 /// skipped before rasterization, so the cost is proportional to the window,
1155 /// not the document. `last` past the end of the document clamps; a window
1156 /// that selects no pages at all (`first` beyond the last page) is an error
1157 /// at convert time. `None` (the default) converts everything.
1158 pub fn pages(mut self, range: Option<(usize, usize)>) -> Self {
1159 self.page_range = range;
1160 self
1161 }
1162
1163 /// In-place variant of [`pages`](Self::pages) for a long-lived pipeline
1164 /// (e.g. docling-serve's warm instance) that applies a per-request window
1165 /// without rebuilding — unlike the model switches, the window is pure
1166 /// configuration. Set it before every conversion; it stays until changed.
1167 pub fn set_pages(&mut self, range: Option<(usize, usize)>) {
1168 self.page_range = range;
1169 }
1170
1171 /// OCR recognition language (see [`OcrLang`]): English by default, `ch`
1172 /// for the multilingual docling-conformance model. `None` keeps the
1173 /// process default (`DOCLING_RS_OCR_LANG`, else English). Set before the
1174 /// first conversion; for a warm pipeline use
1175 /// [`set_ocr_lang`](Self::set_ocr_lang).
1176 pub fn ocr_lang(mut self, lang: Option<ocr::OcrLang>) -> Self {
1177 self.set_ocr_lang(lang);
1178 self
1179 }
1180
1181 /// In-place variant of [`ocr_lang`](Self::ocr_lang) for a long-lived
1182 /// pipeline (docling-serve's warm instance). Unlike the page window this
1183 /// is a *model* switch: any worker whose cached recognition model was
1184 /// loaded for a different language drops it, to be lazily reloaded on the
1185 /// next OCR-needing page (cheap — the rec models are ~10 MB).
1186 pub fn set_ocr_lang(&mut self, lang: Option<ocr::OcrLang>) {
1187 let lang = lang.unwrap_or_else(ocr::OcrLang::from_env);
1188 self.ocr_lang = lang;
1189 for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
1190 if worker.ocr_lang != lang {
1191 worker.ocr_lang = lang;
1192 worker.ocr = None;
1193 }
1194 }
1195 }
1196
1197 /// Resolve the configured 1-based window against a page count into the
1198 /// 0-based inclusive form the backend walks, validating it selects at
1199 /// least one existing page.
1200 fn resolve_range(&self, total: usize) -> Result<Option<(usize, usize)>, PdfError> {
1201 let Some((first, last)) = self.page_range else {
1202 return Ok(None);
1203 };
1204 if first == 0 || last < first {
1205 return Err(PdfError::Pdfium(format!(
1206 "invalid page range {first}-{last} (pages are 1-based, first <= last)"
1207 )));
1208 }
1209 if first > total {
1210 return Err(PdfError::Pdfium(format!(
1211 "page range {first}-{last} is outside the document ({total} page(s))"
1212 )));
1213 }
1214 Ok(Some((first - 1, last.min(total) - 1)))
1215 }
1216
1217 /// Enable the opt-in enrichment passes (docling's
1218 /// `do_picture_classification` / `do_code_enrichment` /
1219 /// `do_formula_enrichment`). Each enabled pass lazily loads its model on
1220 /// the first matching region; a missing model warns once and is skipped.
1221 /// Set before the first conversion (no effect on already-loaded workers).
1222 pub fn enrichments(mut self, opts: EnrichmentOptions) -> Self {
1223 self.enrich = opts;
1224 self
1225 }
1226
1227 /// Skip loading and running the TableFormer table-structure model. Table
1228 /// regions still get emitted, but reconstructed geometrically from cell
1229 /// positions instead of via the ONNX model's predicted structure — faster
1230 /// (no model load, no per-table inference) at the cost of table fidelity.
1231 /// No effect if a worker is already loaded; set this before the first
1232 /// conversion.
1233 pub fn no_table_former(mut self, disable: bool) -> Self {
1234 self.no_table_former = disable;
1235 self
1236 }
1237
1238 /// Keep every detected `picture` region as a picture. By default an
1239 /// *uncaptioned* picture that reads like a dense, uniform text panel (a
1240 /// terms-and-conditions box exported as an image) is demoted into
1241 /// paragraphs (#157); a chart the layout mislabels can still trip that
1242 /// heuristic on scanned pages, and image-extraction workflows may simply
1243 /// want every crop — this flag disables the demotion entirely (#173).
1244 /// No effect on already-loaded workers; set before the first conversion.
1245 pub fn no_text_panels(mut self, disable: bool) -> Self {
1246 self.no_text_panels = disable;
1247 self
1248 }
1249
1250 /// Skip layout detection, OCR, and TableFormer entirely — no model load, no
1251 /// inference of any kind. The PDF's embedded text cells are grouped by line
1252 /// and emitted as plain paragraphs in reading order: no headings, lists,
1253 /// tables, code blocks, or pictures, since that structure comes from the
1254 /// layout model. The fastest possible PDF path, but pages with no embedded
1255 /// text layer (scanned/image-only PDFs) yield no text at all — convert those
1256 /// without this flag. Implies `no_table_former`. No effect if a worker is
1257 /// already loaded; set this before the first conversion.
1258 pub fn no_ocr(mut self, disable: bool) -> Self {
1259 self.no_ocr = disable;
1260 self
1261 }
1262
1263 /// OCR every page from its rendered image even when the page carries an
1264 /// embedded text layer — docling's `force_full_page_ocr`. The escape hatch
1265 /// for text layers that exist but lie: broken encodings, subset fonts with
1266 /// garbage mappings, a scanned form with a few typed-in fields. Ignored
1267 /// when [`no_ocr`](Self::no_ocr) is set, mirroring docling (there
1268 /// `force_full_page_ocr` is a sub-option of `do_ocr`).
1269 pub fn force_full_page_ocr(mut self, force: bool) -> Self {
1270 self.force_full_page_ocr = force;
1271 self
1272 }
1273
1274 /// The shared TableFormer slot handed to each worker, or `None` when the
1275 /// pipeline options skip TableFormer entirely.
1276 fn tables_slot(&self) -> Option<SharedTables> {
1277 if self.no_table_former || self.no_ocr {
1278 None
1279 } else {
1280 Some(Arc::clone(&self.tables))
1281 }
1282 }
1283
1284 /// The shared enrichment slots for a worker (`None` per model unless its
1285 /// flag is on; `no_ocr` skips layout, so there are no regions to enrich).
1286 fn enrich_slots(&self) -> (Option<SharedClassifier>, Option<SharedCodeFormula>) {
1287 if self.no_ocr || !self.enrich.any() {
1288 return (None, None);
1289 }
1290 (
1291 self.enrich
1292 .picture_classification
1293 .then(|| Arc::clone(&self.classifier)),
1294 (self.enrich.code || self.enrich.formula).then(|| Arc::clone(&self.code_formula)),
1295 )
1296 }
1297
1298 /// Eagerly load the models (the full-intra serial worker: layout + OCR, and
1299 /// the shared TableFormer unless disabled) so the first conversion doesn't pay
1300 /// the load cost. Idempotent; respects `no_ocr` / `no_table_former` (with
1301 /// `no_ocr` there is nothing to load). The docling.rs analogue of docling's
1302 /// `DocumentConverter.initialize_pipeline`.
1303 pub fn warm_up(&mut self) -> Result<(), PdfError> {
1304 self.primary()?;
1305 Ok(())
1306 }
1307
1308 /// The full-intra serial worker, loaded on first use.
1309 fn primary(&mut self) -> Result<&mut Worker, PdfError> {
1310 if self.primary.is_none() {
1311 self.primary = Some(Worker::load(
1312 intra_threads(),
1313 self.tables_slot(),
1314 self.enrich_slots(),
1315 self.enrich,
1316 self.no_ocr,
1317 self.force_full_page_ocr,
1318 self.no_text_panels,
1319 self.ocr_lang,
1320 )?);
1321 }
1322 Ok(self.primary.as_mut().unwrap())
1323 }
1324
1325 /// Convert a PDF (bytes) to a [`DoclingDocument`]. A document with fewer than
1326 /// `parallel_min` pages (or a pool size of 1) streams through the full-intra
1327 /// primary; a larger one renders on this thread (pdfium is not thread-safe) and
1328 /// fans the pages out across the worker pool, reassembled in page order so the
1329 /// output is byte-identical to the serial path.
1330 pub fn convert(
1331 &mut self,
1332 bytes: &[u8],
1333 password: Option<&str>,
1334 name: &str,
1335 ) -> Result<DoclingDocument, PdfError> {
1336 let pages = pdfium_backend::page_count(bytes, password)?;
1337 let range = self.resolve_range(pages)?;
1338 // Serial vs parallel is decided by the pages actually converted: a
1339 // 3-page window over a 500-page PDF should not pay the pool load.
1340 let selected = range.map_or(pages, |(a, b)| b - a + 1);
1341 let doc = if self.target_workers >= 2 && selected >= self.parallel_min {
1342 self.convert_parallel(bytes, password, name, range)?
1343 } else {
1344 self.convert_serial(bytes, password, name, range)?
1345 };
1346 timing::report();
1347 Ok(doc)
1348 }
1349
1350 /// Stream pages one at a time through the primary worker — render → process →
1351 /// drop — so the document holds ~one page bitmap (~5 MB) at a time.
1352 fn convert_serial(
1353 &mut self,
1354 bytes: &[u8],
1355 password: Option<&str>,
1356 name: &str,
1357 range: Option<(usize, usize)>,
1358 ) -> Result<DoclingDocument, PdfError> {
1359 let mut doc = DoclingDocument::new(name);
1360 let mut confs = std::collections::BTreeMap::new();
1361 let render_image = !self.no_ocr;
1362 let worker = self.primary()?;
1363 pdfium_backend::for_each_page(
1364 bytes,
1365 password,
1366 render_image,
1367 range,
1368 |n, _total, mut page| {
1369 let (mut nodes, links, conf) = worker.process(n, &mut page)?;
1370 assemble::stamp_page_no(&mut nodes, n + 1);
1371 doc.nodes.extend(nodes);
1372 doc.links.extend(links);
1373 confs.insert(n + 1, conf);
1374 Ok::<(), PdfError>(())
1375 },
1376 )?;
1377 assemble::merge_continuations(&mut doc.nodes);
1378 doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1379 Ok(doc)
1380 }
1381
1382 /// Render pages serially on this thread (pdfium) and process them in parallel
1383 /// across the worker pool. A bounded channel applies backpressure so only a
1384 /// handful of page bitmaps are resident at once; results carry their page
1385 /// index and are reassembled in order, so the output is byte-identical to the
1386 /// serial path.
1387 fn convert_parallel(
1388 &mut self,
1389 bytes: &[u8],
1390 password: Option<&str>,
1391 name: &str,
1392 range: Option<(usize, usize)>,
1393 ) -> Result<DoclingDocument, PdfError> {
1394 self.ensure_pool()?;
1395 let n_workers = self.pool.len();
1396 let render_image = !self.no_ocr;
1397 let layout_batch = pdf_layout_batch();
1398 // Bound sized so every worker can accumulate a full layout batch while
1399 // rendering stays ahead (and never below the pre-#73 render-ahead of
1400 // two pages per worker); still a hard cap on resident page bitmaps.
1401 let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
1402 let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
1403 let results: Arc<Mutex<Vec<(usize, PageOut)>>> = Arc::new(Mutex::new(Vec::new()));
1404 let first_err: Arc<Mutex<Option<PdfError>>> = Arc::new(Mutex::new(None));
1405
1406 // Move the pool into the scope so each worker gets an exclusive `&mut`.
1407 let mut workers = std::mem::take(&mut self.pool);
1408 std::thread::scope(|s| {
1409 for worker in workers.iter_mut() {
1410 let work_rx = Arc::clone(&work_rx);
1411 let results = Arc::clone(&results);
1412 let first_err = Arc::clone(&first_err);
1413 s.spawn(move || loop {
1414 // Hold the receiver lock only for the recv (plus a non-blocking
1415 // drain up to the layout batch size); release before the (long)
1416 // per-page work so other workers can pull concurrently.
1417 let mut batch = Vec::new();
1418 {
1419 let rx = work_rx.lock().unwrap();
1420 match rx.recv() {
1421 Ok(item) => {
1422 batch.push(item);
1423 while batch.len() < layout_batch {
1424 match rx.try_recv() {
1425 Ok(item) => batch.push(item),
1426 Err(_) => break,
1427 }
1428 }
1429 }
1430 Err(_) => break,
1431 }
1432 }
1433 let outs = worker.process_batch(&mut batch);
1434 for ((idx, _), out) in batch.iter().zip(outs) {
1435 match out {
1436 Ok(out) => results.lock().unwrap().push((*idx, out)),
1437 Err(e) => {
1438 let mut slot = first_err.lock().unwrap();
1439 if slot.is_none() {
1440 *slot = Some(e);
1441 }
1442 }
1443 }
1444 }
1445 });
1446 }
1447 // Render on this thread and feed the workers; backpressure blocks here
1448 // when the channel is full. Dropping `work_tx` afterwards signals the
1449 // workers (recv → Err) to finish.
1450 let render = pdfium_backend::for_each_page(
1451 bytes,
1452 password,
1453 render_image,
1454 range,
1455 |i, _total, page| {
1456 work_tx
1457 .send((i, page))
1458 .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
1459 },
1460 );
1461 drop(work_tx);
1462 if let Err(e) = render {
1463 let mut slot = first_err.lock().unwrap();
1464 if slot.is_none() {
1465 *slot = Some(e);
1466 }
1467 }
1468 });
1469 // Threads have joined; restore the pool for the next conversion.
1470 self.pool = workers;
1471
1472 if let Some(e) = first_err.lock().unwrap().take() {
1473 return Err(e);
1474 }
1475 let mut results = Arc::try_unwrap(results)
1476 .unwrap_or_else(|arc| Mutex::new(arc.lock().unwrap().clone()))
1477 .into_inner()
1478 .unwrap();
1479 results.sort_by_key(|(idx, _)| *idx);
1480 let mut doc = DoclingDocument::new(name);
1481 let mut confs = std::collections::BTreeMap::new();
1482 for (idx, (mut nodes, links, conf)) in results {
1483 assemble::stamp_page_no(&mut nodes, idx + 1);
1484 doc.nodes.extend(nodes);
1485 doc.links.extend(links);
1486 confs.insert(idx + 1, conf);
1487 }
1488 assemble::merge_continuations(&mut doc.nodes);
1489 doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1490 Ok(doc)
1491 }
1492
1493 /// Convert a PDF in **streaming** mode: `emit` is called with each finalized,
1494 /// in-document-order batch of nodes (and that span's recovered links) as pages
1495 /// complete, so a caller can serialize Markdown page by page instead of waiting
1496 /// for the whole document. The batches are exactly the buffered [`convert`]'s
1497 /// nodes, split at safe block boundaries by [`assemble::StreamAssembler`] — the
1498 /// parallel path reorders pages back into document order before emitting, so
1499 /// the output is identical regardless of worker scheduling.
1500 ///
1501 /// `emit` runs on the calling thread (never a worker), so it needn't be `Send`
1502 /// and its backpressure throttles the whole pipeline. Returning `Err` from
1503 /// `emit` aborts the conversion with that error.
1504 pub fn convert_streaming<F>(
1505 &mut self,
1506 bytes: &[u8],
1507 password: Option<&str>,
1508 name: &str,
1509 emit: F,
1510 ) -> Result<(), PdfError>
1511 where
1512 F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1513 {
1514 let _ = name; // page nodes carry no name; the caller owns the document name.
1515 let pages = pdfium_backend::page_count(bytes, password)?;
1516 let range = self.resolve_range(pages)?;
1517 let selected = range.map_or(pages, |(a, b)| b - a + 1);
1518 let r = if self.target_workers >= 2 && selected >= self.parallel_min {
1519 self.convert_streaming_parallel(bytes, password, range, emit)
1520 } else {
1521 self.convert_streaming_serial(bytes, password, range, emit)
1522 };
1523 timing::report();
1524 r
1525 }
1526
1527 /// Serial streaming: render → process → emit, one page at a time, holding back
1528 /// only the tail that might still merge into the next page.
1529 fn convert_streaming_serial<F>(
1530 &mut self,
1531 bytes: &[u8],
1532 password: Option<&str>,
1533 range: Option<(usize, usize)>,
1534 mut emit: F,
1535 ) -> Result<(), PdfError>
1536 where
1537 F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1538 {
1539 let mut asm = assemble::StreamAssembler::new();
1540 let render_image = !self.no_ocr;
1541 let worker = self.primary()?;
1542 pdfium_backend::for_each_page(
1543 bytes,
1544 password,
1545 render_image,
1546 range,
1547 |n, _total, mut page| {
1548 // Confidence is dropped on the streaming path: the report is
1549 // only complete once every page has run, which defeats
1550 // page-by-page emission — buffered `convert` carries it.
1551 let (nodes, links, _conf) = worker.process(n, &mut page)?;
1552 emit(asm.push(nodes), links)
1553 },
1554 )?;
1555 emit(asm.finish(), Vec::new())
1556 }
1557
1558 /// Parallel streaming: pages render serially on a dedicated thread (pdfium is
1559 /// not thread-safe) and process across the worker pool; results carry their
1560 /// page index and are reordered on the calling thread into a
1561 /// [`assemble::StreamAssembler`], which emits each page in document order as
1562 /// soon as its predecessors have arrived. Bounded channels keep only a handful
1563 /// of pages resident and let `emit`'s backpressure reach the renderer.
1564 fn convert_streaming_parallel<F>(
1565 &mut self,
1566 bytes: &[u8],
1567 password: Option<&str>,
1568 range: Option<(usize, usize)>,
1569 mut emit: F,
1570 ) -> Result<(), PdfError>
1571 where
1572 F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1573 {
1574 self.ensure_pool()?;
1575 let n_workers = self.pool.len();
1576 let render_image = !self.no_ocr;
1577 let layout_batch = pdf_layout_batch();
1578 // Bound sized so every worker can accumulate a full layout batch while
1579 // rendering stays ahead (and never below the pre-#73 render-ahead of
1580 // two pages per worker); still a hard cap on resident page bitmaps.
1581 let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
1582 let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
1583 // Workers and the renderer report here; the calling thread drains it in
1584 // page order. Bounded so workers block (bounding resident bitmaps) when the
1585 // consumer falls behind.
1586 let (res_tx, res_rx) = sync_channel::<Result<(usize, PageOut), PdfError>>(n_workers * 2);
1587
1588 let mut workers = std::mem::take(&mut self.pool);
1589 let mut asm = assemble::StreamAssembler::new();
1590 let mut first_err: Option<PdfError> = None;
1591
1592 std::thread::scope(|s| {
1593 // Workers: pull a batch of pages (whatever is already rendered, up
1594 // to the layout batch size), process it, report (index-tagged)
1595 // results.
1596 for worker in workers.iter_mut() {
1597 let work_rx = Arc::clone(&work_rx);
1598 let res_tx = res_tx.clone();
1599 s.spawn(move || 'outer: loop {
1600 let mut batch = Vec::new();
1601 {
1602 let rx = work_rx.lock().unwrap();
1603 match rx.recv() {
1604 Ok(item) => {
1605 batch.push(item);
1606 while batch.len() < layout_batch {
1607 match rx.try_recv() {
1608 Ok(item) => batch.push(item),
1609 Err(_) => break,
1610 }
1611 }
1612 }
1613 Err(_) => break,
1614 }
1615 }
1616 let outs = worker.process_batch(&mut batch);
1617 for ((idx, _), out) in batch.iter().zip(outs) {
1618 if res_tx.send(out.map(|o| (*idx, o))).is_err() {
1619 break 'outer; // consumer gone
1620 }
1621 }
1622 });
1623 }
1624 // Renderer: feed pages to the pool on its own thread (pdfium stays on a
1625 // single thread); report a render error through the same channel.
1626 {
1627 let res_tx = res_tx.clone();
1628 s.spawn(move || {
1629 let render = pdfium_backend::for_each_page(
1630 bytes,
1631 password,
1632 render_image,
1633 range,
1634 |i, _total, page| {
1635 work_tx
1636 .send((i, page))
1637 .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
1638 },
1639 );
1640 drop(work_tx); // signal workers to finish
1641 if let Err(e) = render {
1642 let _ = res_tx.send(Err(e));
1643 }
1644 });
1645 }
1646 // Drop our own sender so the channel closes once the threads finish.
1647 drop(res_tx);
1648
1649 // Collector (this thread): reorder into document order and emit.
1650 // With a page window, indices start at the window's first page.
1651 let mut buffer: BTreeMap<usize, PageOut> = BTreeMap::new();
1652 let mut next = range.map_or(0, |(first, _)| first);
1653 for msg in res_rx.iter() {
1654 match msg {
1655 Err(e) => {
1656 if first_err.is_none() {
1657 first_err = Some(e);
1658 }
1659 }
1660 Ok((idx, out)) => {
1661 buffer.insert(idx, out);
1662 if first_err.is_some() {
1663 continue; // keep draining so the threads can exit
1664 }
1665 while let Some((nodes, links, _conf)) = buffer.remove(&next) {
1666 if let Err(e) = emit(asm.push(nodes), links) {
1667 first_err = Some(e);
1668 break;
1669 }
1670 next += 1;
1671 }
1672 }
1673 }
1674 }
1675 });
1676 // Threads have joined; restore the pool for the next conversion.
1677 self.pool = workers;
1678
1679 if let Some(e) = first_err {
1680 return Err(e);
1681 }
1682 emit(asm.finish(), Vec::new())
1683 }
1684
1685 /// Lazily grow the pool to `target_workers`, loading the new workers
1686 /// concurrently (model load is mostly I/O + mmap, so N loads overlap to roughly
1687 /// one load's wall-time). Cached for reuse across documents.
1688 fn ensure_pool(&mut self) -> Result<(), PdfError> {
1689 let need = self.target_workers.saturating_sub(self.pool.len());
1690 if need == 0 {
1691 return Ok(());
1692 }
1693 let intra = pdf_intra();
1694 let no_ocr = self.no_ocr;
1695 let force = self.force_full_page_ocr;
1696 let ntp = self.no_text_panels;
1697 let ocr_lang = self.ocr_lang;
1698 let enrich = self.enrich;
1699 let tables = self.tables_slot();
1700 let enrich_slots = self.enrich_slots();
1701 let loaded: Vec<Result<Worker, PdfError>> = std::thread::scope(|s| {
1702 let handles: Vec<_> = (0..need)
1703 .map(|_| {
1704 let tables = tables.clone();
1705 let enrich_slots = enrich_slots.clone();
1706 s.spawn(move || {
1707 Worker::load(
1708 intra,
1709 tables,
1710 enrich_slots,
1711 enrich,
1712 no_ocr,
1713 force,
1714 ntp,
1715 ocr_lang,
1716 )
1717 })
1718 })
1719 .collect();
1720 handles.into_iter().map(|h| h.join().unwrap()).collect()
1721 });
1722 for w in loaded {
1723 self.pool.push(w?);
1724 }
1725 Ok(())
1726 }
1727
1728 /// Convert a standalone image (PNG/JPEG/TIFF/WebP/…) as a single page —
1729 /// docling routes images through the same layout+OCR pipeline as a PDF page.
1730 pub fn convert_image(&mut self, bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
1731 let image = decode_image_limited(bytes)?;
1732 let (w, h) = image.dimensions();
1733 // The image is its own page rendered at 1 px per "point" (scale 1.0); a
1734 // standalone image has no text layer, so OCR supplies the cells.
1735 let page = PdfPage {
1736 width: w as f32,
1737 height: h as f32,
1738 scale: 1.0,
1739 cells: Vec::new(),
1740 code_cells: Vec::new(),
1741 word_cells: Vec::new(),
1742 // A standalone image *is* its own scale-1.0 page image, so the
1743 // layout model sees it through the docling-exact PIL kernel.
1744 image_layout: Some(image.clone()),
1745 image,
1746 links: Vec::new(),
1747 };
1748 self.process_pages(vec![page], name)
1749 }
1750
1751 /// Run layout (+ OCR for cell-less pages) and assemble each already-rendered
1752 /// page (image / METS inputs, which are small and already materialised).
1753 fn process_pages(
1754 &mut self,
1755 mut pages: Vec<PdfPage>,
1756 name: &str,
1757 ) -> Result<DoclingDocument, PdfError> {
1758 let mut doc = DoclingDocument::new(name);
1759 let mut confs = std::collections::BTreeMap::new();
1760 let worker = self.primary()?;
1761 for (n, page) in pages.iter_mut().enumerate() {
1762 let (mut nodes, links, conf) = worker.process(n, page)?;
1763 assemble::stamp_page_no(&mut nodes, n + 1);
1764 doc.nodes.extend(nodes);
1765 doc.links.extend(links);
1766 confs.insert(n + 1, conf);
1767 }
1768 assemble::merge_continuations(&mut doc.nodes);
1769 doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1770 Ok(doc)
1771 }
1772}
1773
1774#[cfg(feature = "ml")]
1775/// Convenience one-shot conversion (loads the pipeline per call). Errors are
1776/// detailed and surfaced (never silently skipped).
1777pub fn convert(
1778 bytes: &[u8],
1779 password: Option<&str>,
1780 name: &str,
1781) -> Result<DoclingDocument, PdfError> {
1782 convert_with_options(
1783 bytes,
1784 password,
1785 name,
1786 false,
1787 false,
1788 false,
1789 false,
1790 EnrichmentOptions::default(),
1791 None,
1792 None,
1793 )
1794}
1795
1796#[cfg(feature = "ml")]
1797/// Like [`convert`], but optionally skips loading/running TableFormer (see
1798/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
1799/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes (see
1800/// [`Pipeline::enrichments`]).
1801// One positional per pipeline switch mirrors the Pipeline builder; growing
1802// past clippy's arity cap is the price of keeping this one-shot signature
1803// stable-ish instead of churning callers into an options struct mid-series.
1804#[allow(clippy::too_many_arguments)]
1805pub fn convert_with_options(
1806 bytes: &[u8],
1807 password: Option<&str>,
1808 name: &str,
1809 no_table_former: bool,
1810 no_ocr: bool,
1811 force_full_page_ocr: bool,
1812 no_text_panels: bool,
1813 enrich: EnrichmentOptions,
1814 pages: Option<(usize, usize)>,
1815 ocr_lang: Option<OcrLang>,
1816) -> Result<DoclingDocument, PdfError> {
1817 Pipeline::new()?
1818 .no_table_former(no_table_former)
1819 .no_ocr(no_ocr)
1820 .force_full_page_ocr(force_full_page_ocr)
1821 .no_text_panels(no_text_panels)
1822 .enrichments(enrich)
1823 .pages(pages)
1824 .ocr_lang(ocr_lang)
1825 .convert(bytes, password, name)
1826}
1827
1828#[cfg(feature = "ml")]
1829/// Convenience one-shot image conversion (loads the pipeline per call).
1830pub fn convert_image(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
1831 convert_image_with_options(
1832 bytes,
1833 name,
1834 false,
1835 false,
1836 false,
1837 EnrichmentOptions::default(),
1838 None,
1839 )
1840}
1841
1842#[cfg(feature = "ml")]
1843/// Like [`convert_image`], but optionally skips loading/running TableFormer (see
1844/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
1845/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
1846pub fn convert_image_with_options(
1847 bytes: &[u8],
1848 name: &str,
1849 no_table_former: bool,
1850 no_ocr: bool,
1851 no_text_panels: bool,
1852 enrich: EnrichmentOptions,
1853 ocr_lang: Option<OcrLang>,
1854) -> Result<DoclingDocument, PdfError> {
1855 Pipeline::new()?
1856 .no_table_former(no_table_former)
1857 .no_ocr(no_ocr)
1858 .no_text_panels(no_text_panels)
1859 .enrichments(enrich)
1860 .ocr_lang(ocr_lang)
1861 .convert_image(bytes, name)
1862}
1863
1864#[cfg(feature = "ml")]
1865/// Convert pre-segmented pages (image + already-known text cells, e.g. METS/hOCR
1866/// scans) through the shared layout + assembly pipeline.
1867pub fn convert_pages(pages: Vec<PdfPage>, name: &str) -> Result<DoclingDocument, PdfError> {
1868 convert_pages_with_options(
1869 pages,
1870 name,
1871 false,
1872 false,
1873 false,
1874 EnrichmentOptions::default(),
1875 )
1876}
1877
1878#[cfg(feature = "ml")]
1879/// Like [`convert_pages`], but optionally skips loading/running TableFormer (see
1880/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
1881/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
1882pub fn convert_pages_with_options(
1883 pages: Vec<PdfPage>,
1884 name: &str,
1885 no_table_former: bool,
1886 no_ocr: bool,
1887 no_text_panels: bool,
1888 enrich: EnrichmentOptions,
1889) -> Result<DoclingDocument, PdfError> {
1890 Pipeline::new()?
1891 .no_table_former(no_table_former)
1892 .no_text_panels(no_text_panels)
1893 .no_ocr(no_ocr)
1894 .enrichments(enrich)
1895 .process_pages(pages, name)
1896}
1897
1898#[cfg(feature = "ml")]
1899#[cfg(all(test, feature = "ml"))]
1900mod image_limit_tests {
1901 use super::decode_image_with_max_side;
1902
1903 /// A small valid PNG encoded via the `image` crate (robust vs. a hand-rolled
1904 /// byte literal).
1905 fn png_bytes(w: u32, h: u32) -> Vec<u8> {
1906 use std::io::Cursor;
1907 let img = image::RgbImage::new(w, h);
1908 let mut out = Vec::new();
1909 img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png)
1910 .unwrap();
1911 out
1912 }
1913
1914 #[test]
1915 fn normal_image_decodes_under_the_cap() {
1916 let img = decode_image_with_max_side(&png_bytes(8, 8), 30_000).expect("8x8 decodes");
1917 assert_eq!(img.dimensions(), (8, 8));
1918 }
1919
1920 #[test]
1921 fn dimensions_over_the_cap_are_rejected_not_aborted() {
1922 // A per-side cap below the image's declared size must yield a
1923 // recoverable Err, never an allocation-abort — the mechanism that stops
1924 // a crafted image declaring 60000×60000 from OOM-killing the process.
1925 let r = decode_image_with_max_side(&png_bytes(8, 8), 4);
1926 assert!(
1927 r.is_err(),
1928 "decode must fail under the pixel cap, not abort"
1929 );
1930 }
1931}
1932
1933#[cfg(test)]
1934mod median_tests {
1935 #[test]
1936 fn median_of_empty_is_zero_not_a_panic() {
1937 // A crafted table can leave a row/column with zero matched cells; the
1938 // even-count branch would index values[0 - 1] and panic (→ remote crash
1939 // via docling-serve) without the empty guard.
1940 assert_eq!(super::tf_match::median_for_test(&mut []), 0.0);
1941 assert_eq!(super::tf_match::median_for_test(&mut [4.0, 2.0]), 3.0);
1942 assert_eq!(super::tf_match::median_for_test(&mut [5.0, 1.0, 3.0]), 3.0);
1943 }
1944}
1945
1946#[cfg(test)]
1947mod send_check {
1948 /// The Node bindings (`docling-node`) run a shared [`super::Pipeline`] on
1949 /// libuv worker threads (`Arc<Mutex<Pipeline>>`), which is only sound while
1950 /// `Pipeline: Send` holds — this fails to compile if a non-`Send` field
1951 /// (e.g. an `Rc` or a raw pdfium handle) ever lands in the pipeline.
1952 fn assert_send<T: Send>() {}
1953
1954 #[test]
1955 fn pipeline_is_send() {
1956 assert_send::<super::Pipeline>();
1957 }
1958}