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