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