PdfiumRenderConfig

Struct PdfiumRenderConfig 

Source
pub struct PdfiumRenderConfig { /* private fields */ }
Expand description

Configuration for PDF page rendering operations.

Controls how PDF pages are rendered to bitmaps, including dimensions, appearance, image format and performance characteristics.

§Parameter Rules

The configuration handles dimensions and scaling in two distinct modes:

§Auto-scaling mode (width OR height specified)

  • Provide only width or only height
  • The missing dimension is calculated automatically using the page’s aspect ratio
  • Scaling is determined automatically to fully fit the page into the bitmap
  • Error: Do not provide a scale or matrix in this mode

§Manual scaling mode (both width AND height specified)

  • Provide both width and height
  • You must also provide either scale OR matrix
  • Error: Providing neither scaling instructions will cause an error

§PDFium Integration

All parameters are passed directly to PDFium, except for the automatic dimension and scaling calculations described above.

§Examples

use pdfium::*;

// Auto-scaling at specified width (height calculated automatically), grayscale,
// and reset the rendering flags (default is ANNOT and LCD_TEXT)
let config = PdfiumRenderConfig::new()
    .with_width(800)
    .with_format(PdfiumBitmapFormat::Gray)
    .with_flags(PdfiumRenderFlags::empty());

// Manual scaling and panning
let config = PdfiumRenderConfig::new()
    .with_size(1920, 1080)
    .with_scale(2.5)
    .with_pan(900.0, -200.0);

Implementations§

Source§

impl PdfiumRenderConfig

Source

pub fn new() -> Self

Creates a new render configuration with default values.

Default configuration uses BGRA format with a white background, the PdfiumRenderFlags::ANNOT and PdfiumRenderFlags::LCD_TEXT rendering flags and no clipping.

You must specify at least width or height before rendering.

Examples found in repository?
examples/export_pages.rs (line 51)
34pub fn example_export_pages_to_images() -> PdfiumResult<()> {
35    // Load the PDF document from the specified file path
36    // Parameters:
37    // - "resources/groningen.pdf": Path to the PDF file (relative to current working directory)
38    // - None: No password required for this PDF (use Some("password") if needed)
39    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
40
41    // Iterate through all pages in the document
42    // document.pages() returns an iterator over all pages
43    // enumerate() adds an index counter starting from 0
44    // This gives us both the page object and its 0-based index
45    for (index, page) in document.pages().enumerate() {
46        // Render the current page as a bitmap image
47        // This is where the PDF content gets converted to a raster image
48        //
49        // In the configuration we only specify the height in pixels. The width will be calculated
50        // automatically to maintain aspect ratio.
51        let config = PdfiumRenderConfig::new().with_height(1080);
52        let bitmap = page?.render(&config)?;
53
54        // Verify that the bitmap was rendered at the requested height
55        // This assertion ensures the rendering process worked as expected
56        // If this fails, it indicates a bug in the rendering logic
57        assert_eq!(bitmap.height(), 1080);
58
59        // Generate a unique filename for this page
60        // Format: "groningen-page-{page_number}.jpg"
61        // - index + 1 converts from 0-based index to 1-based page numbers
62        //   * Page 0 becomes "groningen-page-1.jpg"
63        //   * Page 1 becomes "groningen-page-2.jpg", etc.
64        // - The .jpg extension indicates JPEG format will be used
65        let filename = format!("groningen-page-{}.jpg", index + 1);
66
67        // Save the rendered bitmap to disk as a JPEG image
68        // Parameters:
69        // - &filename: Reference to the generated filename string
70        // - image::ImageFormat::Jpeg: Specifies JPEG compression format
71        //   * Alternative format: Png (lossless)
72        //   * JPEG provides good compression but is lossy (some quality loss)
73        //
74        // The save operation handles:
75        // - Converting from BGRA format to JPEG-compatible format
76        // - Applying JPEG compression
77        // - Writing the file to disk
78        bitmap.save(&filename, image::ImageFormat::Jpeg)?;
79
80        // Note: No explicit cleanup needed - Rust's ownership system automatically
81        // deallocates the bitmap memory when it goes out of scope at the end of this iteration
82    }
83
84    // Return success - all pages have been successfully exported
85    Ok(())
86}
Source

pub fn with_size(self, width: i32, height: i32) -> Self

Sets width and height of the bitmap in pixels.

Short for .with_width(width).with_height(height)

When both dimensions are specified, you must also provide either a scale factor or a transformation matrix to define how the page maps to the bitmap.

§Arguments
  • width - Target bitmap width in pixels (must be > 0)
  • height - Target bitmap height in pixels (must be > 0)
Source

pub fn with_width(self, width: i32) -> Self

Sets the bitmap width.

If height is not provided, it will be calculated automatically according to the aspect ratio of the page. In that case also the required scale factor will be calculated.

§Arguments
  • width - Target bitmap width in pixels (must be > 0)
Source

pub fn with_height(self, height: i32) -> Self

Sets the bitmap height.

If width is not provided, it will be calculated automatically according to the aspect ratio of the page. In that case also the required scale factor will be calculated.

§Arguments
  • height - Target bitmap height in pixels (must be > 0)
