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