rustybara 0.1.2

Prepress-focused PDF manipulation library for graphic designers and print operators
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::encode::OutputFormat;
use crate::pages::PageBoxes;
use crate::raster::RenderConfig;
use crate::stream::{ColorRemap, ContentFilter};
use image::DynamicImage;
use lopdf::Document;
use std::path::Path;

/// A pipeline for processing and manipulating PDF documents.
///
/// The `PdfPipeline` struct provides a structured way to work with PDF files,
/// encapsulating a `Document` and offering methods to perform various operations
/// such as reading, modifying, and writing PDF content.
///
/// # Examples
///
/// ```no_test
/// // Create a new PDF pipeline
/// let pipeline = PdfPipeline::new();
///
/// // Load a PDF document
/// let doc = Document::load("example.pdf")?;
/// let pipeline = PdfPipeline::with_document(doc);
/// ```
pub struct PdfPipeline {
    doc: Document,
}

impl PdfPipeline {
    /// Opens a document from the specified file path.
    ///
    /// This function attempts to load a document from the given path and wraps it
    /// in a new instance of the containing struct.
    ///
    /// # Arguments
    ///
    /// * `path` - A path-like object that implements `AsRef<Path>` pointing to the document file
    ///
    /// # Returns
    ///
    /// * `Ok(Self)` - A new instance containing the loaded document
    /// * `Err(crate::Error)` - An error if the document could not be loaded
    ///
    /// # Examples
    ///
    /// ```no_test
    /// let document = MyStruct::open("path/to/document.txt")?;
    /// ```
    pub fn open(path: impl AsRef<Path>) -> crate::Result<Self> {
        let doc = Document::load(path)?;
        Ok(Self { doc })
    }

    /// Removes whitespace from the beginning and end of the document content.
    ///
    /// This method trims excess whitespace characters (spaces, tabs, newlines, etc.)
    /// from the outer boundaries of the document's content. It modifies the document
    /// in-place and returns a mutable reference to self for method chaining.
    ///
    /// # Returns
    ///
    /// Returns `Ok(&mut Self)` containing a mutable reference to the document if
    /// trimming succeeds, or an error if the trimming operation fails.
    ///
    /// # Errors
    ///
    /// Returns an error if the content filtering operation encounters issues
    /// while attempting to remove the outer whitespace.
    ///
    /// # Example
    ///
    /// ```no_test
    /// // Assuming `doc` is a mutable document instance
    /// doc.trim()?;
    /// ```
    pub fn trim(&mut self) -> crate::Result<&mut Self> {
        ContentFilter::remove_outside_trim(&mut self.doc)?;
        Ok(self)
    }

    /// Resizes the document's page boxes by applying bleed margins.
    ///
    /// This method adjusts the MediaBox (and optionally CropBox) of all pages in the document
    /// by expanding them outward by the specified bleed points. Bleed is extra space added
    /// around the edges of a page to ensure proper printing and trimming.
    ///
    /// # Arguments
    ///
    /// * `bleed_pts` - The amount of bleed margin to add in points (1/72 of an inch)
    ///
    /// # Returns
    ///
    /// Returns a mutable reference to self on success, or an error if page box operations fail.
    ///
    /// # Behavior
    ///
    /// For each page in the document:
    /// - Reads the current page boxes (MediaBox, CropBox, etc.)
    /// - Calculates a new media rectangle expanded by the bleed amount
    /// - Updates the MediaBox with the new dimensions
    /// - If the page has a CropBox, updates it to match the new MediaBox dimensions
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Failed to read page boxes from any page
    /// - Failed to access or modify page dictionary objects
    pub fn resize(&mut self, bleed_pts: f64) -> crate::Result<&mut Self> {
        let pages = self.doc.get_pages();
        for &page_id in pages.values() {
            let boxes = PageBoxes::read(&self.doc, page_id)?;
            let new_media = boxes.bleed_rect(bleed_pts).to_pdf_array();
            let page_dict = self.doc.get_dictionary_mut(page_id)?;
            let arr: Vec<lopdf::Object> = new_media.iter().map(|&v| v.into()).collect();
            let has_cropbox = page_dict.has(b"CropBox");
            page_dict.set(b"MediaBox", arr.clone());
            if has_cropbox {
                page_dict.set(b"CropBox", arr);
            }
        }
        Ok(self)
    }

    pub fn remap_color(
        &mut self,
        from: [f64; 4],
        to: [f64; 4],
        tolerance: f64,
    ) -> crate::Result<&mut Self> {
        let remaps = ColorRemap {
            from,
            to,
            tolerance,
        };
        ColorRemap::apply(&mut self.doc, &[remaps])?;
        Ok(self)
    }

