oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
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
//! PDF merging functionality
//!
//! This module provides functionality to merge multiple PDF documents into a single file.

use super::{OperationError, OperationResult, PageRange};
use crate::parser::{PdfDocument, PdfReader};
use crate::{Document, Page};
use std::fs::File;
use std::path::{Path, PathBuf};

/// Options for PDF merging
#[derive(Debug, Clone)]
pub struct MergeOptions {
    /// Page ranges to include from each input file
    pub page_ranges: Option<Vec<PageRange>>,
    /// Whether to preserve bookmarks/outlines
    pub preserve_bookmarks: bool,
    /// Whether to preserve form fields
    pub preserve_forms: bool,
    /// Whether to optimize the output
    pub optimize: bool,
    /// How to handle metadata
    pub metadata_mode: MetadataMode,
}

impl Default for MergeOptions {
    fn default() -> Self {
        Self {
            page_ranges: None,
            preserve_bookmarks: true,
            preserve_forms: false,
            optimize: false,
            metadata_mode: MetadataMode::FromFirst,
        }
    }
}

/// How to handle metadata when merging
#[derive(Debug, Clone)]
pub enum MetadataMode {
    /// Use metadata from the first document
    FromFirst,
    /// Use metadata from a specific document (by index)
    FromDocument(usize),
    /// Use custom metadata
    Custom {
        title: Option<String>,
        author: Option<String>,
        subject: Option<String>,
        keywords: Option<String>,
    },
    /// Don't set any metadata
    None,
}

/// Input specification for merging
#[derive(Debug)]
pub struct MergeInput {
    /// Path to the PDF file
    pub path: PathBuf,
    /// Optional page range to include
    pub pages: Option<PageRange>,
}

impl MergeInput {
    /// Create a new merge input that includes all pages
    pub fn new<P: Into<PathBuf>>(path: P) -> Self {
        Self {
            path: path.into(),
            pages: None,
        }
    }

    /// Create a merge input with specific pages
    pub fn with_pages<P: Into<PathBuf>>(path: P, pages: PageRange) -> Self {
        Self {
            path: path.into(),
            pages: Some(pages),
        }
    }
}

/// PDF merger
pub struct PdfMerger {
    inputs: Vec<MergeInput>,
    options: MergeOptions,
}

impl PdfMerger {
    /// Create a new PDF merger
    pub fn new(options: MergeOptions) -> Self {
        Self {
            inputs: Vec::new(),
            options,
        }
    }

    /// Add an input file to merge
    pub fn add_input(&mut self, input: MergeInput) {
        self.inputs.push(input);
    }

    /// Add multiple input files
    pub fn add_inputs(&mut self, inputs: impl IntoIterator<Item = MergeInput>) {
        self.inputs.extend(inputs);
    }

    /// Merge all input files into a single document
    pub fn merge(&mut self) -> OperationResult<Document> {
        if self.inputs.is_empty() {
            return Err(OperationError::NoPagesToProcess);
        }

        let mut output_doc = Document::new();

        // Process each input file
        for input_idx in 0..self.inputs.len() {
            let input_path = self.inputs[input_idx].path.clone();
            let input_pages = self.inputs[input_idx].pages.clone();

            let document = PdfReader::open_document(&input_path).map_err(|e| {
                OperationError::ParseError(format!(
                    "Failed to open {}: {}",
                    input_path.display(),
                    e
                ))
            })?;

            // Get page range
            let total_pages = document
                .page_count()
                .map_err(|e| OperationError::ParseError(e.to_string()))?
                as usize;

            let page_range = input_pages.as_ref().unwrap_or(&PageRange::All);

            let page_indices = page_range.get_indices(total_pages)?;

            // Extract and add pages
            for page_idx in page_indices {
                let parsed_page = document
                    .get_page(page_idx as u32)
                    .map_err(|e| OperationError::ParseError(e.to_string()))?;

                // Use Page::from_parsed_with_content to preserve original content streams
                // and resources (fonts, images, XObjects) instead of reconstructing pages
                let page = Page::from_parsed_with_content(&parsed_page, &document)
                    .map_err(|e| OperationError::ParseError(e.to_string()))?;
                output_doc.add_page(page);
            }

            // Handle metadata for the first document or specified document
            match &self.options.metadata_mode {
                MetadataMode::FromFirst if input_idx == 0 => {
                    self.copy_metadata(&document, &mut output_doc)?;
                }
                MetadataMode::FromDocument(idx) if input_idx == *idx => {
                    self.copy_metadata(&document, &mut output_doc)?;
                }
                _ => {}
            }
        }

        // Apply custom metadata if specified
        if let MetadataMode::Custom {
            title,
            author,
            subject,
            keywords,
        } = &self.options.metadata_mode
        {
            if let Some(title) = title {
                output_doc.set_title(title);
            }
            if let Some(author) = author {
                output_doc.set_author(author);
            }
            if let Some(subject) = subject {
                output_doc.set_subject(subject);
            }
            if let Some(keywords) = keywords {
                output_doc.set_keywords(keywords);
            }
        }

        Ok(output_doc)
    }

