Skip to main content

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