oxidize-pdf 2.4.2

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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! External PDF Validators
//!
//! This module integrates with external PDF validation tools to provide
//! REAL verification of ISO compliance. These tools are industry-standard
//! and provide the ground truth for PDF validity.

use crate::error::{PdfError, Result};
use crate::verification::ExternalValidationResult;
use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;

/// Validate PDF with external tools
pub fn validate_external(pdf_bytes: &[u8]) -> Result<ExternalValidationResult> {
    // Create temporary file for validation
    let mut temp_file = NamedTempFile::new().map_err(PdfError::Io)?;

    temp_file.write_all(pdf_bytes).map_err(PdfError::Io)?;

    let temp_path = temp_file.path();

    let mut result = ExternalValidationResult {
        qpdf_passed: None,
        verapdf_passed: None,
        adobe_preflight_passed: None,
        error_messages: Vec::new(),
    };

    // Validate with qpdf (most important)
    if let Some(path_str) = temp_path.to_str() {
        match validate_with_qpdf(path_str) {
            Ok(passed) => result.qpdf_passed = Some(passed),
            Err(e) => result.error_messages.push(format!("qpdf error: {}", e)),
        }

        // Validate with veraPDF if available
        match validate_with_verapdf(path_str) {
            Ok(passed) => result.verapdf_passed = Some(passed),
            Err(e) => result.error_messages.push(format!("veraPDF error: {}", e)),
        }

        // Adobe Preflight is typically not available in CI/dev environments
        // but we'll try if it exists
        match validate_with_adobe_preflight(path_str) {
            Ok(passed) => result.adobe_preflight_passed = Some(passed),
            Err(_) => {
                // Adobe Preflight not available - this is expected
                result
                    .error_messages
                    .push("Adobe Preflight not available".to_string());
            }
        }
    } else {
        result
            .error_messages
            .push("Path contains invalid UTF-8 characters".to_string());
    }

    Ok(result)
}

/// Validate PDF with qpdf
/// qpdf is the most reliable open-source PDF validator
pub fn validate_with_qpdf(pdf_path: &str) -> Result<bool> {
    let output = Command::new("qpdf")
        .arg("--check")
        .arg("--show-all-pages")
        .arg(pdf_path)
        .output();

    match output {
        Ok(output) => {
            if output.status.success() {
                Ok(true)
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                Err(PdfError::ExternalValidationError(format!(
                    "qpdf validation failed: {}",
                    stderr
                )))
            }
        }
        Err(e) => {
            if e.kind() == std::io::ErrorKind::NotFound {
                Err(PdfError::ExternalValidationError(
                    "qpdf not found. Install with: brew install qpdf".to_string(),
                ))
            } else {
                Err(PdfError::ExternalValidationError(format!(
                    "Failed to run qpdf: {}",
                    e
                )))
            }
        }
    }
}

/// Validate PDF with veraPDF
/// veraPDF is specifically designed for PDF/A validation
pub fn validate_with_verapdf(pdf_path: &str) -> Result<bool> {
    let output = Command::new("verapdf")
        .arg("--format")
        .arg("pdf")
        .arg("--flavour")
        .arg("1b") // PDF/A-1b validation
        .arg(pdf_path)
        .output();

    match output {
        Ok(output) => {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let stderr = String::from_utf8_lossy(&output.stderr);

            // veraPDF returns success even for invalid PDFs, so check output
            if stdout.contains("ValidationProfile") && !stdout.contains("failed") {
                Ok(true)
            } else {
                Err(PdfError::ExternalValidationError(format!(
                    "veraPDF validation failed: {}",
                    stderr
                )))
            }
        }
        Err(e) => {
            if e.kind() == std::io::ErrorKind::NotFound {
                Err(PdfError::ExternalValidationError(
                    "veraPDF not found. Download from: https://verapdf.org/".to_string(),
                ))
            } else {
                Err(PdfError::ExternalValidationError(format!(
                    "Failed to run veraPDF: {}",
                    e
                )))
            }
        }
    }
}

