PDFium-rs
A modern, streamlined Rust wrapper for the PDFium C library, designed for simplicity and thread safety in interactive applications. PDFium is Google's PDF library developed for (and used in) the Chromium and Chrome web browsers.
PDFium-rs serves as one of the two PDF engines behind MView6, a PDF and photo viewer written in Rust and GTK4. With a single keypress, you can switch engines and compare the rendering quality differences between mupdf and PDFium.
Features
- Thread-safe static library access - Initialize once, use everywhere
- Lifetime-free API - No complex lifetime management for documents, pages, and bitmaps
- Access to C API - Safe access to the low-level C API is possible
- Renderer selection - Select either
SkiaorAGG(Anti-Grain Geometry) as renderer for PDFium - Interactive rendering focus - Optimized render functions for real-time applications
Why This Crate?
While existing PDFium bindings for Rust are available, this crate takes a different approach focused on ease of use and thread safety:
Thread-Safe Static Access
This library uses a modern, static, and thread-safe initialization pattern with parking_lot::ReentrantMutex. On first use, it checks for the availability of the PDFium dynamic library on your system or in a specified directory, and stores the library reference statically for the application's lifetime. This prevents deadlocks when used multiple times in the same thread and eliminates the need for complex library management.
No Lifetime Complexity
Unlike other implementations, this crate doesn't impose lifetimes on structs representing documents, pages, bitmaps, and other structures. This makes integration into your application much simpler - you can store these objects wherever you need them without fighting the borrow checker. This makes it ideal for interactive use cases, such as PDF viewers, editors, and other real-time applications.
Access to C API without unsafe Functions
While focusing on high-level safe Rust integration, PDFium-rs also provides safe public access to the C API. Unsafe pointers to C structures and memory are transparently replaced with their safe Rust counterparts.
Quick Start
use *;
Using the PDFium C API
This is the same example, but now using the C API of PDFium directly.
- Using the C API is safe, no
unsafecode blocks in your code - Access the C API through [
lib] or [try_lib] - You can mix high-level Rust and C, shown here with the
bitmap.saveoperation
use *;
Installation
Add this to your Cargo.toml:
[]
= "0.5.8" # Check crates.io for the latest version
For the latest version, visit crates.io or use cargo search pdfium.
Dynamic Library Requirements
PDFium-rs requires the PDFium dynamic library to be available on your system:
- Linux:
libpdfium.so - Windows:
pdfium.dll - macOS:
libpdfium.dylib
Obtaining PDFium Libraries
Pre-built libraries for all major platforms are available from: https://github.com/bblanchon/pdfium-binaries/releases
Choose the appropriate build for your target platform and architecture (x64, arm64, etc.).
Library Installation
Option 1: System Installation (Recommended for Linux/macOS)
- Linux: Place in
/usr/lib/,/usr/local/lib/, or/usr/lib/your_app/ - macOS: Place in
/usr/local/lib/or/opt/homebrew/lib/ - Use [
set_library_location] to specify custom paths
Option 2: Application Bundle (Recommended for Windows)
- Windows: Place
pdfium.dllin the same directory as your executable
Option 3: Custom Location
use *;
// Set custom library path before first use
set_library_location;
// Your application code...
let doc = new_from_path;
Renderer Selection: Skia or AGG
PDFium currently supports two rendering backends. You can select Skia over AGG (Anti-Grain Geometry) using the [set_use_skia] function:
use *;
// Use Skia renderer (must be called before first PDFium usage)
set_use_skia;
// Your application code...
let doc = new_from_path;
Supported Platforms
- Linux: x86_64, aarch64 (ARM64)
- Windows: x86_64, x86 (32-bit)
- macOS: x86_64, aarch64 (Apple Silicon)
All platforms require their respective PDFium dynamic libraries.
Troubleshooting
Library Loading Errors
/// Error loading or initializing the PDFium library
PdfiumError::LibraryError(String)
Solutions
- Verify the PDFium library is in the correct location
- Check that the library architecture matches your application (64-bit vs 32-bit)
- On Linux, verify library dependencies with
ldd libpdfium.so - Use [
set_library_location] to specify the exact path - Ensure file permissions allow reading the library
Windows-Specific Issues
- Install Visual C++ Redistributable if you encounter DLL loading errors
- Ensure
pdfium.dllis in your PATH or application directory
macOS-Specific Issues
- You may need to remove quarantine attributes:
xattr -d com.apple.quarantine libpdfium.dylib - For unsigned libraries, you might need to approve them in System Preferences > Security & Privacy
Runtime Errors
This might indicate an incompatible PDFium library version. Try downloading a different build from the pdfium-binaries releases.
Getting Help
If you encounter issues not covered here:
- Check the GitHub Issues
- Verify your PDFium library version compatibility
- Include your platform, Rust version, and PDFium library source when reporting issues
Current Status
Work in Progress - This crate is actively being developed and currently implements a focused subset of PDFium's functionality. While it covers the core rendering and document manipulation features needed for most interactive applications, it doesn't yet provide the complete feature set available in more comprehensive PDFium bindings.
What's Available
- Document loading and basic properties
- Page rendering with customizable parameters
- Bitmap handling and export
- Thread-safe access patterns
- Safe C API access
Planned Features
- Text extraction and search
- Form field support
- Annotation handling
- Additional rendering options
- Security and encryption support
- Bookmarks and navigation
Requirements
- Rust 1.85 or later
- PDFium dynamic library (see installation instructions)
Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
After cloning the repository, install the git hooks:
License
PDFium-rs is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.