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 onlyheight
- 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
ormatrix
in this mode
§Manual scaling mode (both width AND height specified)
- Provide both
width
andheight
- You must also provide either
scale
ORmatrix
- 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
impl PdfiumRenderConfig
Sourcepub fn new() -> Self
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?
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}
Sourcepub fn with_size(self, width: i32, height: i32) -> Self
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)
Sourcepub fn with_width(self, width: i32) -> Self
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)
Sourcepub fn with_height(self, height: i32) -> Self
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?
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}
Sourcepub fn with_format(self, format: PdfiumBitmapFormat) -> Self
pub fn with_format(self, format: PdfiumBitmapFormat) -> Self
Sets the pixel format for the rendered bitmap.
Different formats have different memory requirements and compatibility:
PdfiumBitmapFormat::Bgra
: 32-bit with alpha, most common for displayPdfiumBitmapFormat::Bgr
: 24-bit without alpha, smaller memory footprintPdfiumBitmapFormat::Gray
: 8-bit grayscale, smallest memory usage
Sourcepub fn with_background(self, color: PdfiumColor) -> Self
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
Sourcepub fn with_transparent_background(self) -> Self
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).
Sourcepub fn with_flags(self, flags: PdfiumRenderFlags) -> Self
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
Sourcepub fn add_flags(self, flags: PdfiumRenderFlags) -> Self
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
Sourcepub fn with_clipping(self, rect: PdfiumRect) -> Self
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
Sourcepub fn with_scale(self, scale: f32) -> Self
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)
Sourcepub fn with_pan(self, pan_x: f32, pan_y: f32) -> Self
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 pixelspan_y
- Vertical translation in pixels
Sourcepub fn with_matrix(self, matrix: PdfiumMatrix) -> Self
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
Sourcepub fn validate(&self) -> PdfiumResult<()>
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
impl Clone for PdfiumRenderConfig
Source§fn clone(&self) -> PdfiumRenderConfig
fn clone(&self) -> PdfiumRenderConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more