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