Skip to main content

Pdfium

Struct Pdfium 

Source
pub struct Pdfium { /* private fields */ }
Expand description

Handle to the process-wide PDFium library.

§Loading model

PDFium has process-global state, so this crate maintains one instance per process: the first successful Pdfium::load / Pdfium::load_from_path / Pdfium::load_from_directory call initializes PDFium and every later call returns a handle to the same instance (Pdfium::load_from_path with a different path returns Error::AlreadyLoaded instead of silently using the wrong binary). The library is never unloaded; see docs/DESIGN.md for why.

§Thread safety

Pdfium (and every handle derived from it) is Send + Sync. PDFium itself is single-threaded, so all FFI calls are serialized through one process-wide mutex — concurrent use is safe but not parallel. For CPU-bound throughput, use multiple processes (PDFium upstream’s own recommendation).

Implementations§

Source§

impl Pdfium

Source

pub fn load_document( &self, bytes: impl Into<Vec<u8>>, password: Option<&str>, ) -> Result<PdfDocument>

Opens a PDF from bytes, taking ownership of them.

password unlocks encrypted documents (PDFium tries UTF-8 then Latin-1 encodings of it). Pass None for unencrypted documents; a Some password is ignored by unencrypted documents.

§Errors
Source

pub fn load_document_from_file( &self, path: impl AsRef<Path>, password: Option<&str>, ) -> Result<PdfDocument>

Opens a PDF file from disk (reads it fully into memory first — PDFium is fastest and simplest with in-memory documents).

Source§

impl Pdfium

Source

pub fn load() -> Result<Pdfium>

Loads PDFium using the documented discovery chain.

Candidates are tried in order; the first that exists wins:

  1. The PDFIUM_LIB_PATH environment variable (library file, or directory containing platform_library_name). If set but unloadable, this is a hard error — no silent fallback.
  2. The directory containing the current executable.
  3. ./target/pdfium/<platform>/lib then .../bin (the archives use lib everywhere except Windows, which uses bin; both are probed on every platform) — the layout produced by cargo xtask fetch-pdfium.
  4. The system loader’s default search path, by bare library name.

If PDFium is already loaded, returns the existing instance without consulting the chain.

Source

pub fn load_from_path(path: impl AsRef<Path>) -> Result<Pdfium>

Loads PDFium from an explicit library file path (the recommended production configuration).

Returns Error::AlreadyLoaded if PDFium was already loaded from a different path in this process.

Source

pub fn load_from_directory(dir: impl AsRef<Path>) -> Result<Pdfium>

Loads PDFium from dir/platform_library_name().

Source

pub fn instance() -> Option<Pdfium>

Returns the already-loaded instance, if any, without attempting a load.

Source

pub fn loaded_from(&self) -> Option<&Path>

The path the active library was loaded from (None when it was resolved by bare name through the system loader).

Source

pub fn ffi_lock(&self) -> MutexGuard<'static, ()>

Acquires the process-wide FFI lock guarding all PDFium calls.

Only needed when calling into crate::sys directly: hold the guard for the duration of every raw call sequence, and never call safe-API methods while holding it (they would deadlock re-acquiring the same lock).

Source

pub unsafe fn raw(&self) -> &Bindings

The raw bindings table, for use with Pdfium::ffi_lock.

§Safety

See crate::sys::Bindings: all calls must be serialized via Pdfium::ffi_lock, and PDFium’s per-function preconditions apply.

Trait Implementations§

Source§

impl Clone for Pdfium

Source§

fn clone(&self) -> Pdfium

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Pdfium

Source§

impl Debug for Pdfium

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.