Skip to main content

docling/
lib.rs

1//! docling.rs: a Rust port of [docling](https://github.com/docling-project/docling).
2//!
3//! The public surface mirrors the Python SDK, kept deliberately small:
4//!
5//! ```no_run
6//! use docling::{DocumentConverter, SourceDocument};
7//!
8//! let converter = DocumentConverter::new();
9//! let result = converter
10//!     .convert(SourceDocument::from_file("input.md").unwrap())
11//!     .unwrap();
12//! println!("{}", result.document.export_to_markdown());
13//! ```
14//!
15//! For the PDF/image ML pipeline (pdfium + layout/TableFormer/OCR ONNX), reuse a
16//! [`Pipeline`] across documents to amortize model loading, instead of the
17//! per-call [`DocumentConverter`]. Deploying as a service: `examples/Dockerfile`
18//! is a 3-stage build that bakes the binary, native libs, and exported models
19//! (including the KV-cached TableFormer decoder) into a slim, Python-free runtime
20//! image — see the "Deploy in a container" section of the README.
21//!
22//! See `docs/MIGRATION.md` for the architecture, format-by-format parity
23//! status, and how conformance against Python docling is measured.
24
25pub mod archive;
26pub mod chunks;
27mod converter;
28pub mod dclx;
29pub mod email_attachments;
30mod error;
31pub mod fetch;
32mod format;
33// Allocator housekeeping for processes that convert many documents (#669).
34pub mod memory;
35mod metadata;
36pub mod options;
37pub mod plugins;
38mod redact;
39mod result;
40mod sniff;
41mod source;
42#[cfg(feature = "pdf")]
43mod stream;
44#[cfg(feature = "vlm")]
45pub mod vlm;
46
47pub mod backend;
48#[cfg(feature = "asr")]
49pub mod video;
50
51pub use archive::{ArchiveLimits, ArchiveOutcome};
52pub use backend::images::{ImageLimits, ImagePolicy, ImageSources, DEFAULT_MAX_IMAGE_BYTES};
53pub use backend::ipynb::{IpynbBackend, IpynbOutputs};
54#[cfg(feature = "pdf")]
55pub use converter::WarmPipeline;
56pub use converter::{parse_page_range, DocumentConverter, ALL_VIDEO_FRAMES, DEFAULT_VIDEO_FRAMES};
57pub use docling_core::{
58    CustomPattern, ImageRedaction, PiiKind, RedactionOptions, RedactionReport, Replacement,
59};
60pub use email_attachments::{EmailAttachmentInfo, EmailAttachments};
61pub use error::{ConversionError, EncryptionError};
62pub use fetch::FetchPolicy;
63pub use format::{InputFormat, OUTPUT_FORMATS};
64pub use options::{
65    cli_flag, merge_options, ConvertOptions, OptionInfo, OptionsError, PipelineKind, OPTIONS,
66};
67pub use plugins::{BackendMatcher, RegisteredBackend};
68pub use result::{ConversionResult, ConversionStatus, ErrorItem};
69pub use source::SourceDocument;
70#[cfg(feature = "pdf")]
71pub use stream::MarkdownStream;
72
73// Re-export the core model so callers only need the one crate, and so
74// `result.document.export_to_markdown()` works without an extra import.
75pub use docling_core::base64;
76pub use docling_core::chunker;
77pub use docling_core::pandoc;
78pub use docling_core::{
79    ConfidenceReport, ContentLayer, ContentLayers, DocItemLabel, DoclingDocument,
80    HtmlExportOptions, ImageMode, MarkdownExportOptions, MarkdownStreamer, Node, PictureImage,
81    QualityGrade, Table,
82};
83pub use docling_core::{DocumentMetadata, GpsPosition, MetadataOutput};
84
85// The reusable PDF/image pipeline (models loaded once, reused across documents),
86// for callers that convert many files or want a warm, startup-excluded measurement.
87#[cfg(feature = "pdf")]
88pub use docling_pdf::{
89    model_inventory, page_count as pdf_page_count, render_pages as render_pdf_pages,
90    tesseract_lang_arg, EnrichmentOptions, HeadingHierarchyOptions, ImageOutput, ModelEntry,
91    OcrEngine, OcrLang, OcrMode, PdfError, Pipeline, RenderedPage,
92};
93// The pure-Rust text-layer extraction (no pdfium, no models) — compiled with
94// either PDF feature. The CLI uses it as the `--text-layer-only` fallback when the
95// runtime assets are missing (launch blocker: a bare `cargo install` ships
96// neither pdfium nor the models).
97#[cfg(any(feature = "pdf", feature = "pdf-text"))]
98pub use docling_pdf::convert_text_layer_pages as pdf_text_layer_pages;
99
100/// Re-execute under an absolute `argv[0]` so ONNX Runtime finds its GPU
101/// provider libraries next to the binary (#664) — see
102/// `docling_onnx::reexec_for_provider_libraries`. Binaries call it first
103/// thing in `main`; a no-op unless a CUDA/TensorRT build was started by bare
104/// name on Linux.
105pub fn reexec_for_provider_libraries() {
106    #[cfg(feature = "pdf")]
107    docling_pdf::ep::reexec_for_provider_libraries();
108}
109
110/// The ONNX Runtime the ML stages run on, as `(major, minor)` — the
111/// statically linked one (1.31 since #523), or the library an
112/// `ort-load-dynamic` build loaded. `None` without the `pdf` feature or
113/// when the runtime cannot be asked.
114pub fn onnx_runtime_version() -> Option<(u32, u32)> {
115    #[cfg(feature = "pdf")]
116    {
117        docling_pdf::ep::runtime_version()
118    }
119    #[cfg(not(feature = "pdf"))]
120    {
121        None
122    }
123}
124
125/// Which PDF conversion this build compiled in: the full ML pipeline (`pdf`
126/// feature), the pure-Rust text-layer path (`pdf-text`, the wasm32 build), or
127/// neither. Compile-time facts, exported so downstream crates (whose own
128/// features can't see this crate's) can branch — e.g. docling-wasm's host
129/// tests, where workspace feature unification may pull `pdf` in.
130pub const PDF_ML_COMPILED: bool = cfg!(feature = "pdf");
131/// True when the `pdf-text` text-layer-only PDF path is compiled in.
132pub const PDF_TEXT_COMPILED: bool = cfg!(feature = "pdf-text");
133
134/// Stand-in for `docling_pdf::EnrichmentOptions` when the `pdf` feature is
135/// off: the `DocumentConverter` builder methods keep compiling (and stay
136/// inert — the formats these flags affect are rejected at convert time).
137#[cfg(not(feature = "pdf"))]
138#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
139pub struct EnrichmentOptions {
140    /// Classify each picture with DocumentFigureClassifier (26 classes).
141    pub picture_classification: bool,
142    /// Rewrite code blocks (and detect their language) with CodeFormulaV2.
143    pub code: bool,
144    /// Decode display formulas to LaTeX with CodeFormulaV2.
145    pub formula: bool,
146}