pdfium_render/
error.rs

1//! Defines the [PdfiumError] enum, used to wrap Pdfium errors as `Err` values.
2
3use crate::bindgen::{
4    FPDF_ERR_FILE, FPDF_ERR_FORMAT, FPDF_ERR_PAGE, FPDF_ERR_PASSWORD, FPDF_ERR_SECURITY,
5    FPDF_ERR_UNKNOWN,
6};
7use std::error::Error;
8use std::ffi::IntoStringError;
9use std::fmt::{Display, Formatter, Result};
10use std::num::ParseIntError;
11
12#[cfg(target_arch = "wasm32")]
13use wasm_bindgen::JsValue;
14
15/// A wrapped internal library error from Pdfium's `FPDF_ERR_*` constant values.
16///
17/// Pdfium only provides detailed internal error information for document loading functions.
18/// All other functions in the Pdfium API return a value indicating success or failure,
19/// but otherwise detailed error information for failed API calls is not available. In these
20/// cases, an error value of [PdfiumInternalError::Unknown] will be returned.
21// For more information, see: https://github.com/ajrcarey/pdfium-render/issues/78
22#[derive(Debug)]
23pub enum PdfiumInternalError {
24    /// The document could not be loaded due to a file system error.
25    FileError = FPDF_ERR_FILE as isize,
26
27    /// The document could not be loaded due to a format parsing error.
28    FormatError = FPDF_ERR_FORMAT as isize,
29
30    /// The document could not be loaded because the wrong password was supplied.
31    PasswordError = FPDF_ERR_PASSWORD as isize,
32
33    /// The document could not be loaded because of the document's security settings.
34    SecurityError = FPDF_ERR_SECURITY as isize,
35
36    /// The page could not be loaded due to an internal error.
37    PageError = FPDF_ERR_PAGE as isize,
38
39    /// A generic error value returned in all other unhandled situations.
40    Unknown = FPDF_ERR_UNKNOWN as isize,
41}
42
43/// A wrapper enum for handling Pdfium errors as standard Rust `Err` values.
44#[derive(Debug)]
45pub enum PdfiumError {
46    /// The Pdfium WASM module has not been configured.
47    /// It is essential that the exported `initialize_pdfium_render()` function be called
48    /// from Javascript _before_ calling any `pdfium-render` function from within your Rust code.
49    /// See: <https://github.com/ajrcarey/pdfium-render/blob/master/examples/index.html>
50    #[cfg(target_arch = "wasm32")]
51    PdfiumWASMModuleNotConfigured,
52
53    /// An error occurred during dynamic binding to an external Pdfium library.
54    #[cfg(not(target_arch = "wasm32"))]
55    LoadLibraryError(libloading::Error),
56
57    /// An error occurred during dynamic binding while converting an FPDF_* function name
58    /// to a C string. The wrapped string value contains more information.
59    #[cfg(not(target_arch = "wasm32"))]
60    LoadLibraryFunctionNameError(String),
61
62    UnrecognizedPath,
63    PageIndexOutOfBounds,
64    LinkIndexOutOfBounds,
65    UnknownBitmapFormat,
66    UnknownBitmapRotation,
67    UnknownFormType,
68    UnknownFormFieldType,
69    UnknownActionType,
70    UnknownAppearanceMode,
71    PageObjectIndexOutOfBounds,
72    PageAnnotationIndexOutOfBounds,
73    OwnershipNotAttachedToDocument,
74    OwnershipNotAttachedToPage,
75    OwnershipAlreadyAttachedToDifferentPage,
76    OwnershipNotAttachedToAnnotation,
77    FormFieldOptionIndexOutOfBounds,
78    FormFieldAppearanceStreamUndefined,
79    PageFlattenFailure,
80    PageMissingEmbeddedThumbnail,
81    UnknownPdfPageObjectType,
82    UnknownPdfPageTextRenderMode,
83    UnknownPdfPagePathFillMode,
84    UnknownPdfAnnotationType,
85    UnknownPdfDestinationViewType,
86    UnknownPdfSecurityHandlerRevision,
87    UnknownPdfSignatureModificationDetectionPermissionLevel,
88    UnsupportedPdfPageObjectType,
89    TextSegmentIndexOutOfBounds,
90    CharIndexOutOfBounds,
91    NoCharsInPageObject,
92    NoCharsInAnnotation,
93    NoCharsInRect,
94    ImageObjectFilterIndexOutOfBounds,
95    ImageObjectFilterIndexInBoundsButFilterUndefined,
96    UnknownPdfColorSpace,
97    InvalidTransformationMatrix,
98    SignatureIndexOutOfBounds,
99    AttachmentIndexOutOfBounds,
100    NoDataInAttachment,
101    FontGlyphIndexOutOfBounds,
102    UnknownPathSegmentType,
103    NoPagesInDocument,
104    NoPageObjectsInCollection,
105    NoPageLinksInCollection,
106    NoAnnotationsInCollection,
107    PageObjectNotCopyable,
108    ImageObjectFiltersNotCopyable,
109    PathObjectBezierControlPointsNotCopyable,
110    PathObjectUnknownSegmentTypeNotCopyable,
111    GroupContainsNonCopyablePageObjects,
112    SourcePageIndexNotInCache,
113    NoUriForAction,
114    DestinationPageIndexNotAvailable,
115    DestinationPageLocationNotAvailable,
116    PageAnnotationAttachmentPointIndexOutOfBounds,
117    NoAttachmentPointsInPageAnnotation,
118    CoordinateConversionFunctionIndicatedError,
119
120    /// Pdfium does not support adding or removing page objects from the page objects
121    // collection inside a PdfPageXObjectFormObject object.
122    PageObjectsCollectionIsImmutable,
123
124    /// A call to `FPDFDest_GetView()` returned a valid `FPDFDEST_VIEW_*` value, but the number
125    /// of view parameters returned does not match the PDF specification.
126    PdfDestinationViewInvalidParameters,
127
128    /// A [ParseIntError] occurred while attempting to parse a `PdfColor` from a hexadecimal string
129    /// in `PdfColor::from_hex()`.
130    ParseHexadecimalColorError(ParseIntError),
131
132    /// The hexadecimal string given to `PdfColor::from_hex()` was not either exactly 7 or 9
133    /// characters long.
134    ParseHexadecimalColorUnexpectedLength,
135
136    /// The leading `#` character was not found while attempting to parse a `PdfColor` from
137    /// a hexadecimal string in `PdfColor::from_hex()`.
138    ParseHexadecimalColorMissingLeadingHash,
139
140    /// An error occurred converting a byte stream into a `CString`.
141    CStringConversionError(IntoStringError),
142
143    /// Two data buffers are expected to have the same size, but they do not.
144    DataBufferLengthMismatch,
145
146    /// The setting cannot be returned because this `PdfPageGroupObject` is empty.
147    EmptyPageObjectGroup,
148
149    /// A call to a internal Pdfium `FPDF_*` function returned a value indicating failure.
150    ///
151    /// For Pdfium functions that return enumerations, this means the function returned
152    /// a value of -1 rather than a valid enumeration constant.
153    ///
154    /// For Pdfium functions that return C-style boolean integers, this means that the function
155    /// returned a value other than `PdfiumLibraryBindings::TRUE`.
156    PdfiumFunctionReturnValueIndicatedFailure,
157
158    /// A call to a Pdfium function that returns a standard 8-bit color component value
159    /// (for example, `FPDFPageObj_GetStrokeColor()` and `FPDFPageObj_GetStrokeColor()`)
160    /// successfully returned a value, but the value could not be converted from a c_int
161    /// to a standard Rust u8.
162    UnableToConvertPdfiumColorValueToRustu8(std::num::TryFromIntError),
163
164    /// The browser's built-in `Window` object could not be retrieved.
165    WebSysWindowObjectNotAvailable,
166
167    #[cfg(target_arch = "wasm32")]
168    /// A JsValue returned from a function call was set to JsValue::UNDEFINED instead of
169    /// a valid value of the expected type.
170    JsValueUndefined,
171
172    #[cfg(target_arch = "wasm32")]
173    /// An error was returned when attempting to use the browser's built-in `fetch()` API.
174    WebSysFetchError(JsValue),
175
176    #[cfg(target_arch = "wasm32")]
177    /// An invalid Response object was returned when attempting to use the browser's built-in `fetch()` API.
178    WebSysInvalidResponseError,
179
180    #[cfg(target_arch = "wasm32")]
181    /// An error was returned when attempting to construct a `Blob` object from a byte buffer.
182    JsSysErrorConstructingBlobFromBytes,
183
184    #[cfg(target_arch = "wasm32")]
185    /// An error occurred when attempting to retrieve the function table for the compiled
186    /// Pdfium WASM module.
187    JsSysErrorRetrievingFunctionTable(JsValue),
188
189    #[cfg(target_arch = "wasm32")]
190    /// An error occurred when attempting to retrieve an exported function from
191    /// `pdfium-render`'s WASM module.
192    JsSysErrorRetrievingFunction(JsValue),
193
194    #[cfg(target_arch = "wasm32")]
195    /// An error occurred when attempting to update an entry in Pdfium's WASM function table.
196    JsSysErrorPatchingFunctionTable(JsValue),
197
198    #[cfg(target_arch = "wasm32")]
199    /// No previously cached function was available for a WASM function table restore operation.
200    ///
201    /// This error should never occur; if it does, it indicates a programming error in pdfium-render.
202    /// Please file an issue: https://github.com/ajrcarey/pdfium-render/issues
203    NoPreviouslyCachedFunctionSet,
204
205    /// An error occurred during an image processing operation.
206    ImageError,
207
208    /// Dimensions of `Image::Image` are specified in `u32`, but bitmaps in Pdfium are sized in
209    /// `c_int` (`i32`), meaning that an `Image::Image` can have dimensions that overflow
210    /// the maximum size of a Pdfium bitmap. As a compromise, Image dimensions in `pdfium-render`
211    /// are limited to `u16`.
212    ///
213    /// This error indicates that an `Image::Image` had a width or height larger than the maximum
214    /// `u16` size allowed by `pdfium-render`.
215    ImageSizeOutOfBounds,
216
217    /// An I/O error occurred during a Pdfium file operation.
218    IoError(std::io::Error),
219
220    /// A wrapped internal library error from Pdfium's `FPDF_ERR_*` constant values.
221    PdfiumLibraryInternalError(PdfiumInternalError),
222}
223
224impl Display for PdfiumError {
225    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
226        write!(f, "{:#?}", self)
227    }
228}
229
230impl Error for PdfiumError {}