/// Validate PDF with Adobe Preflight (if available)
/// This is the gold standard but not available in most environments
pub fn validate_with_adobe_preflight(pdf_path: &str) -> Result<bool> {
    // Adobe Preflight is typically part of Adobe Acrobat Pro
    // It's not available in most CI/development environments
    // This is a placeholder for when it might be available

    let output = Command::new("acrobat")
        .arg("-preflight")
        .arg("ISO32000")
        .arg(pdf_path)
        .output();

    match output {
        Ok(output) => {
            if output.status.success() {
                Ok(true)
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                Err(PdfError::ExternalValidationError(format!(
                    "Adobe Preflight failed: {}",
                    stderr
                )))
            }
        }
        Err(_) => Err(PdfError::ExternalValidationError(
            "Adobe Preflight not available".to_string(),
        )),
    }
}

/// Additional validation with pdftk for structure checking
pub fn validate_with_pdftk(pdf_path: &str) -> Result<bool> {
    let output = Command::new("pdftk")
        .arg(pdf_path)
        .arg("dump_data")
        .output();

    match output {
        Ok(output) => {
            if output.status.success() {
                let stdout = String::from_utf8_lossy(&output.stdout);
                // Basic checks for PDF structure
                let has_info = stdout.contains("InfoKey:");
                let has_pages = stdout.contains("NumberOfPages:");
                Ok(has_info && has_pages)
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                Err(PdfError::ExternalValidationError(format!(
                    "pdftk validation failed: {}",
                    stderr
                )))
            }
        }
        Err(e) => {
            if e.kind() == std::io::ErrorKind::NotFound {
                Err(PdfError::ExternalValidationError(
                    "pdftk not found. Install with: brew install pdftk-java".to_string(),
                ))
            } else {
                Err(PdfError::ExternalValidationError(format!(
                    "Failed to run pdftk: {}",
                    e
                )))
            }
        }
    }
}

/// Check which external validators are available
pub fn check_available_validators() -> Vec<String> {
    let mut available = Vec::new();

    // Check qpdf
    if Command::new("qpdf").arg("--version").output().is_ok() {
        available.push("qpdf".to_string());
    }

    // Check veraPDF
    if Command::new("verapdf").arg("--version").output().is_ok() {
        available.push("verapdf".to_string());
    }

    // Check pdftk
    if Command::new("pdftk").arg("--version").output().is_ok() {
        available.push("pdftk".to_string());
    }

    // Check Adobe Acrobat (very unlikely)
    if Command::new("acrobat").arg("-help").output().is_ok() {
        available.push("adobe-acrobat".to_string());
    }

    available
}

/// Install instructions for missing validators
pub fn get_install_instructions() -> HashMap<String, String> {
    let mut instructions = HashMap::new();

    instructions.insert(
        "qpdf".to_string(),
        "Install qpdf:\n  macOS: brew install qpdf\n  Ubuntu: apt-get install qpdf\n  Windows: Download from https://qpdf.sourceforge.io/".to_string()
    );

    instructions.insert(
        "verapdf".to_string(),
        "Install veraPDF:\n  Download from https://verapdf.org/software/\n  Or use: brew install verapdf".to_string()
    );

    instructions.insert(
        "pdftk".to_string(),
        "Install pdftk:\n  macOS: brew install pdftk-java\n  Ubuntu: apt-get install pdftk\n  Windows: Download from https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/".to_string()
    );

    instructions
}

