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 /// A call to `FPDFDest_GetView()` returned a valid `FPDFDEST_VIEW_*` value, but the number
121 /// of view parameters returned does not match the PDF specification.
122 PdfDestinationViewInvalidParameters,
123
124 /// A [ParseIntError] occurred while attempting to parse a `PdfColor` from a hexadecimal string
125 /// in `PdfColor::from_hex()`.
126 ParseHexadecimalColorError(ParseIntError),
127
128 /// The hexadecimal string given to `PdfColor::from_hex()` was not either exactly 7 or 9
129 /// characters long.
130 ParseHexadecimalColorUnexpectedLength,
131
132 /// The leading `#` character was not found while attempting to parse a `PdfColor` from
133 /// a hexadecimal string in `PdfColor::from_hex()`.
134 ParseHexadecimalColorMissingLeadingHash,
135
136 /// An error occurred converting a byte stream into a `CString`.
137 CStringConversionError(IntoStringError),
138
139 /// Two data buffers are expected to have the same size, but they do not.
140 DataBufferLengthMismatch,
141
142 /// The setting cannot be returned because this `PdfPageGroupObject` is empty.
143 EmptyPageObjectGroup,
144
145 /// A call to a internal Pdfium `FPDF_*` function returned a value indicating failure.
146 ///
147 /// For Pdfium functions that return enumerations, this means the function returned
148 /// a value of -1 rather than a valid enumeration constant.
149 ///
150 /// For Pdfium functions that return C-style boolean integers, this means that the function
151 /// returned a value other than `PdfiumLibraryBindings::TRUE`.
152 PdfiumFunctionReturnValueIndicatedFailure,
153
154 /// A call to a Pdfium function that returns a standard 8-bit color component value
155 /// (for example, `FPDFPageObj_GetStrokeColor()` and `FPDFPageObj_GetStrokeColor()`)
156 /// successfully returned a value, but the value could not be converted from a c_int
157 /// to a standard Rust u8.
158 UnableToConvertPdfiumColorValueToRustu8(std::num::TryFromIntError),
159
160 /// The browser's built-in `Window` object could not be retrieved.
161 WebSysWindowObjectNotAvailable,
162
163 #[cfg(target_arch = "wasm32")]
164 /// A JsValue returned from a function call was set to JsValue::UNDEFINED instead of
165 /// a valid value of the expected type.
166 JsValueUndefined,
167
168 #[cfg(target_arch = "wasm32")]
169 /// An error was returned when attempting to use the browser's built-in `fetch()` API.
170 WebSysFetchError(JsValue),
171
172 #[cfg(target_arch = "wasm32")]
173 /// An invalid Response object was returned when attempting to use the browser's built-in `fetch()` API.
174 WebSysInvalidResponseError,
175
176 #[cfg(target_arch = "wasm32")]
177 /// An error was returned when attempting to construct a `Blob` object from a byte buffer.
178 JsSysErrorConstructingBlobFromBytes,
179
180 #[cfg(target_arch = "wasm32")]
181 /// An error occurred when attempting to retrieve the function table for the compiled
182 /// Pdfium WASM module.
183 JsSysErrorRetrievingFunctionTable(JsValue),
184
185 #[cfg(target_arch = "wasm32")]
186 /// An error occurred when attempting to retrieve an exported function from
187 /// `pdfium-render`'s WASM module.
188 JsSysErrorRetrievingFunction(JsValue),
189
190 #[cfg(target_arch = "wasm32")]
191 /// An error occurred when attempting to update an entry in Pdfium's WASM function table.
192 JsSysErrorPatchingFunctionTable(JsValue),
193
194 #[cfg(target_arch = "wasm32")]
195 /// No previously cached function was available for a WASM function table restore operation.
196 ///
197 /// This error should never occur; if it does, it indicates a programming error in pdfium-render.
198 /// Please file an issue: https://github.com/ajrcarey/pdfium-render/issues
199 NoPreviouslyCachedFunctionSet,
200
201 /// An error occurred during an image processing operation.
202 ImageError,
203
204 /// Dimensions of `Image::Image` are specified in `u32`, but bitmaps in Pdfium are sized in
205 /// `c_int` (`i32`), meaning that an `Image::Image` can have dimensions that overflow
206 /// the maximum size of a Pdfium bitmap. As a compromise, Image dimensions in `pdfium-render`
207 /// are limited to `u16`.
208 ///
209 /// This error indicates that an `Image::Image` had a width or height larger than the maximum
210 /// `u16` size allowed by `pdfium-render`.
211 ImageSizeOutOfBounds,
212
213 /// An I/O error occurred during a Pdfium file operation.
214 IoError(std::io::Error),
215
216 /// A wrapped internal library error from Pdfium's `FPDF_ERR_*` constant values.
217 PdfiumLibraryInternalError(PdfiumInternalError),
218}
219
220impl Display for PdfiumError {
221 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
222 write!(f, "{:#?}", self)
223 }
224}
225
226impl Error for PdfiumError {}