Examples found in repository?
examples/export_pages.rs (line 51)
34pub fn example_export_pages_to_images() -> PdfiumResult<()> {
35    // Load the PDF document from the specified file path
36    // Parameters:
37    // - "resources/groningen.pdf": Path to the PDF file (relative to current working directory)
38    // - None: No password required for this PDF (use Some("password") if needed)
39    let document = PdfiumDocument::new_from_path("resources/groningen.pdf", None)?;
40
41    // Iterate through all pages in the document
42    // document.pages() returns an iterator over all pages
43    // enumerate() adds an index counter starting from 0
44    // This gives us both the page object and its 0-based index
45    for (index, page) in document.pages().enumerate() {
46        // Render the current page as a bitmap image
47        // This is where the PDF content gets converted to a raster image
48        //
49        // In the configuration we only specify the height in pixels. The width will be calculated
50        // automatically to maintain aspect ratio.
51        let config = PdfiumRenderConfig::new().with_height(1080);
52        let bitmap = page?.render(&config)?;
53
54        // Verify that the bitmap was rendered at the requested height
55        // This assertion ensures the rendering process worked as expected
56        // If this fails, it indicates a bug in the rendering logic
57        assert_eq!(bitmap.height(), 1080);
58
59        // Generate a unique filename for this page
60        // Format: "groningen-page-{page_number}.jpg"
61        // - index + 1 converts from 0-based index to 1-based page numbers
62        //   * Page 0 becomes "groningen-page-1.jpg"
63        //   * Page 1 becomes "groningen-page-2.jpg", etc.
64        // - The .jpg extension indicates JPEG format will be used
65        let filename = format!("groningen-page-{}.jpg", index + 1);
66
67        // Save the rendered bitmap to disk as a JPEG image
68        // Parameters:
69        // - &filename: Reference to the generated filename string
70        // - image::ImageFormat::Jpeg: Specifies JPEG compression format
71        //   * Alternative format: Png (lossless)
72        //   * JPEG provides good compression but is lossy (some quality loss)
73        //
74        // The save operation handles:
75        // - Converting from BGRA format to JPEG-compatible format
76        // - Applying JPEG compression
77        // - Writing the file to disk
78        bitmap.save(&filename, image::ImageFormat::Jpeg)?;
79
80        // Note: No explicit cleanup needed - Rust's ownership system automatically
81        // deallocates the bitmap memory when it goes out of scope at the end of this iteration
82    }
83
84    // Return success - all pages have been successfully exported
85    Ok(())
86}
Source

pub fn with_format(self, format: PdfiumBitmapFormat) -> Self

Sets the pixel format for the rendered bitmap.

Different formats have different memory requirements and compatibility:

Source

pub fn with_background(self, color: PdfiumColor) -> Self

Sets the background color for areas not covered by page content.

§Arguments
  • color - The background color to use
Source

pub fn with_transparent_background(self) -> Self

Removes the background color, resulting in a transparent background.

Only meaningful when using a pixel format that supports transparency (e.g., BGRA).

Source

pub fn with_flags(self, flags: PdfiumRenderFlags) -> Self

Sets the rendering flags to control various behaviors.

You can combine multiple flags using the bitwise OR operator (|). Default flags are PdfiumRenderFlags::ANNOT and PdfiumRenderFlags::LCD_TEXT

§Arguments
  • flags - Combination of PdfiumRenderFlags
Source

pub fn add_flags(self, flags: PdfiumRenderFlags) -> Self

Adds additional flags to the existing configuration.

This is useful when you want to add flags without replacing existing ones.

§Arguments
  • flags - Combination of PdfiumRenderFlags to add
Source

pub fn with_clipping(self, rect: PdfiumRect) -> Self

Sets a clipping rectangle to render only a portion of the bitmap.

The rectangle is specified in bitmap pixels. Default is to render the entire bitmap.

§Arguments
  • rect - The clipping rectangle in bitmap pixels
Source

pub fn with_scale(self, scale: f32) -> Self

Sets the scaling factor for the rendered bitmap.

Cannot be used with custom transformation matrices.

§Arguments
  • scale - Scaling factor (must be > 0.0)
Source

pub fn with_pan(self, pan_x: f32, pan_y: f32) -> Self

Sets the pan (translation) values for the rendered bitmap.

Pan values are in bitmap pixel coordinates and are applied after scaling. Positive values move the content right/down, negative values move left/up.

Cannot be used with custom transformation matrices.

§Arguments
  • pan_x - Horizontal translation in pixels
  • pan_y - Vertical translation in pixels
Source

pub fn with_matrix(self, matrix: PdfiumMatrix) -> Self

Sets a custom transformation matrix for advanced rendering control.

When specified, scale and pan parameters are not allowed.

§Arguments
  • matrix - The transformation matrix to apply
Source

pub fn validate(&self) -> PdfiumResult<()>

Validates the configuration for internal consistency.

This method checks for conflicting or impossible parameter combinations and returns descriptive error messages for invalid configurations.

Trait Implementations§

Source§

impl Clone for PdfiumRenderConfig

Source§

fn clone(&self) -> PdfiumRenderConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PdfiumRenderConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PdfiumRenderConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.