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::provider::*,
185        pdf::font::*,
186        pdf::link::*,
187        pdf::matrix::*,
188        pdf::path::clip_path::*,
189        pdf::path::segment::*,
190        pdf::path::segments::*,
191        pdf::points::*,
192        pdf::quad_points::*,
193        pdf::rect::*,
194        pdfium::*,
195    };
196}
197
198#[cfg(test)]
199mod tests {
200    use crate::prelude::*;
201    use crate::utils::test::test_bind_to_pdfium;
202    use image_025::ImageFormat;
203    use std::fs::File;
204    use std::path::Path;
205
206    #[test]
207    #[cfg(not(feature = "static"))]
208    fn test_readme_example() -> Result<(), PdfiumError> {
209        // Runs the code in the main example at the top of README.md.
210
211        fn export_pdf_to_jpegs(
212            path: &impl AsRef<Path>,
213            password: Option<&str>,
214        ) -> Result<(), PdfiumError> {
215            // Renders each page in the given test PDF file to a separate JPEG file.
216
217            // Bind to a Pdfium library in the same directory as our Rust executable.
218            // See the "Dynamic linking" section below.
219
220            let pdfium = Pdfium::default();
221
222            // Open the PDF document...
223
224            let document = pdfium.load_pdf_from_file(path, password)?;
225
226            // ... set rendering options that will apply to all pages...
227
228            let render_config = PdfRenderConfig::new()
229                .set_target_width(2000)
230                .set_maximum_height(2000)
231                .rotate_if_landscape(PdfPageRenderRotation::Degrees90, true);
232
233            // ... then render each page to a bitmap image, saving each image to a JPEG file.
234
235            for (index, page) in document.pages().iter().enumerate() {
236                page.render_with_config(&render_config)?
237                    .as_image()? // Renders this page to an Image::DynamicImage...
238                    .into_rgb8() // ... then converts it to an Image::Image ...
239                    .save_with_format(format!("test-page-{}.jpg", index), ImageFormat::Jpeg) // ... and saves it to a file.
240                    .map_err(|_| PdfiumError::ImageError)?;
241            }
242
243            Ok(())
244        }
245
246        export_pdf_to_jpegs(&"./test/export-test.pdf", None)
247    }
248
249    #[test]
250    #[cfg(not(feature = "static"))]
251    fn test_dynamic_bindings() -> Result<(), PdfiumError> {
252        let pdfium = Pdfium::default();
253
254        let document = pdfium.load_pdf_from_file("./test/form-test.pdf", None)?;
255
256        let render_config = PdfRenderConfig::new()
257            .set_target_width(2000)
258            .set_maximum_height(2000)
259            .rotate_if_landscape(PdfPageRenderRotation::Degrees90, true)
260            .render_form_data(true)
261            .render_annotations(true);
262
263        for (index, page) in document.pages().iter().enumerate() {
264            let result = page
265                .render_with_config(&render_config)?
266                .as_image()?
267                .into_rgb8()
268                .save_with_format(format!("form-test-page-{}.jpg", index), ImageFormat::Jpeg);
269
270            assert!(result.is_ok());
271        }
272
273        Ok(())
274    }
275
276    #[test]
277    #[cfg(feature = "static")]
278    fn test_static_bindings() {
279        // Simply checks that the static bindings contain no compilation errors.
280
281        Pdfium::bind_to_statically_linked_library().unwrap();
282    }
283
284    #[test]
285    fn test_reader_lifetime() -> Result<(), PdfiumError> {
286        // Confirms that a reader given to Pdfium::load_pdf_from_reader() does not need
287        // a lifetime longer than that of the PdfDocument it is used to create.
288
289        let pdfium = test_bind_to_pdfium();
290
291        let paths = ["test/form-test.pdf", "test/annotations-test.pdf"];
292
293        for path in paths {
294            let page_count = {
295                let reader = File::open(path).map_err(PdfiumError::IoError)?;
296
297                let document = pdfium.load_pdf_from_reader(reader, None)?;
298
299                document.pages().len()
300
301                // reader will be dropped here, immediately after document.
302            };
303
304            println!("{} has {} pages", path, page_count);
305        }
306
307        Ok(())
308    }
309}