Skip to main content

pdf_ocr/
error.rs

1//! Error types for `pdf-ocr` operations.
2
3// Copyright (c) 2026 Innovation Trigger B.V.
4//
5// PDFluent is available under two licences, at your option: the GNU AGPLv3, or
6// the PDFluent Commercial Licence. See the LICENSE file in this repository --
7// that file travels with the copy you received, which a URL does not.
8
9use thiserror::Error;
10
11/// Errors returned by the `pdf-ocr` crate during optical character
12/// recognition.
13///
14/// Wrapping engines (Tesseract, PaddleOCR) bubble their own errors up via
15/// the [`OcrError::Engine`] variant — this crate does not classify
16/// engine-specific failures.
17#[derive(Debug, Error)]
18pub enum OcrError {
19    /// The underlying PDF byte stream could not be parsed before OCR could
20    /// be attempted (malformed cross-reference table, truncated stream,
21    /// encrypted document opened without a password, etc.).
22    #[error("PDF error: {0}")]
23    Pdf(#[from] lopdf::Error),
24
25    /// An I/O error occurred while reading the source PDF or writing OCR
26    /// output (e.g. searchable PDF or hOCR sidecar).
27    #[error("IO error: {0}")]
28    Io(#[from] std::io::Error),
29
30    /// The caller asked to OCR a page index that does not exist in the
31    /// document.
32    ///
33    /// Fields: `(requested_page, total_pages)`. Page numbers are 1-based.
34    #[error("page {0} out of range (document has {1} pages)")]
35    PageOutOfRange(u32, u32),
36
37    /// The configured OCR engine returned a non-recoverable error
38    /// (Tesseract initialisation failure, missing language data, model
39    /// load failure, etc.). The wrapped string is the engine's verbatim
40    /// message — surface it to the user.
41    #[error("OCR engine error: {0}")]
42    Engine(String),
43
44    /// Rasterising the page for OCR failed before the engine could see
45    /// any pixels (font resolution failure, unsupported colorspace,
46    /// page geometry error, etc.).
47    #[error("render error: {0}")]
48    Render(String),
49
50    /// A non-categorised OCR failure. Reserved for cases the more specific
51    /// variants do not cover; the message describes the situation.
52    #[error("{0}")]
53    Other(String),
54}
55
56/// Convenience `Result` alias for fallible `pdf-ocr` operations.
57pub type Result<T> = std::result::Result<T, OcrError>;