Skip to main content

Crate firecrawl_pdfium

Crate firecrawl_pdfium 

Source
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.
PageChar
One character as reported by PDFium’s text engine, with its geometry in page space.
PagePoint
A point in page space (points, origin bottom-left, y-up).
PageRect
An axis-aligned rectangle in page space.
PageSize
Page dimensions in points (1/72 inch), after applying the page’s /Rotate entry (PDFium’s FPDF_GetPageWidthF/HeightF semantics: a portrait page with /Rotate 90 reports landscape dimensions).
PageText
Text content of one page: the extracted string plus per-character geometry. Plain owned data — no PDFium resources, Send + Sync, outlives page and document.
PageTransform
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).
PixelPoint
A point in pixel space (pixels, origin top-left, y-down).
PixelRect
An axis-aligned rectangle in pixel space: top-left corner plus size.
RenderConfig
Configuration for PdfPage::render.
RenderedPage
A rendered page: owned pixels plus everything needed to interpret them.

Enums§

Error
All errors returned by the safe API.
FormType
The kind of interactive form a document contains, per FPDF_GetFormType.
LoadError
Failure to locate, open, or validate the PDFium shared library.
MetadataTag
Standard PDF metadata tags accepted by PdfDocument::metadata.
PixelFormat
Pixel layout of a rendered bitmap.
Rotation
A rotation in 90° clockwise increments — used both for a page’s own /Rotate entry 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, or pdfium.dll).
platform_slug
The platform slug used by pdfium-binaries release assets and by cargo xtask fetch-pdfium (mac-arm64, linux-x64, win-x64, …).

Type Aliases§

Result
Convenience alias used throughout the crate.