use std::collections::HashMap;

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

    fn create_minimal_pdf() -> Vec<u8> {
        // Create a minimal valid PDF for testing
        let pdf_content = r#"%PDF-1.4
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj

2 0 obj
<<
/Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj

3 0 obj
<<
/Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
>>
endobj

xref
0 4
0000000000 65535 f
0000000010 00000 n
0000000079 00000 n
0000000173 00000 n
trailer
<<
/Size 4
/Root 1 0 R
>>
startxref
256
%%EOF"#;
        pdf_content.as_bytes().to_vec()
    }

    fn create_invalid_pdf() -> Vec<u8> {
        // Create an invalid PDF (corrupted structure)
        b"not a valid pdf at all".to_vec()
    }

    fn create_truncated_pdf() -> Vec<u8> {
        // Create a truncated PDF missing the trailer
        b"%PDF-1.4\n1 0 obj\n<<\n/Type /Catalog\n>>\nendobj\n".to_vec()
    }

    #[test]
    fn test_check_available_validators() {
        let available = check_available_validators();
        // Just test that the function runs without panicking
        // The actual validators may or may not be available in test environment
        // Just ensure function runs without actual assertion on length
        let _ = available.len();
    }

    #[test]
    fn test_check_available_validators_returns_vec() {
        let available = check_available_validators();
        // Verify returns a Vec<String>
        for validator in &available {
            assert!(!validator.is_empty());
        }
    }

    #[test]
    fn test_get_install_instructions() {
        let instructions = get_install_instructions();
        assert!(instructions.contains_key("qpdf"));
        assert!(instructions.contains_key("verapdf"));
        assert!(instructions.contains_key("pdftk"));
        assert!(instructions["qpdf"].contains("brew install qpdf"));
    }

    #[test]
    fn test_get_install_instructions_verapdf_content() {
        let instructions = get_install_instructions();
        assert!(instructions["verapdf"].contains("https://verapdf.org/"));
    }

    #[test]
    fn test_get_install_instructions_pdftk_content() {
        let instructions = get_install_instructions();
        assert!(instructions["pdftk"].contains("pdftk-java"));
    }

    #[test]
    fn test_validate_external_with_mock_pdf() {
        let pdf_bytes = create_minimal_pdf();

        // This test will only succeed if qpdf is installed
        // In CI environments without qpdf, it should fail gracefully
        match validate_external(&pdf_bytes) {
            Ok(result) => {
                // If validation succeeds, check that we got some result
                assert!(
                    result.qpdf_passed.is_some()
                        || result.verapdf_passed.is_some()
                        || !result.error_messages.is_empty()
                );
            }
            Err(e) => {
                // If validation fails due to missing tools, that's expected
                tracing::debug!(
                    "External validation failed (expected in environments without PDF tools): {}",
                    e
                );
            }
        }
    }

    #[test]
    fn test_validate_external_result_structure() {
        let pdf_bytes = create_minimal_pdf();
        let result = validate_external(&pdf_bytes);

        // The function should always return Ok, even if validators fail
        assert!(result.is_ok());

        let validation_result = result.unwrap();
        // Error messages should always be populated (at least for missing tools)
        // This exercises the Ok path of validate_external
        let _ = validation_result.error_messages.len();
    }

    #[test]
    fn test_validate_with_qpdf_valid_pdf() {
        // Create a temporary file with valid PDF
        let pdf_bytes = create_minimal_pdf();
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&pdf_bytes).unwrap();
        let path = temp_file.path().to_str().unwrap();

        match validate_with_qpdf(path) {
            Ok(passed) => {
                // qpdf is available and validated successfully
                assert!(passed);
            }
            Err(e) => {
                // qpdf not installed - verify error message
                let err_str = e.to_string();
                assert!(
                    err_str.contains("not found") || err_str.contains("validation failed"),
                    "Unexpected error: {}",
                    err_str
                );
            }
        }
    }

    #[test]
    fn test_validate_with_qpdf_invalid_pdf() {
        // Create a temporary file with invalid PDF
        let pdf_bytes = create_invalid_pdf();
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&pdf_bytes).unwrap();
        let path = temp_file.path().to_str().unwrap();

        match validate_with_qpdf(path) {
            Ok(_) => {
                // qpdf might still "pass" on some invalid PDFs
            }
            Err(e) => {
                // Expected: qpdf rejects invalid PDF or qpdf not installed
                let err_str = e.to_string();
                assert!(
                    err_str.contains("validation failed") || err_str.contains("not found"),
                    "Unexpected error: {}",
                    err_str
                );
            }
        }
    }

    #[test]
    fn test_validate_with_qpdf_truncated_pdf() {
        // Create a temporary file with truncated PDF
        let pdf_bytes = create_truncated_pdf();
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&pdf_bytes).unwrap();
        let path = temp_file.path().to_str().unwrap();

        match validate_with_qpdf(path) {
            Ok(_) => {
                // Truncated PDFs might pass basic validation
            }
            Err(e) => {
                let err_str = e.to_string();
                assert!(
                    err_str.contains("validation failed") || err_str.contains("not found"),
                    "Unexpected error: {}",
                    err_str
                );
            }
        }
    }

    #[test]
    fn test_validate_with_qpdf_nonexistent_file() {
        let result = validate_with_qpdf("/nonexistent/path/to/file.pdf");

        // Should return an error
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        // Either qpdf not found or file not found error
        assert!(
            err_str.contains("not found") || err_str.contains("failed"),
            "Unexpected error: {}",
            err_str
        );
    }

    #[test]
    fn test_validate_with_verapdf_not_available() {
        // veraPDF is typically not installed on most systems
        let pdf_bytes = create_minimal_pdf();
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&pdf_bytes).unwrap();
        let path = temp_file.path().to_str().unwrap();

        match validate_with_verapdf(path) {
            Ok(passed) => {
                // veraPDF is available - verify it returns a boolean
                let _ = passed;
            }
            Err(e) => {
                // Expected: veraPDF not installed
                let err_str = e.to_string();
                assert!(
                    err_str.contains("not found") || err_str.contains("validation failed"),
                    "Unexpected error: {}",
                    err_str
                );
            }
        }
    }

    #[test]
    fn test_validate_with_adobe_preflight_not_available() {
        // Adobe Preflight is almost never available
        let pdf_bytes = create_minimal_pdf();
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&pdf_bytes).unwrap();
        let path = temp_file.path().to_str().unwrap();

        let result = validate_with_adobe_preflight(path);

        // Adobe Preflight is almost never installed
        // It should return an error
        match result {
            Ok(_) => {
                // Rare case: Adobe Acrobat is installed
            }
            Err(e) => {
                let err_str = e.to_string();
                assert!(
                    err_str.contains("not available") || err_str.contains("failed"),
                    "Unexpected error: {}",
                    err_str
                );
            }
        }
    }

    #[test]
    fn test_validate_with_pdftk_not_available() {
        // pdftk may or may not be installed
        let pdf_bytes = create_minimal_pdf();
        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(&pdf_bytes).unwrap();
        let path = temp_file.path().to_str().unwrap();

        match validate_with_pdftk(path) {
            Ok(passed) => {
                // pdftk is available - it checks for InfoKey and NumberOfPages
                // A minimal PDF might not have both
                let _ = passed;
            }
            Err(e) => {
                let err_str = e.to_string();
                assert!(
                    err_str.contains("not found") || err_str.contains("validation failed"),
                    "Unexpected error: {}",
                    err_str
                );
            }
        }
    }

    #[test]
    fn test_validate_with_pdftk_nonexistent_file() {
        let result = validate_with_pdftk("/nonexistent/path/to/file.pdf");

        // Should return an error
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_external_empty_pdf() {
        let pdf_bytes = Vec::new();
        let result = validate_external(&pdf_bytes);

        // Should return Ok with error messages about failed validations
        assert!(result.is_ok());
        let validation_result = result.unwrap();
        // At least Adobe Preflight should be in error messages (always unavailable)
        assert!(!validation_result.error_messages.is_empty());
    }

    #[test]
    fn test_external_validation_result_fields() {
        let pdf_bytes = create_minimal_pdf();
        let result = validate_external(&pdf_bytes).unwrap();

        // Test that all fields are accessible
        let _ = result.qpdf_passed;
        let _ = result.verapdf_passed;
        let _ = result.adobe_preflight_passed;
        let _ = result.error_messages.len();
    }

    #[test]
    fn test_install_instructions_completeness() {
        let instructions = get_install_instructions();

        // Should have exactly 3 validators
        assert_eq!(instructions.len(), 3);

        // Each should have non-empty instructions
        for (name, instruction) in &instructions {
            assert!(!name.is_empty());
            assert!(!instruction.is_empty());
            // Each should have at least one installation method
            assert!(
                instruction.contains("brew")
                    || instruction.contains("apt")
                    || instruction.contains("Download"),
                "Instructions for {} don't contain installation method",
                name
            );
        }
    }
}