export_pages/
export_pages.rs

1// PDFium-rs -- Modern Rust interface to PDFium, the PDF library from Google
2//
3// Copyright (c) 2025 Martin van der Werff <github (at) newinnovations.nl>
4//
5// This file is part of PDFium-rs.
6//
7// PDFium-rs is free software: you can redistribute it and/or modify it under the terms of
8// the GNU General Public License as published by the Free Software Foundation, either version 3
9// of the License, or (at your option) any later version.
10//
11// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
12// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
13// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
14// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
16// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
17// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20// Import all necessary items from the pdfium crate
21// This provides access to PDF document handling, rendering, and bitmap operations
22use pdfium::*;
23
24/// Exports all pages from a PDF document as individual JPEG images.
25///
26/// This function demonstrates how to:
27/// - Load a PDF document from disk
28/// - Iterate through all pages in the document
29/// - Render each page as a bitmap with specific dimensions and format
30/// - Save each rendered page as a separate image file
31///
32/// The function uses proper error handling with Result types, allowing
33/// errors to propagate up the call stack rather than panicking.
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}
87
88fn main() -> PdfiumResult<()> {
89    example_export_pages_to_images()?;
90    Ok(())
91}