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    UnknownPageAnnotationVariableTextJustificationType,
72    PageObjectIndexOutOfBounds,
73    PageAnnotationIndexOutOfBounds,
74    OwnershipNotAttachedToDocument,
75    OwnershipNotAttachedToPage,
76    OwnershipAlreadyAttachedToDifferentPage,
77    OwnershipNotAttachedToAnnotation,
78    FormFieldOptionIndexOutOfBounds,
79    FormFieldAppearanceStreamUndefined,
80    PageFlattenFailure,
81    PageMissingEmbeddedThumbnail,
82    UnknownPdfPageObjectType,
83    UnknownPdfPageTextRenderMode,
84    UnknownPdfPagePathFillMode,
85    UnknownPdfAnnotationType,
86    UnknownPdfDestinationViewType,
87    UnknownPdfSecurityHandlerRevision,
88    UnknownPdfSignatureModificationDetectionPermissionLevel,
89    UnsupportedPdfPageObjectType,
90    TextSegmentIndexOutOfBounds,
91    TextSearchTargetIsEmpty,
92    CharIndexOutOfBounds,
93    NoCharsInPageObject,
94    NoCharsInAnnotation,
95    NoCharsInRect,
96    ImageObjectFilterIndexOutOfBounds,
97    ImageObjectFilterIndexInBoundsButFilterUndefined,
98    UnknownPdfColorSpace,
99    InvalidTransformationMatrix,
100    SignatureIndexOutOfBounds,
101    AttachmentIndexOutOfBounds,
102    NoDataInAttachment,
103    FontGlyphIndexOutOfBounds,
104    UnknownPathSegmentType,
105    NoPagesInDocument,
106    NoPageObjectsInCollection,
107    NoPageLinksInCollection,
108    NoAnnotationsInCollection,
109    PageObjectNotCopyable,
110    ImageObjectFiltersNotCopyable,
111    PathObjectBezierControlPointsNotCopyable,
112    PathObjectUnknownSegmentTypeNotCopyable,
113    GroupContainsNonCopyablePageObjects,
114    SourcePageIndexNotInCache,
115    NoUriForAction,
116    DestinationPageIndexNotAvailable,
117    DestinationPageLocationNotAvailable,
118    PageAnnotationAttachmentPointIndexOutOfBounds,
119    NoAttachmentPointsInPageAnnotation,
120    CoordinateConversionFunctionIndicatedError,
121
122    /// Pdfium does not safely support moving page object ownership from one document to another.
123    CannotMoveObjectAcrossDocuments,
124
125    /// Pdfium does not support adding or removing page objects from the page objects
126    /// collection inside a PdfPageXObjectFormObject object.
127    PageObjectsCollectionIsImmutable,
128
129    /// A call to `FPDFDest_GetView()` returned a valid `FPDFDEST_VIEW_*` value, but the number
130    /// of view parameters returned does not match the PDF specification.
131    PdfDestinationViewInvalidParameters,
132
133    /// A [ParseIntError] occurred while attempting to parse a `PdfColor` from a hexadecimal string
134    /// in `PdfColor::from_hex()`.
135    ParseHexadecimalColorError(ParseIntError),
136
137    /// The hexadecimal string given to `PdfColor::from_hex()` was not either exactly 7 or 9
138    /// characters long.
139    ParseHexadecimalColorUnexpectedLength,
140
141    /// The leading `#` character was not found while attempting to parse a `PdfColor` from
142    /// a hexadecimal string in `PdfColor::from_hex()`.
143    ParseHexadecimalColorMissingLeadingHash,
144
145    /// An error occurred converting a byte stream into a `CString`.
146    CStringConversionError(IntoStringError),
147
148    /// Two data buffers are expected to have the same size, but they do not.
149    DataBufferLengthMismatch,
150
151    /// The setting cannot be returned because this `PdfPageGroupObject` is empty.
152    EmptyPageObjectGroup,
153
154    /// A call to a internal Pdfium `FPDF_*` function returned a value indicating failure.
155    ///
156    /// For Pdfium functions that return enumerations, this means the function returned
157    /// a value of -1 rather than a valid enumeration constant.
158    ///
159    /// For Pdfium functions that return C-style boolean integers, this means that the function
160    /// returned a value other than `PdfiumLibraryBindings::TRUE`.
161    PdfiumFunctionReturnValueIndicatedFailure,
162
163    /// A call to a Pdfium function that returns a standard 8-bit color component value
164    /// (for example, `FPDFPageObj_GetStrokeColor()` and `FPDFPageObj_GetStrokeColor()`)
165    /// successfully returned a value, but the value could not be converted from a c_int
166    /// to a standard Rust u8.
167    UnableToConvertPdfiumColorValueToRustu8(std::num::TryFromIntError),
168
169    /// The browser's built-in `Window` object could not be retrieved.
170    WebSysWindowObjectNotAvailable,
171
172    #[cfg(target_arch = "wasm32")]
173    /// A JsValue returned from a function call was set to JsValue::UNDEFINED instead of
174    /// a valid value of the expected type.
175    JsValueUndefined,
176
177    #[cfg(target_arch = "wasm32")]
178    /// An error was returned when attempting to use the browser's built-in `fetch()` API.
179    WebSysFetchError(JsValue),
180
181    #[cfg(target_arch = "wasm32")]
182    /// An invalid Response object was returned when attempting to use the browser's built-in `fetch()` API.
183    WebSysInvalidResponseError,
184
185    #[cfg(target_arch = "wasm32")]
186    /// An error was returned when attempting to construct a `Blob` object from a byte buffer.
187    JsSysErrorConstructingBlobFromBytes,
188
189    #[cfg(target_arch = "wasm32")]
190    /// An error occurred when attempting to retrieve the function table for the compiled
191    /// Pdfium WASM module.
192    JsSysErrorRetrievingFunctionTable(JsValue),
193
194    #[cfg(target_arch = "wasm32")]
195    /// An error occurred when attempting to retrieve an exported function from
196    /// `pdfium-render`'s WASM module.
197    JsSysErrorRetrievingFunction(JsValue),
198
199    #[cfg(target_arch = "wasm32")]
200    /// An error occurred when attempting to update an entry in Pdfium's WASM function table.
201    JsSysErrorPatchingFunctionTable(JsValue),
202
203    #[cfg(target_arch = "wasm32")]
204    /// No previously cached function was available for a WASM function table restore operation.
205    ///
206    /// This error should never occur; if it does, it indicates a programming error in pdfium-render.
207    /// Please file an issue: https://github.com/ajrcarey/pdfium-render/issues
208    NoPreviouslyCachedFunctionSet,
209
210    /// An error occurred during an image processing operation.
211    ImageError,
212
213    /// Dimensions of `Image::Image` are specified in `u32`, but bitmaps in Pdfium are sized in
214    /// `c_int` (`i32`), meaning that an `Image::Image` can have dimensions that overflow
215    /// the maximum size of a Pdfium bitmap. As a compromise, Image dimensions in `pdfium-render`
216    /// are limited to `u16`.
217    ///
218    /// This error indicates that an `Image::Image` had a width or height larger than the maximum
219    /// `u16` size allowed by `pdfium-render`.
220    ImageSizeOutOfBounds,
221
222    /// An I/O error occurred during a Pdfium file operation.
223    IoError(std::io::Error),
224
225    /// A wrapped internal library error from Pdfium's `FPDF_ERR_*` constant values.
226    PdfiumLibraryInternalError(PdfiumInternalError),
227}
228
229impl Display for PdfiumError {
230    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
231        write!(f, "{self:#?}")
232    }
233}
234
235impl Error for PdfiumError {}