Expand description
Safe, self-contained Rust bindings for PDFium, Google’s PDF engine: open documents from memory, inspect pages, render to owned pixel buffers, map coordinates between rendered pixels and PDF page space, extract positioned text, and draw form fields — safely usable from concurrent code.
§Quickstart
PDFium is loaded at runtime (no build-time linking): fetch a binary
once with cargo xtask fetch-pdfium (repo checkouts), ship one next to
your executable, or point PDFIUM_LIB_PATH at one. Then:
use firecrawl_pdfium::{Pdfium, RenderConfig};
// Discovery chain: $PDFIUM_LIB_PATH → exe dir → ./target/pdfium → system.
let pdfium = Pdfium::load()?;
let bytes = std::fs::read("document.pdf")?;
let doc = pdfium.load_document(bytes, None)?; // None = no password
println!("{} pages", doc.page_count());
let page = doc.page(0)?;
let rendered = page.render(&RenderConfig::new().dpi(144.0))?;
println!(
"{}x{} pixels, {} bytes/row, {:?}",
rendered.width(),
rendered.height(),
rendered.stride(),
rendered.format(),
);
// Map a pixel back into PDF page space (points, origin bottom-left):
let pt = rendered.transform().pixel_to_page((10.0, 10.0).into());
println!("pixel (10,10) is at ({:.2}, {:.2})pt", pt.x, pt.y);§Errors on hostile input
Encrypted documents surface as Error::PasswordRequired /
Error::IncorrectPassword / Error::UnsupportedSecurity; garbage
and truncated files as Error::InvalidPdf; oversized render requests
as Error::RenderTooLarge (bounded by
RenderConfig::max_output_bytes).
§Concurrency model
PDFium itself is single-threaded. Every call is serialized through one
process-wide mutex, making all handle types Send + Sync — safe from
any thread, but not parallel. See Pdfium for details and
docs/DESIGN.md for the soundness argument. For parallel throughput,
shard across processes.
§Raw FFI escape hatch
The sys module exposes the loaded function table for calls this
crate does not wrap yet; combine with Pdfium::ffi_lock to stay
within the serialization contract.
Modules§
- sys
- Raw FFI layer: PDFium types, constants, and the dynamically loaded function table.
Structs§
- Color
- An sRGB color with straight alpha, used for render backgrounds.
- Page
Char - One character as reported by PDFium’s text engine, with its geometry in page space.
- Page
Point - A point in page space (points, origin bottom-left, y-up).
- Page
Rect - An axis-aligned rectangle in page space.
- Page
Size - Page dimensions in points (1/72 inch), after applying the page’s
/Rotateentry (PDFium’sFPDF_GetPageWidthF/HeightFsemantics: a portrait page with/Rotate 90reports landscape dimensions). - Page
Text - Text content of one page: the extracted string plus per-character
geometry. Plain owned data — no PDFium resources,
Send + Sync, outlives page and document. - Page
Transform - Affine transform between pixel space of one rendered bitmap and page space of the page it was rendered from.
- PdfDocument
- An open PDF document.
- PdfPage
- An open page of a
PdfDocument. - Pdfium
- Handle to the process-wide PDFium library.
- Permissions
- Document permission flags from the PDF’s encryption dictionary
(
FPDF_GetDocPermissions). For unencrypted documents every permission is granted (0xFFFF_FFFF). - Pixel
Point - A point in pixel space (pixels, origin top-left, y-down).
- Pixel
Rect - An axis-aligned rectangle in pixel space: top-left corner plus size.
- Render
Config - Configuration for
PdfPage::render. - Rendered
Page - A rendered page: owned pixels plus everything needed to interpret them.
Enums§
- Error
- All errors returned by the safe API.
- Form
Type - The kind of interactive form a document contains, per
FPDF_GetFormType. - Load
Error - Failure to locate, open, or validate the PDFium shared library.
- Metadata
Tag - Standard PDF metadata tags accepted by
PdfDocument::metadata. - Pixel
Format - Pixel layout of a rendered bitmap.
- Rotation
- A rotation in 90° clockwise increments — used both for a page’s own
/Rotateentry and for extra rotation applied at render time.
Constants§
- DEFAULT_
MAX_ TEXT_ CHARS - Default ceiling for
PdfPage::text: one million characters. Real pages hold a few thousand; the ceiling exists because a small hostile PDF can claim an enormous character count and this crate allocates roughly 90 bytes per character during extraction. - PDFIUM_
LIB_ PATH_ ENV - Environment variable consulted first by
Pdfium::load: a path to the PDFium library file, or to a directory containing it.
Functions§
- platform_
library_ name - The name of the PDFium shared library on this platform
(
libpdfium.dylib,libpdfium.so, orpdfium.dll). - platform_
slug - The platform slug used by
pdfium-binariesrelease assets and bycargo xtask fetch-pdfium(mac-arm64,linux-x64,win-x64, …).
Type Aliases§
- Result
- Convenience alias used throughout the crate.