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
impl Pdfium
Sourcepub fn load_document(
&self,
bytes: impl Into<Vec<u8>>,
password: Option<&str>,
) -> Result<PdfDocument>
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
Error::PasswordRequired— encrypted, no password given.Error::IncorrectPassword— encrypted, wrong password.Error::UnsupportedSecurity— unsupported encryption scheme.Error::InvalidPdf— not a PDF / unrecoverably corrupt.
Sourcepub fn load_document_from_file(
&self,
path: impl AsRef<Path>,
password: Option<&str>,
) -> Result<PdfDocument>
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
impl Pdfium
Sourcepub fn load() -> Result<Pdfium>
pub fn load() -> Result<Pdfium>
Loads PDFium using the documented discovery chain.
Candidates are tried in order; the first that exists wins:
- The
PDFIUM_LIB_PATHenvironment variable (library file, or directory containingplatform_library_name). If set but unloadable, this is a hard error — no silent fallback. - The directory containing the current executable.
./target/pdfium/<platform>/libthen.../bin(the archives uselibeverywhere except Windows, which usesbin; both are probed on every platform) — the layout produced bycargo xtask fetch-pdfium.- The system loader’s default search path, by bare library name.
If PDFium is already loaded, returns the existing instance without consulting the chain.
Sourcepub fn load_from_path(path: impl AsRef<Path>) -> Result<Pdfium>
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.
Sourcepub fn load_from_directory(dir: impl AsRef<Path>) -> Result<Pdfium>
pub fn load_from_directory(dir: impl AsRef<Path>) -> Result<Pdfium>
Loads PDFium from dir/platform_library_name().
Sourcepub fn instance() -> Option<Pdfium>
pub fn instance() -> Option<Pdfium>
Returns the already-loaded instance, if any, without attempting a load.
Sourcepub fn loaded_from(&self) -> Option<&Path>
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).
Sourcepub fn ffi_lock(&self) -> MutexGuard<'static, ()>
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).
Sourcepub unsafe fn raw(&self) -> &Bindings
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.