    /// Returns the total number of pages in the document.
    ///
    /// This method retrieves the current page count by accessing the underlying
    /// document's page collection and returning its length.
    ///
    /// # Returns
    ///
    /// The number of pages as a `usize`. Returns 0 if the document is empty
    /// or contains no pages.
    ///
    /// # Examples
    ///
    /// ```no_test
    /// let doc = Document::new();
    /// assert_eq!(doc.page_count(), 0);
    ///
    /// // Add some pages...
    /// assert_eq!(doc.page_count(), 3);
    /// ```
    pub fn page_count(&self) -> usize {
        self.doc.get_pages().len()
    }

    /// Saves the current document as a PDF file to the specified path.
    ///
    /// This method serializes the document content and writes it to a PDF file
    /// at the given location. If the file already exists, it will be overwritten.
    ///
    /// # Arguments
    ///
    /// * `path` - The file path where the PDF should be saved. Can be any type
    ///   that implements `AsRef<Path>` (e.g., `&str`, `String`, `PathBuf`).
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the PDF was successfully saved, or an error if:
    /// - The document could not be serialized
    /// - There was an I/O error writing to the file
    /// - The path is invalid or inaccessible
    ///
    /// # Examples
    ///
    /// ```no_test
    /// // Save to a string path
    /// document.save_pdf("output.pdf")?;
    ///
    /// // Save to a PathBuf
    /// let path = std::path::PathBuf::from("documents/report.pdf");
    /// document.save_pdf(path)?;
    /// ```
    pub fn save_pdf(&mut self, path: impl AsRef<Path>) -> crate::Result<()> {
        self.doc.save(path)?;
        Ok(())
    }

    /// Renders a specific page from the PDF document as an image.
    ///
    /// This function takes a page number and rendering configuration, then generates
    /// a rasterized image of that page. The rendering is performed using Pdfium (the
    /// same engine used by Chrome for PDF rendering), which provides high-quality
    /// and accurate PDF rendering.
    ///
    /// # Arguments
    ///
    /// * `page_num` - The zero-based index of the page to render
    /// * `config` - A reference to `RenderConfig` containing rendering parameters
    ///   such as scale factor, rotation, and color options
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing either:
    /// * `Ok(DynamicImage)` - The rendered page as a dynamic image that can be
    ///   further processed or saved to various formats
    /// * `Err(crate::Error)` - An error if the rendering fails, which could be due
    ///   to invalid page numbers, PDF loading issues, or rendering problems
    ///
    /// # Platform Support
    ///
    /// The function automatically detects the operating system and loads the
    /// appropriate Pdfium library:
    /// * Windows: `pdfium.dll`
    /// * macOS: `libpdfium.dylib`  
    /// * Linux: `libpdfium.so`
    ///
    /// # Example
    ///
    /// ```no_test
    /// let config = RenderConfig::default();
    /// let image = pdf_renderer.render_page(0, &config)?;
    /// image.save("page_1.png")?;
    /// ```
    ///
    /// # Notes
    ///
    /// * The page numbering is zero-based (first page = 0)
    /// * The function clones the internal PDF document for rendering to avoid
    ///   borrowing conflicts
    /// * Pdfium library must be available at runtime in the same directory as
    ///   the executable
    pub fn render_page(&self, page_num: u32, config: &RenderConfig) -> crate::Result<DynamicImage> {
        use pdfium_render::prelude::*;

        let mut doc_clone = self.doc.clone();
        let mut buf = Vec::new();
        doc_clone.save_to(&mut buf).map_err(crate::Error::Io)?;

        let dylib_name = if cfg!(target_os = "windows") {
            "pdfium.dll"
        } else if cfg!(target_os = "macos") {
            "libpdfium.dylib"
        } else {
            "libpdfium.so" // Linux
        };

        let bindings_result = std::env::current_exe()
            .ok()
            .and_then(|p| p.parent().map(|p| p.join(dylib_name)))
            .and_then(|lib| Pdfium::bind_to_library(lib).ok())
            .map_or_else(|| Pdfium::bind_to_system_library(), Ok);

        let pdfium = match bindings_result {
            Ok(bindings) => Pdfium::new(bindings),
            Err(_) => Pdfium,
        };

        let pdf_doc = pdfium.load_pdf_from_byte_vec(buf, None)?;
        let page = pdf_doc.pages().get(page_num as PdfPageIndex)?;
        crate::raster::render_page(&page, config)
    }

