A fast, helpful, and open-source document parser
pdfium: set_crop_box, render_into and Bitmap::new_with_format for extract-compatible rendering
Logan Markewich0d2df2489ff1
diff --git a/crates/pdfium-sys/src/dynamic.rs b/crates/pdfium-sys/src/dynamic.rsindex 35d4900..b8094ff 100644--- a/crates/pdfium-sys/src/dynamic.rs+++ b/crates/pdfium-sys/src/dynamic.rs@@ -120,6 +120,7 @@ pub struct PdfiumBindings {pub FPDF_GetPageHeightF: unsafe extern "C" fn(FPDF_PAGE) -> f32,pub FPDF_GetPageBoundingBox: unsafe extern "C" fn(FPDF_PAGE, *mut FS_RECTF) -> FPDF_BOOL,pub FPDFPage_GetRotation: unsafe extern "C" fn(FPDF_PAGE) -> std::os::raw::c_int,+ pub FPDFPage_SetCropBox: unsafe extern "C" fn(FPDF_PAGE, f32, f32, f32, f32),/// LlamaParse fork API (absent from stock pdfium and older fork/// releases); callers fall back to the raw-byte `/UserUnit` scan.pub FPDFPage_GetUserUnit: Option<unsafe extern "C" fn(FPDF_PAGE) -> f32>,@@ -593,6 +594,7 @@ impl PdfiumBindings {FPDF_GetPageHeightF: load_fn!(lib, "FPDF_GetPageHeightF"),FPDF_GetPageBoundingBox: load_fn!(lib, "FPDF_GetPageBoundingBox"),FPDFPage_GetRotation: load_fn!(lib, "FPDFPage_GetRotation"),+ FPDFPage_SetCropBox: load_fn!(lib, "FPDFPage_SetCropBox"),FPDFPage_GetUserUnit: load_fn_opt!(lib, "FPDFPage_GetUserUnit"),FPDFPage_Flatten: load_fn_opt!(lib, "FPDFPage_Flatten"),FPDF_PageToDevice: load_fn!(lib, "FPDF_PageToDevice"),diff --git a/crates/pdfium/src/bitmap.rs b/crates/pdfium/src/bitmap.rsindex e96b466..5b88b7b 100644--- a/crates/pdfium/src/bitmap.rs+++ b/crates/pdfium/src/bitmap.rs@@ -36,11 +36,24 @@ impl<'lib> Bitmap<'lib> {/// from the call site (e.g. returning a `Bitmap<'lib>` from a method on/// `Page<'_, 'lib>`, whose existence already proves the lock is held).pub unsafe fn new(width: i32, height: i32) -> Result<Self, PdfiumError> {+ unsafe { Self::new_with_format(width, height, pdfium_sys::FPDFBitmap_BGRA as i32) }+ }++ /// Like [`Self::new`] with an explicit pdfium pixel format (`FPDFBitmap_BGR`,+ /// `FPDFBitmap_BGRA`, `FPDFBitmap_BGRx`, `FPDFBitmap_Gray`). pdfium composites+ /// 24-bit and 32-bit targets through different paths, so a consumer matching+ /// another renderer's pixels byte for byte has to pick the same format it used.+ /// `buffer()`/`stride()` follow the format: 3 bytes per pixel for BGR.+ pub unsafe fn new_with_format(+ width: i32,+ height: i32,+ format: i32,+ ) -> Result<Self, PdfiumError> {let handle = unsafe {ffi!(FPDFBitmap_CreateEx(width,height,- pdfium_sys::FPDFBitmap_BGRA as i32,+ format,std::ptr::null_mut(),0, // stride=0 lets pdfium choose))diff --git a/crates/pdfium/src/page.rs b/crates/pdfium/src/page.rsindex c195ce8..edb69f9 100644--- a/crates/pdfium/src/page.rs+++ b/crates/pdfium/src/page.rs@@ -270,6 +270,66 @@ impl<'doc, 'lib: 'doc> Page<'doc, 'lib> {unsafe { ffi!(FPDFPage_GetRotation(self.handle)) }}+ /// Replace the page's `/CropBox` (PDF user space, points, bottom-left origin).+ /// pdfium recomputes the page size, so [`Self::width`], [`Self::height`],+ /// [`Self::view_box`] and any later render see the new box. This is how a+ /// caller renders one region of a page at full resolution: crop, then render+ /// the whole (now smaller) page.+ pub fn set_crop_box(&self, left: f32, bottom: f32, right: f32, top: f32) {+ unsafe { ffi!(FPDFPage_SetCropBox(self.handle, left, bottom, right, top)) }+ }++ /// Render into a caller-owned bitmap with explicit pixel geometry and pdfium+ /// flags, then draw form fields on top when `form` is given.+ ///+ /// The page is mapped onto the `size_x × size_y` pixel rectangle whose top-left+ /// sits at (`start_x`, `start_y`) in `bitmap`; parts outside the bitmap are+ /// clipped, so a shorter bitmap with a negative `start_y` renders one horizontal+ /// strip of a tall page. The bitmap is not cleared first. For a plain DPI-based+ /// render use [`Self::render_with_form`]; this exists for consumers that need an+ /// exact edge length or a different flag set (the LlamaParse extractor renders+ /// with `FPDF_ANNOT` alone, at a pixel size it computes from a max-edge rule).+ #[allow(clippy::too_many_arguments)]+ pub fn render_into(+ &self,+ bitmap: &Bitmap<'lib>,+ start_x: i32,+ start_y: i32,+ size_x: i32,+ size_y: i32,+ form: Option<&FormEnvironment>,+ flags: i32,+ ) {+ unsafe {+ ffi!(FPDF_RenderPageBitmap(+ bitmap.handle(),+ self.handle,+ start_x,+ start_y,+ size_x,+ size_y,+ 0,+ flags,+ ));+ }+ if let Some(form) = form {+ // Form layer drawn with flags 0, as the extractor does (no popups).+ unsafe {+ ffi!(FPDF_FFLDraw(+ form.handle,+ bitmap.handle(),+ self.handle,+ start_x,+ start_y,+ size_x,+ size_y,+ 0,+ 0,+ ));+ }+ }+ }+/// Page dimensions in the same rotation-adjusted viewport coordinate/// space returned by [`Self::page_to_viewport`]. Unlike [`Self::width`]//// [`Self::height`], this applies the `/UserUnit` multiplier, so the