pdfium_render/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod bindgen {
4    #![allow(non_upper_case_globals)]
5    #![allow(non_camel_case_types)]
6    #![allow(non_snake_case)]
7    #![allow(dead_code)]
8
9    // Select the Pdfium FPDF_* API version to use based on crate feature flags.
10
11    #[cfg(feature = "pdfium_future")]
12    include!("bindgen/pdfium_future.rs");
13
14    #[cfg(feature = "pdfium_7123")]
15    include!("bindgen/pdfium_7123.rs");
16
17    #[cfg(feature = "pdfium_6996")]
18    include!("bindgen/pdfium_6996.rs");
19
20    #[cfg(feature = "pdfium_6721")]
21    include!("bindgen/pdfium_6721.rs");
22
23    #[cfg(feature = "pdfium_6666")]
24    include!("bindgen/pdfium_6666.rs");
25
26    #[cfg(feature = "pdfium_6611")]
27    include!("bindgen/pdfium_6611.rs");
28
29    #[cfg(feature = "pdfium_6569")]
30    include!("bindgen/pdfium_6569.rs");
31
32    #[cfg(feature = "pdfium_6555")]
33    include!("bindgen/pdfium_6555.rs");
34
35    #[cfg(feature = "pdfium_6490")]
36    include!("bindgen/pdfium_6490.rs");
37
38    #[cfg(feature = "pdfium_6406")]
39    include!("bindgen/pdfium_6406.rs");
40
41    #[cfg(feature = "pdfium_6337")]
42    include!("bindgen/pdfium_6337.rs");
43
44    #[cfg(feature = "pdfium_6295")]
45    include!("bindgen/pdfium_6295.rs");
46
47    #[cfg(feature = "pdfium_6259")]
48    include!("bindgen/pdfium_6259.rs");
49
50    #[cfg(feature = "pdfium_6164")]
51    include!("bindgen/pdfium_6164.rs");
52
53    #[cfg(feature = "pdfium_6124")]
54    include!("bindgen/pdfium_6124.rs");
55
56    #[cfg(feature = "pdfium_6110")]
57    include!("bindgen/pdfium_6110.rs");
58
59    #[cfg(feature = "pdfium_6084")]
60    include!("bindgen/pdfium_6084.rs");
61
62    #[cfg(feature = "pdfium_6043")]
63    include!("bindgen/pdfium_6043.rs");
64
65    #[cfg(feature = "pdfium_6015")]
66    include!("bindgen/pdfium_6015.rs");
67
68    #[cfg(feature = "pdfium_5961")]
69    include!("bindgen/pdfium_5961.rs");
70
71    pub(crate) type size_t = usize;
72}
73
74mod bindings;
75mod error;
76mod pdf;
77mod pdfium;
78mod utils;
79
80/// A prelude for conveniently importing all public `pdfium-render` definitions at once.
81///
82/// Usage:
83/// ```
84/// use pdfium_render::prelude::*;
85/// ```
86pub mod prelude {
87    #[allow(deprecated)]
88    // TODO: AJRC - 5-Aug-24 - deprecated items will be removed in release 0.9.0. Tracking issue:
89    // https://github.com/ajrcarey/pdfium-render/issues/36
90    pub use crate::{
91        bindings::*,
92        error::*,
93        pdf::action::*,
94        pdf::appearance_mode::*,
95        pdf::bitmap::*,
96        pdf::color::*,
97        pdf::color_space::*,
98        pdf::destination::*,
99        pdf::document::attachment::*,
100        pdf::document::attachments::*,
101        pdf::document::bookmark::*,
102        pdf::document::bookmarks::*,
103        pdf::document::fonts::*,
104        pdf::document::form::*,
105        pdf::document::metadata::*,
106        pdf::document::page::annotation::attachment_points::*,
107        pdf::document::page::annotation::circle::*,
108        pdf::document::page::annotation::free_text::*,
109        pdf::document::page::annotation::highlight::*,
110        pdf::document::page::annotation::ink::*,
111        pdf::document::page::annotation::link::*,
112        pdf::document::page::annotation::objects::*,
113        pdf::document::page::annotation::popup::*,
114        pdf::document::page::annotation::redacted::*,
115        pdf::document::page::annotation::square::*,
116        pdf::document::page::annotation::squiggly::*,
117        pdf::document::page::annotation::stamp::*,
118        pdf::document::page::annotation::strikeout::*,
119        pdf::document::page::annotation::text::*,
120        pdf::document::page::annotation::underline::*,
121        pdf::document::page::annotation::unsupported::*,
122        pdf::document::page::annotation::widget::*,
123        pdf::document::page::annotation::xfa_widget::*,
124        pdf::document::page::annotation::{
125            PdfPageAnnotation, PdfPageAnnotationCommon, PdfPageAnnotationType,
126        },
127        pdf::document::page::annotations::*,
128        pdf::document::page::boundaries::*,
129        pdf::document::page::field::button::*,
130        pdf::document::page::field::checkbox::*,
131        pdf::document::page::field::combo::*,
132        pdf::document::page::field::list::*,
133        pdf::document::page::field::option::*,
134        pdf::document::page::field::options::*,
135        pdf::document::page::field::radio::*,
136        pdf::document::page::field::signature::*,
137        pdf::document::page::field::text::*,
138        pdf::document::page::field::unknown::*,
139        pdf::document::page::field::{PdfFormField, PdfFormFieldCommon, PdfFormFieldType},
140        pdf::document::page::links::*,
141        pdf::document::page::object::group::*,
142        pdf::document::page::object::image::*,
143        pdf::document::page::object::path::*,
144        pdf::document::page::object::shading::*,
145        pdf::document::page::object::text::*,
146        pdf::document::page::object::unsupported::*,
147        pdf::document::page::object::x_object_form::*,
148        pdf::document::page::object::{
149            PdfPageObject, PdfPageObjectBlendMode, PdfPageObjectCommon, PdfPageObjectLineCap,
150            PdfPageObjectLineJoin, PdfPageObjectType,
151        },
152        pdf::document::page::objects::common::*,
153        pdf::document::page::objects::*,
154        pdf::document::page::render_config::*,
155        pdf::document::page::size::*,
156        pdf::document::page::text::char::*,
157        pdf::document::page::text::chars::*,
158        pdf::document::page::text::search::*,
159        pdf::document::page::text::segment::*,
160        pdf::document::page::text::segments::*,
161        pdf::document::page::text::*,
162        pdf::document::page::{
163            PdfBitmapRotation, PdfPage, PdfPageContentRegenerationStrategy, PdfPageOrientation,
164            PdfPageRenderRotation,
165        },
166        pdf::document::pages::*,
167        pdf::document::permissions::*,
168        pdf::document::signature::*,
169        pdf::document::signatures::*,
170        pdf::document::{PdfDocument, PdfDocumentVersion},
171        pdf::font::glyph::*,
172        pdf::font::glyphs::*,
173        pdf::font::*,
174        pdf::link::*,
175        pdf::matrix::*,
176        pdf::path::segment::*,
177        pdf::path::segments::*,
178        pdf::points::*,
179        pdf::quad_points::*,
180        pdf::rect::*,
181        pdfium::*,
182    };
183}
184
185#[cfg(test)]
186mod tests {
187    use crate::prelude::*;
188    use crate::utils::test::test_bind_to_pdfium;
189    use image_025::ImageFormat;
190    use std::fs::File;
191    use std::path::Path;
192
193    #[test]
194    #[cfg(not(feature = "static"))]
195    fn test_readme_example() -> Result<(), PdfiumError> {
196        // Runs the code in the main example at the top of README.md.
197
198        fn export_pdf_to_jpegs(
199            path: &impl AsRef<Path>,
200            password: Option<&str>,
201        ) -> Result<(), PdfiumError> {
202            // Renders each page in the given test PDF file to a separate JPEG file.
203
204            // Bind to a Pdfium library in the same directory as our Rust executable.
205            // See the "Dynamic linking" section below.
206
207            let pdfium = Pdfium::default();
208
209            // Open the PDF document...
210
211            let document = pdfium.load_pdf_from_file(path, password)?;
212
213            // ... set rendering options that will apply to all pages...
214
215            let render_config = PdfRenderConfig::new()
216                .set_target_width(2000)
217                .set_maximum_height(2000)
218                .rotate_if_landscape(PdfPageRenderRotation::Degrees90, true);
219
220            // ... then render each page to a bitmap image, saving each image to a JPEG file.
221
222            for (index, page) in document.pages().iter().enumerate() {
223                page.render_with_config(&render_config)?
224                    .as_image() // Renders this page to an Image::DynamicImage...
225                    .into_rgb8() // ... then converts it to an Image::Image ...
226                    .save_with_format(format!("test-page-{}.jpg", index), ImageFormat::Jpeg) // ... and saves it to a file.
227                    .map_err(|_| PdfiumError::ImageError)?;
228            }
229
230            Ok(())
231        }
232
233        export_pdf_to_jpegs(&"./test/export-test.pdf", None)
234    }
235
236    #[test]
237    #[cfg(not(feature = "static"))]
238    fn test_dynamic_bindings() -> Result<(), PdfiumError> {
239        let pdfium = Pdfium::new(
240            Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./"))
241                .or_else(|_| Pdfium::bind_to_system_library())?,
242        );
243
244        let document = pdfium.load_pdf_from_file("./test/form-test.pdf", None)?;
245
246        let render_config = PdfRenderConfig::new()
247            .set_target_width(2000)
248            .set_maximum_height(2000)
249            .rotate_if_landscape(PdfPageRenderRotation::Degrees90, true)
250            .render_form_data(true)
251            .render_annotations(true);
252
253        for (index, page) in document.pages().iter().enumerate() {
254            let result = page
255                .render_with_config(&render_config)?
256                .as_image()
257                .into_rgb8()
258                .save_with_format(format!("form-test-page-{}.jpg", index), ImageFormat::Jpeg);
259
260            assert!(result.is_ok());
261        }
262
263        Ok(())
264    }
265
266    #[test]
267    #[cfg(feature = "static")]
268    fn test_static_bindings() {
269        // Simply checks that the static bindings contain no compilation errors.
270
271        Pdfium::bind_to_statically_linked_library().unwrap();
272    }
273
274    #[test]
275    fn test_reader_lifetime() -> Result<(), PdfiumError> {
276        // Confirms that a reader given to Pdfium::load_pdf_from_reader() does not need
277        // a lifetime longer than that of the PdfDocument it is used to create.
278
279        let pdfium = test_bind_to_pdfium();
280
281        let paths = ["test/form-test.pdf", "test/annotations-test.pdf"];
282
283        for path in paths {
284            let page_count = {
285                let reader = File::open(path).map_err(PdfiumError::IoError)?;
286
287                let document = pdfium.load_pdf_from_reader(reader, None)?;
288
289                document.pages().len()
290
291                // reader will be dropped here, immediately after document.
292            };
293
294            println!("{} has {} pages", path, page_count);
295        }
296
297        Ok(())
298    }
299}