pdfium-bind

Crates.iopdfium-bind
lib.rspdfium-bind
version0.1.0
created_at2025-12-26 04:01:06.570532+00
updated_at2025-12-26 04:01:06.570532+00
descriptionRust bindings for PDFium with vendored prebuilt binaries
homepagehttps://github.com/houqp/kiorg
repositoryhttps://github.com/houqp/kiorg
max_upload_size
id2005247
size63,015
QP Hou (houqp)

documentation

README

pdfium-bind

Rust FFI bindings and a high-level wrapper for PDFium.

Designed for ease of distribution: This crate eliminates the need for users to manually install PDFium on their systems. It embeds prebuilt PDFium binaries directly into your executable.

Features

  • dynamic (Default): Embeds the PDFium dynamic library within your binary. At runtime, it extracts the library to a temporary file and loads it.
  • static: Links PDFium statically at build time.

Configuration

You can override the PDFium library and headers used during build by setting the following environment variables:

  • PDFIUM_STATIC_LIB_PATH: Path to libpdfium.a (for static feature).
  • PDFIUM_DYNAMIC_LIB_PATH: Path to the dynamic library (e.g., .so, .dylib, or .dll) (for dynamic feature).
  • PDFIUM_INCLUDE_PATH: Path to the directory containing PDFium headers (required if any of the above are set).

If these variables are not set, the build script will automatically download the appropriate PDFium binary for your platform.

Usage

use pdfium_bind::PdfDocument;
use std::path::Path;

fn main() -> Result<(), String> {
    // Open a PDF document
    let path = Path::new("example.pdf");
    let doc = PdfDocument::open(path)?;

    // Get page count
    let count = doc.page_count();
    println!("Page count: {}", count);

    // Get PDF version
    println!("PDF version: {}", doc.get_pdf_version());

    // Get metadata
    if let Some(title) = doc.get_metadata_value("Title") {
        println!("Title: {}", title);
    }

    // Render a page (e.g., page 0 at 300 DPI)
    // Returns (pixel_data, width, height) where pixel_data is RGBA
    let (pixels, width, height) = doc.render_page(0, 300.0)?;
    println!("Rendered page size: {}x{}", width, height);

    Ok(())
}

Windows-specific Cache Cleanup

When using the dynamic feature on Windows, the PDFium DLL is extracted to a temporary location. If you want to ensure this file is cleaned up when your application exits, you can call:

pdfium_bind::cleanup_cache();

On Unix platforms, the temporary file is deleted immediately after being loaded into memory.

Automatic Downloads

The crate automatically downloads pre-built PDFium binaries for supported platforms:

Supported Platforms

  • macOS (aarch64, x86_64)
  • Linux (x86_64, aarch64)
  • Windows (x86_64, aarch64, x86)
Commit count: 0

cargo fmt