1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Defines the [Pdfium] struct, a high-level idiomatic Rust wrapper around Pdfium.
use crate::bindings::PdfiumLibraryBindings;
use crate::document::{PdfDocument, PdfDocumentVersion};
use crate::error::{PdfiumError, PdfiumInternalError};
#[cfg(all(not(target_arch = "wasm32"), not(feature = "static")))]
use std::ffi::OsString;
#[cfg(all(not(target_arch = "wasm32"), not(feature = "static")))]
use libloading::Library;
#[cfg(all(not(target_arch = "wasm32"), not(feature = "static")))]
use crate::native::DynamicPdfiumBindings;
#[cfg(all(not(target_arch = "wasm32"), feature = "static"))]
use crate::linked::StaticPdfiumBindings;
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::files::get_pdfium_file_accessor_from_reader;
#[cfg(not(target_arch = "wasm32"))]
use std::fs::File;
#[cfg(not(target_arch = "wasm32"))]
use std::io::{Read, Seek};
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
#[cfg(target_arch = "wasm32")]
use crate::wasm::{PdfiumRenderWasmState, WasmPdfiumBindings};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsCast;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::JsFuture;
#[cfg(target_arch = "wasm32")]
use js_sys::{ArrayBuffer, Uint8Array};
#[cfg(target_arch = "wasm32")]
use web_sys::{window, Blob, Response};
#[cfg(feature = "thread_safe")]
use crate::thread_safe::ThreadSafePdfiumBindings;
// The following dummy declaration is used only when running cargo doc.
// It allows documentation of WASM-specific functionality to be included
// in documentation generated on non-WASM targets.
#[cfg(doc)]
struct Blob;
/// A high-level idiomatic Rust wrapper around Pdfium, the C++ PDF library used by
/// the Google Chromium project.
pub struct Pdfium {
bindings: Box<dyn PdfiumLibraryBindings>,
}
impl Pdfium {
/// Binds to a Pdfium library that was statically linked into the currently running
/// executable, returning a new [PdfiumLibraryBindings] object that contains bindings to the
/// functions exposed by the library. The application will immediately crash if Pdfium
/// was not correctly statically linked into the executable at compile time.
///
/// This function is only available when this crate's `static` feature is enabled.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(doc, feature = "static"))]
#[inline]
pub fn bind_to_statically_linked_library() -> Result<Box<dyn PdfiumLibraryBindings>, PdfiumError>
{
let bindings = StaticPdfiumBindings::new();
#[cfg(feature = "thread_safe")]
let bindings = ThreadSafePdfiumBindings::new(bindings);
Ok(Box::new(bindings))
}
/// Initializes the external Pdfium library, loading it from the system libraries.
/// Returns a new [PdfiumLibraryBindings] object that contains bindings to the functions exposed
/// by the library, or an error if the library could not be loaded.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(feature = "static"))]
#[inline]
pub fn bind_to_system_library() -> Result<Box<dyn PdfiumLibraryBindings>, PdfiumError> {
let bindings = DynamicPdfiumBindings::new(
unsafe { Library::new(Self::pdfium_platform_library_name()) }
.map_err(PdfiumError::LoadLibraryError)?,
)
.map_err(PdfiumError::LoadLibraryError)?;
#[cfg(feature = "thread_safe")]
let bindings = ThreadSafePdfiumBindings::new(bindings);
Ok(Box::new(bindings))
}
/// Initializes the external Pdfium library, binding to an external WASM module.
/// Returns a new [PdfiumLibraryBindings] object that contains bindings to the functions exposed
/// by the library, or an error if the library is not available.
///
/// It is essential that the exported `initialize_pdfium_render()` function be called
/// from Javascript _before_ calling this function from within your Rust code. For an example, see:
/// <https://github.com/ajrcarey/pdfium-render/blob/master/examples/index.html>
#[cfg(target_arch = "wasm32")]
#[inline]
pub fn bind_to_system_library() -> Result<Box<dyn PdfiumLibraryBindings>, PdfiumError> {
if PdfiumRenderWasmState::lock().is_ready() {
let bindings = WasmPdfiumBindings::new();
#[cfg(feature = "thread_safe")]
let bindings = ThreadSafePdfiumBindings::new(bindings);
Ok(Box::new(bindings))
} else {
Err(PdfiumError::PdfiumWASMModuleNotConfigured)
}
}
/// Initializes the external pdfium library, loading it from the given path.
/// Returns a new [PdfiumLibraryBindings] object that contains bindings to the functions
/// exposed by the library, or an error if the library could not be loaded.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(feature = "static"))]
#[inline]
pub fn bind_to_library(
path: impl ToString,
) -> Result<Box<dyn PdfiumLibraryBindings>, PdfiumError> {
let bindings = DynamicPdfiumBindings::new(
unsafe { Library::new(OsString::from(path.to_string())) }
.map_err(PdfiumError::LoadLibraryError)?,
)
.map_err(PdfiumError::LoadLibraryError)?;
#[cfg(feature = "thread_safe")]
let bindings = ThreadSafePdfiumBindings::new(bindings);
Ok(Box::new(bindings))
}
/// Returns the name of the external Pdfium library on the currently running platform.
/// On Linux and Android, this will be `libpdfium.so` or similar; on Windows, this will
/// be `pdfium.dll` or similar; on MacOS, this will be `libpdfium.dylib` or similar.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(feature = "static"))]
#[inline]
pub fn pdfium_platform_library_name() -> OsString {
libloading::library_filename("pdfium")
}
/// Returns the name of the external Pdfium library on the currently running platform,
/// prefixed with the given path string.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(feature = "static"))]
#[inline]
pub fn pdfium_platform_library_name_at_path(path: impl ToString) -> String {
let mut path = path.to_string();
path.push_str(Pdfium::pdfium_platform_library_name().to_str().unwrap());
path
}
/// Creates a new [Pdfium] instance from the given external Pdfium library bindings.
#[inline]
pub fn new(bindings: Box<dyn PdfiumLibraryBindings>) -> Self {
bindings.FPDF_InitLibrary();
Self { bindings }
}
// TODO: AJRC - 17/9/22 - remove deprecated Pdfium::get_bindings() function in 0.9.0
// as part of tracking issue https://github.com/ajrcarey/pdfium-render/issues/36
/// Returns the [PdfiumLibraryBindings] wrapped by this instance of [Pdfium].
#[deprecated(
since = "0.7.18",
note = "This function has been renamed. Use the Pdfium::bindings() function instead."
)]
#[doc(hidden)]
#[inline]
pub fn get_bindings(&self) -> &dyn PdfiumLibraryBindings {
self.bindings.as_ref()
}
/// Returns the [PdfiumLibraryBindings] wrapped by this instance of [Pdfium].
#[inline]
pub fn bindings(&self) -> &dyn PdfiumLibraryBindings {
self.bindings.as_ref()
}
// TODO: AJRC - 18/12/22 - remove deprecated Pdfium::load_pdf_from_bytes() function in 0.9.0
// as part of tracking issue https://github.com/ajrcarey/pdfium-render/issues/36
/// Returns the [PdfiumLibraryBindings] wrapped by this instance of [Pdfium].
#[deprecated(
since = "0.7.26",
note = "This function has been renamed. Use the Pdfium::load_pdf_from_byte_slice() function instead."
)]
#[doc(hidden)]
#[inline]
pub fn load_pdf_from_bytes(
&self,
bytes: &'static [u8],
password: Option<&str>,
) -> Result<PdfDocument, PdfiumError> {
self.load_pdf_from_byte_slice(bytes, password)
}
/// Attempts to open a [PdfDocument] from the given static byte buffer.
///
/// If the document is password protected, the given password will be used to unlock it.
pub fn load_pdf_from_byte_slice(
&self,
bytes: &'static [u8],
password: Option<&str>,
) -> Result<PdfDocument, PdfiumError> {
Self::pdfium_document_handle_to_result(
self.bindings.FPDF_LoadMemDocument64(bytes, password),
self.bindings(),
)
}
/// Attempts to open a [PdfDocument] from the given owned byte buffer.
///
/// If the document is password protected, the given password will be used to unlock it.
///
/// `pdfium-render` will take ownership of the given byte buffer, ensuring its lifetime lasts
/// as long as the [PdfDocument] opened from it.
pub fn load_pdf_from_byte_vec(
&self,
bytes: Vec<u8>,
password: Option<&str>,
) -> Result<PdfDocument, PdfiumError> {
Self::pdfium_document_handle_to_result(
self.bindings
.FPDF_LoadMemDocument64(bytes.as_slice(), password),
self.bindings(),
)
.map(|mut document| {
// Give the newly-created document ownership of the byte buffer, so that Pdfium can continue
// to read from it on an as-needed basis throughout the lifetime of the document.
document.set_source_byte_buffer(bytes);
document
})
}
/// Attempts to open a [PdfDocument] from the given file path.
///
/// If the document is password protected, the given password will be used
/// to unlock it.
///
/// This function is not available when compiling to WASM. You have several options for
/// loading your PDF document data in WASM:
/// * Use the [Pdfium::load_pdf_from_fetch()] function to download document data from a
/// URL using the browser's built-in `fetch()` API. This function is only available when
/// compiling to WASM.
/// * Use the [Pdfium::load_pdf_from_blob()] function to load document data from a
/// Javascript `File` or `Blob` object (such as a `File` object returned from an HTML
/// `<input type="file">` element). This function is only available when compiling to WASM.
/// * Use another method to retrieve the bytes of the target document over the network,
/// then load those bytes into Pdfium using either the [Pdfium::load_pdf_from_byte_slice()]
/// function or the [Pdfium::load_pdf_from_byte_vec()] function.
/// * Embed the bytes of the target document directly into the compiled WASM module
/// using the `include_bytes!()` macro.
#[cfg(not(target_arch = "wasm32"))]
pub fn load_pdf_from_file(
&self,
path: &(impl AsRef<Path> + ?Sized),
password: Option<&str>,
) -> Result<PdfDocument, PdfiumError> {
self.load_pdf_from_reader(File::open(path).map_err(PdfiumError::IoError)?, password)
}
/// Attempts to open a [PdfDocument] from the given reader.
///
/// Pdfium will only load the portions of the document it actually needs into memory.
/// This is more efficient than loading the entire document into memory, especially when
/// working with large documents, and allows for working with documents larger than the
/// amount of available memory.
///
/// Because Pdfium must know the total content length in advance prior to loading
/// any portion of it, the given reader must implement the `Seek` trait as well as
/// the `Read` trait.
///
/// If the document is password protected, the given password will be used
/// to unlock it.
///
/// This function is not available when compiling to WASM. You have several options for
/// loading your PDF document data in WASM:
/// * Use the [Pdfium::load_pdf_from_fetch()] function to download document data from a
/// URL using the browser's built-in `fetch()` API. This function is only available when
/// compiling to WASM.
/// * Use the [Pdfium::load_pdf_from_blob()] function to load document data from a
/// Javascript `File` or `Blob` object (such as a `File` object returned from an HTML
/// `<input type="file">` element). This function is only available when compiling to WASM.
/// * Use another method to retrieve the bytes of the target document over the network,
/// then load those bytes into Pdfium using either the [Pdfium::load_pdf_from_byte_slice()]
/// function or the [Pdfium::load_pdf_from_byte_vec()] function.
/// * Embed the bytes of the target document directly into the compiled WASM module
/// using the `include_bytes!()` macro.
#[cfg(not(target_arch = "wasm32"))]
pub fn load_pdf_from_reader<R: Read + Seek + 'static>(
&self,
reader: R,
password: Option<&str>,
) -> Result<PdfDocument, PdfiumError> {
let mut reader = get_pdfium_file_accessor_from_reader(reader);
Self::pdfium_document_handle_to_result(
self.bindings
.FPDF_LoadCustomDocument(reader.as_fpdf_file_access_mut_ptr(), password),
self.bindings(),
)
.map(|mut document| {
// Give the newly-created document ownership of the reader, so that Pdfium can continue
// to read from it on an as-needed basis throughout the lifetime of the document.
document.set_file_access_reader(reader);
document
})
}
/// Attempts to open a [PdfDocument] by loading document data from the given URL.
/// The Javascript `fetch()` API is used to download data over the network.
///
/// If the document is password protected, the given password will be used to unlock it.
///
/// This function is only available when compiling to WASM.
#[cfg(any(doc, target_arch = "wasm32"))]
pub async fn load_pdf_from_fetch<'a>(
&'a self,
url: impl ToString,
password: Option<&str>,
) -> Result<PdfDocument<'a>, PdfiumError> {
if let Some(window) = window() {
let fetch_result = JsFuture::from(window.fetch_with_str(url.to_string().as_str()))
.await
.map_err(PdfiumError::WebSysFetchError)?;
debug_assert!(fetch_result.is_instance_of::<Response>());
let response: Response = fetch_result
.dyn_into()
.map_err(|_| PdfiumError::WebSysInvalidResponseError)?;
let blob: Blob =
JsFuture::from(response.blob().map_err(PdfiumError::WebSysFetchError)?)
.await
.map_err(PdfiumError::WebSysFetchError)?
.into();
self.load_pdf_from_blob(blob, password).await
} else {
Err(PdfiumError::WebSysWindowObjectNotAvailable)
}
}
/// Attempts to open a [PdfDocument] by loading document data from the given `Blob`.
/// A `File` object returned from a `FileList` is a suitable `Blob`:
///
/// ```text
/// <input id="filePicker" type="file">
///
/// const file = document.getElementById('filePicker').files[0];
/// ```
///
/// If the document is password protected, the given password will be used to unlock it.
///
/// This function is only available when compiling to WASM.
#[cfg(any(doc, target_arch = "wasm32"))]
pub async fn load_pdf_from_blob<'a>(
&'a self,
blob: Blob,
password: Option<&str>,
) -> Result<PdfDocument<'a>, PdfiumError> {
let array_buffer: ArrayBuffer = JsFuture::from(blob.array_buffer())
.await
.map_err(PdfiumError::WebSysFetchError)?
.into();
let u8_array: Uint8Array = Uint8Array::new(&array_buffer);
let bytes: Vec<u8> = u8_array.to_vec();
self.load_pdf_from_byte_vec(bytes, password)
}
/// Creates a new, empty [PdfDocument] in memory.
pub fn create_new_pdf(&self) -> Result<PdfDocument, PdfiumError> {
Self::pdfium_document_handle_to_result(
self.bindings.FPDF_CreateNewDocument(),
self.bindings(),
)
.map(|mut document| {
document.set_version(PdfDocumentVersion::DEFAULT_VERSION);
document
})
}
/// Returns a [PdfDocument] from the given `FPDF_DOCUMENT` handle, if possible.
pub(crate) fn pdfium_document_handle_to_result(
handle: crate::bindgen::FPDF_DOCUMENT,
bindings: &dyn PdfiumLibraryBindings,
) -> Result<PdfDocument, PdfiumError> {
if handle.is_null() {
if let Some(error) = bindings.get_pdfium_last_error() {
Err(PdfiumError::PdfiumLibraryInternalError(error))
} else {
// This would be an unusual situation; a null handle indicating failure,
// yet Pdfium's error code indicates success.
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Ok(PdfDocument::from_pdfium(handle, bindings))
}
}
}
impl Drop for Pdfium {
/// Closes the external Pdfium library, releasing held memory.
#[inline]
fn drop(&mut self) {
self.bindings.FPDF_DestroyLibrary();
}
}
impl Default for Pdfium {
/// Binds to a Pdfium library that was statically linked into the currently running
/// executable by calling [Pdfium::bind_to_statically_linked_library()]. This function
/// will panic if no statically linked Pdfium functions can be located.
#[cfg(feature = "static")]
#[inline]
fn default() -> Self {
Pdfium::new(Pdfium::bind_to_statically_linked_library().unwrap())
}
/// Binds to an external Pdfium library, loading it from the system libraries,
/// by calling [Pdfium::bind_to_system_library()]. This function will panic if no
/// suitable system library can be loaded.
#[cfg(not(feature = "static"))]
#[inline]
fn default() -> Self {
Pdfium::new(Pdfium::bind_to_system_library().unwrap())
}
}
#[cfg(feature = "sync")]
unsafe impl Sync for Pdfium {}
#[cfg(feature = "sync")]
unsafe impl Send for Pdfium {}