    /// Merge files and save to output path
    pub fn merge_to_file<P: AsRef<Path>>(&mut self, output_path: P) -> OperationResult<()> {
        let mut doc = self.merge()?;
        doc.save(output_path)?;
        Ok(())
    }

    /// Copy metadata from source to destination document
    fn copy_metadata(
        &self,
        document: &PdfDocument<File>,
        doc: &mut Document,
    ) -> OperationResult<()> {
        if let Ok(metadata) = document.metadata() {
            if let Some(title) = metadata.title {
                doc.set_title(&title);
            }
            if let Some(author) = metadata.author {
                doc.set_author(&author);
            }
            if let Some(subject) = metadata.subject {
                doc.set_subject(&subject);
            }
            if let Some(keywords) = metadata.keywords {
                doc.set_keywords(&keywords);
            }
        }
        Ok(())
    }
}

/// Merge multiple PDF files into one
pub fn merge_pdfs<P: AsRef<Path>>(
    inputs: Vec<MergeInput>,
    output_path: P,
    options: MergeOptions,
) -> OperationResult<()> {
    let mut merger = PdfMerger::new(options);
    merger.add_inputs(inputs);
    merger.merge_to_file(output_path)
}

/// Simple merge of multiple PDF files with default options
pub fn merge_pdf_files<P: AsRef<Path>, Q: AsRef<Path>>(
    input_paths: &[P],
    output_path: Q,
) -> OperationResult<()> {
    let inputs: Vec<MergeInput> = input_paths
        .iter()
        .map(|p| MergeInput::new(p.as_ref()))
        .collect();

    merge_pdfs(inputs, output_path, MergeOptions::default())
}

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

    #[test]
    fn test_merge_options_default() {
        let options = MergeOptions::default();
        assert!(options.page_ranges.is_none());
        assert!(options.preserve_bookmarks);
        assert!(!options.preserve_forms);
        assert!(!options.optimize);
        assert!(matches!(options.metadata_mode, MetadataMode::FromFirst));
    }

    #[test]
    fn test_merge_input_creation() {
        let input = MergeInput::new("test.pdf");
        assert_eq!(input.path, PathBuf::from("test.pdf"));
        assert!(input.pages.is_none());

        let input_with_pages = MergeInput::with_pages("test.pdf", PageRange::Range(0, 4));
        assert!(input_with_pages.pages.is_some());
    }

    // ============= Additional MergeOptions Tests =============

    #[test]
    fn test_merge_options_with_custom_metadata() {
        let options = MergeOptions {
            page_ranges: Some(vec![PageRange::All]),
            preserve_bookmarks: false,
            preserve_forms: true,
            optimize: true,
            metadata_mode: MetadataMode::Custom {
                title: Some("Merged Document".to_string()),
                author: Some("PDF Merger".to_string()),
                subject: Some("Combined PDFs".to_string()),
                keywords: Some("merge, pdf".to_string()),
            },
        };

        assert!(options.page_ranges.is_some());
        assert!(!options.preserve_bookmarks);
        assert!(options.preserve_forms);
        assert!(options.optimize);

        if let MetadataMode::Custom { title, .. } = options.metadata_mode {
            assert_eq!(title, Some("Merged Document".to_string()));
        } else {
            panic!("Expected Custom metadata mode");
        }
    }

    #[test]
    fn test_merge_options_from_document() {
        let options = MergeOptions {
            metadata_mode: MetadataMode::FromDocument(2),
            ..Default::default()
        };

        if let MetadataMode::FromDocument(idx) = options.metadata_mode {
            assert_eq!(idx, 2);
        } else {
            panic!("Expected FromDocument metadata mode");
        }
    }

    #[test]
    fn test_page_range_variants() {
        // Test All variant
        let all_pages = PageRange::All;
        assert!(matches!(all_pages, PageRange::All));

        // Test Single page
        let single = PageRange::Single(5);
        if let PageRange::Single(page) = single {
            assert_eq!(page, 5);
        } else {
            panic!("Expected Single page range");
        }

        // Test Range
        let range = PageRange::Range(1, 10);
        if let PageRange::Range(start, end) = range {
            assert_eq!(start, 1);
            assert_eq!(end, 10);
        } else {
            panic!("Expected Range");
        }

        // Test List
        let list = PageRange::List(vec![1, 3, 5, 7]);
        if let PageRange::List(pages) = list {
            assert_eq!(pages, vec![1, 3, 5, 7]);
        } else {
            panic!("Expected List");
        }
    }

    #[test]
    fn test_merge_input_with_all_pages() {
        let input = MergeInput::with_pages("document.pdf", PageRange::All);
        assert_eq!(input.path, PathBuf::from("document.pdf"));
        assert!(input.pages.is_some()); // Can't compare PageRange directly
    }

    #[test]
    fn test_merge_input_with_single_page() {
        let input = MergeInput::with_pages("document.pdf", PageRange::Single(0));
        assert_eq!(input.path, PathBuf::from("document.pdf"));
        assert!(input.pages.is_some()); // Can't compare PageRange directly
    }

    #[test]
    fn test_merge_input_with_page_list() {
        let pages = vec![0, 2, 4, 6];
        let input = MergeInput::with_pages("document.pdf", PageRange::List(pages));
        assert_eq!(input.path, PathBuf::from("document.pdf"));
        assert!(input.pages.is_some());
    }

    // Tests removed for PdfMerger methods that don't exist or have different signatures

    #[test]
    fn test_metadata_mode_all_variants() {
        // Test all MetadataMode variants
        let from_first = MetadataMode::FromFirst;
        assert!(matches!(from_first, MetadataMode::FromFirst));

        let from_doc = MetadataMode::FromDocument(3);
        assert!(matches!(from_doc, MetadataMode::FromDocument(3)));

        let custom = MetadataMode::Custom {
            title: Some("Title".to_string()),
            author: None,
            subject: None,
            keywords: None,
        };
        assert!(matches!(custom, MetadataMode::Custom { .. }));
    }

    // Removed test_merge_builder_pattern - PdfMerger methods not matching

    #[test]
    fn test_merge_options_builder() {
        let options = MergeOptions {
            page_ranges: Some(vec![
                PageRange::All,
                PageRange::Range(0, 5),
                PageRange::Single(10),
            ]),
            preserve_bookmarks: true,
            preserve_forms: true,
            optimize: true,
            metadata_mode: MetadataMode::FromFirst,
        };

        assert!(options.page_ranges.is_some());
        let ranges = options.page_ranges.unwrap();
        assert_eq!(ranges.len(), 3);
    }
}

#[cfg(test)]
#[path = "merge_tests.rs"]
mod merge_tests;