    /// Saves a rendered page as an image file.
    ///
    /// This method renders a specific page from the document and saves it to the specified
    /// file path in the desired output format.
    ///
    /// # Arguments
    ///
    /// * `page_num` - The page number to render and save (0-indexed)
    /// * `path` - The file path where the image should be saved
    /// * `format` - The output format for the saved image (PNG, JPEG, etc.)
    /// * `config` - Rendering configuration specifying quality, resolution, and other parameters
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on successful save, or an error if rendering or encoding fails.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The page number is invalid
    /// - Rendering the page fails
    /// - Encoding or saving the image fails
    /// - File system operations fail
    ///
    /// # Example
    ///
    /// ```no_test
    /// use document_renderer::{RenderConfig, OutputFormat};
    ///
    /// let renderer = DocumentRenderer::new();
    /// let config = RenderConfig::default();
    /// let format = OutputFormat::Png;
    ///
    /// renderer.save_page_image(0, "output/page_1.png", &format, &config)?;
    /// ```
    pub fn save_page_image(
        &self,
        page_num: u32,
        path: impl AsRef<Path>,
        format: &OutputFormat,
        config: &RenderConfig,
    ) -> crate::Result<()> {
        let image = self.render_page(page_num, config)?;
        crate::encode::save(&image, path.as_ref(), format, config.dpi)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pages::PageBoxes;

    fn fixture() -> std::path::PathBuf {
        std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/pdf_test_data_print_v2.pdf")
    }

    #[test]
    fn open_and_page_count() {
        let p = PdfPipeline::open(fixture()).unwrap();
        assert!(p.page_count() > 0);
    }

    #[test]
    fn open_nonexistent_fails() {
        let err = PdfPipeline::open("no_such_file.pdf");
        assert!(err.is_err());
    }

    #[test]
    fn trim_succeeds() {
        let mut p = PdfPipeline::open(fixture()).unwrap();
        p.trim().unwrap();
    }

    #[test]
    fn trim_is_chainable() {
        let mut p = PdfPipeline::open(fixture()).unwrap();
        let out = std::env::temp_dir().join("rustybara_pipeline_trim_chain.pdf");
        p.trim().unwrap().save_pdf(&out).unwrap();
        assert!(out.exists());
        std::fs::remove_file(&out).ok();
    }

    #[test]
    fn resize_expands_mediabox() {
        let bleed = 9.0;
        let mut p = PdfPipeline::open(fixture()).unwrap();

        // Grab original trim dimensions for comparison
        let orig_doc = Document::load(fixture()).unwrap();
        let orig_pages = orig_doc.get_pages();
        let first_id = *orig_pages.values().next().unwrap();
        let orig_boxes = PageBoxes::read(&orig_doc, first_id).unwrap();
        let orig_trim = orig_boxes.trim_or_media();

        p.resize(bleed).unwrap();

        // Read back from the mutated doc
        let pages = p.doc.get_pages();
        let page_id = *pages.values().next().unwrap();
        let boxes = PageBoxes::read(&p.doc, page_id).unwrap();
        let media = boxes.media_box;

        assert!(
            (media.width - (orig_trim.width + 2.0 * bleed)).abs() < 0.01,
            "media width should be trim + 2*bleed"
        );
        assert!(
            (media.height - (orig_trim.height + 2.0 * bleed)).abs() < 0.01,
            "media height should be trim + 2*bleed"
        );
    }

    #[test]
    fn save_roundtrip() {
        let mut p = PdfPipeline::open(fixture()).unwrap();
        let original_count = p.page_count();
        let out = std::env::temp_dir().join("rustybara_pipeline_roundtrip.pdf");

        p.trim().unwrap().save_pdf(&out).unwrap();

        let reopened = PdfPipeline::open(&out).unwrap();
        assert_eq!(reopened.page_count(), original_count);
        std::fs::remove_file(&out).ok();
    }

    #[test]
    fn resize_then_save() {
        let mut p = PdfPipeline::open(fixture()).unwrap();
        let out = std::env::temp_dir().join("rustybara_pipeline_resize_save.pdf");
        p.resize(9.0).unwrap().save_pdf(&out).unwrap();
        assert!(out.exists());

        // Verify the saved file is loadable
        let reopened = PdfPipeline::open(&out).unwrap();
        assert!(reopened.page_count() > 0);
        std::fs::remove_file(&out).ok();
    }

    #[test]
    fn trim_then_resize_pipeline() {
        let mut p = PdfPipeline::open(fixture()).unwrap();
        let out = std::env::temp_dir().join("rustybara_pipeline_trim_resize.pdf");
        p.trim()
            .unwrap()
            .resize(9.0)
            .unwrap()
            .save_pdf(&out)
            .unwrap();
        assert!(out.exists());
        std::fs::remove_file(&out).ok();
    }

    #[test]
    #[ignore = "requires pdfium runtime library"]
    fn render_page_produces_image() {
        let p = PdfPipeline::open(fixture()).unwrap();
        let config = RenderConfig::default();
        let img = p.render_page(0, &config).unwrap();
        assert!(img.width() > 0);
        assert!(img.height() > 0);
    }
}