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