pub struct PdfiumBitmap { /* private fields */ }Expand description
Rust interface to FPDF_BITMAP
Implementations§
Source§impl PdfiumBitmap
impl PdfiumBitmap
Sourcepub fn empty(
width: i32,
height: i32,
format: PdfiumBitmapFormat,
) -> PdfiumResult<Self>
pub fn empty( width: i32, height: i32, format: PdfiumBitmapFormat, ) -> PdfiumResult<Self>
Creates a new PdfiumBitmap with the given width, height and PdfiumBitmapFormat.
Sourcepub fn fill(&self, color: &PdfiumColor) -> PdfiumResult<()>
pub fn fill(&self, color: &PdfiumColor) -> PdfiumResult<()>
Fills this entire PdfiumBitmap with the given PdfiumColor.
Sourcepub fn width(&self) -> i32
pub fn width(&self) -> i32
Returns the width of the image in the bitmap buffer backing this PdfiumBitmap.
Sourcepub fn height(&self) -> i32
pub fn height(&self) -> i32
Returns the height of the image in the bitmap buffer backing this PdfiumBitmap.
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 format(&self) -> PdfiumBitmapFormat
pub fn format(&self) -> PdfiumBitmapFormat
Returns the pixel format of the image in the bitmap buffer backing this PdfiumBitmap.
Sourcepub fn as_raw_bytes<'a>(&self) -> &'a [u8] ⓘ
pub fn as_raw_bytes<'a>(&self) -> &'a [u8] ⓘ
Returns an immutable reference to the bitmap buffer backing this PdfiumBitmap.
This function does not attempt any color channel normalization.
Sourcepub fn as_rgba_bytes(&self) -> PdfiumResult<Vec<u8>>
pub fn as_rgba_bytes(&self) -> PdfiumResult<Vec<u8>>
Returns an owned copy of the bitmap buffer backing this PdfiumBitmap as RGBA.
Normalizing all color channels into RGBA irrespective of the original pixel format.
Sourcepub fn as_rgba8_image(&self) -> PdfiumResult<DynamicImage>
pub fn as_rgba8_image(&self) -> PdfiumResult<DynamicImage>
Returns a copy of this a bitmap as a DynamicImage::ImageRgba8
Sourcepub fn as_rgb8_image(&self) -> PdfiumResult<DynamicImage>
pub fn as_rgb8_image(&self) -> PdfiumResult<DynamicImage>
Returns a copy of this a bitmap as a DynamicImage::ImageRgb8
Sourcepub fn save(&self, path: &str, format: ImageFormat) -> PdfiumResult<()>
pub fn save(&self, path: &str, format: ImageFormat) -> PdfiumResult<()>
Saves this bitmap to the given path.
Include alpha channel only if the ImageFormat supports it.
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}Trait Implementations§
Source§impl Clone for PdfiumBitmap
impl Clone for PdfiumBitmap
Source§fn clone(&self) -> PdfiumBitmap
fn clone(&self) -> PdfiumBitmap
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more