pdf_oxide 0.3.38

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
//! Individual PDF/A validation functions.
//!
//! This module contains the validation logic for different aspects of PDF/A compliance.

use super::types::{
    ComplianceError, ComplianceWarning, ErrorCode, PdfALevel, ValidationResult, WarningCode,
};
use crate::document::PdfDocument;
use crate::error::Result;
use crate::object::Object;
use std::collections::HashMap;

/// Type alias for PDF dictionary.
type Dictionary = HashMap<String, Object>;

/// Helper to extract the catalog dictionary from a document.
///
/// Returns `None` if the catalog is not a dictionary, allowing callers
/// to handle this case appropriately.
fn get_catalog_dict(document: &mut PdfDocument) -> Result<Option<Dictionary>> {
    let catalog = document.catalog()?;
    match catalog {
        Object::Dictionary(d) => Ok(Some(d)),
        _ => Ok(None),
    }
}

/// Validate XMP metadata requirements.
pub fn validate_xmp_metadata(
    document: &mut PdfDocument,
    level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    // Get the catalog
    let catalog_dict = match get_catalog_dict(document)? {
        Some(d) => d,
        None => {
            result.add_error(ComplianceError::new(
                ErrorCode::MissingXmpMetadata,
                "Document catalog is invalid",
            ));
            return Ok(());
        },
    };

    // Check for /Metadata entry
    if !catalog_dict.contains_key("Metadata") {
        result.add_error(
            ComplianceError::new(
                ErrorCode::MissingXmpMetadata,
                "Document is missing XMP metadata stream",
            )
            .with_clause("6.7.2"),
        );
        return Ok(());
    }

    // Parse XMP metadata and check for PDF/A identification
    match crate::extractors::xmp::XmpExtractor::extract(document) {
        Ok(Some(xmp)) => {
            let part = xmp.custom.get("pdfaid:part");
            let conformance = xmp.custom.get("pdfaid:conformance");

            if part.is_none() {
                result.add_error(
                    ComplianceError::new(
                        ErrorCode::MissingXmpMetadata,
                        "XMP metadata missing pdfaid:part identification",
                    )
                    .with_clause("6.7.11"),
                );
            }

            if conformance.is_none() {
                result.add_error(
                    ComplianceError::new(
                        ErrorCode::MissingXmpMetadata,
                        "XMP metadata missing pdfaid:conformance identification",
                    )
                    .with_clause("6.7.11"),
                );
            }

            // Validate part matches declared level
            if let Some(part_val) = part {
                let expected_part = level.xmp_part();
                if part_val != expected_part {
                    result.add_warning(ComplianceWarning::new(
                        WarningCode::MissingRecommendedMetadata,
                        format!(
                            "XMP pdfaid:part is '{}' but validating against PDF/A-{} (expected '{}')",
                            part_val, expected_part, expected_part
                        ),
                    ));
                }
            }

            // Store detected level
            if let (Some(p), Some(c)) = (part, conformance) {
                result.detected_level = PdfALevel::from_xmp(p, c);
            }
        },
        Ok(None) => {
            result.add_error(
                ComplianceError::new(
                    ErrorCode::MissingXmpMetadata,
                    "Could not extract XMP metadata from document",
                )
                .with_clause("6.7.11"),
            );
        },
        Err(_) => {
            result.add_warning(ComplianceWarning::new(
                WarningCode::PartialCheck,
                "Failed to parse XMP metadata for PDF/A identification",
            ));
        },
    }

    Ok(())
}

/// Validate font embedding requirements.
///
/// Note: This is a simplified version that checks the document catalog.
/// Full font validation requires the rendering feature for page resource access.
pub fn validate_fonts(
    document: &mut PdfDocument,
    level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    // Font validation requires accessing page resources, which needs the rendering feature.
    // For now, we'll just note that fonts should be checked.
    // The full validation will be available when rendering feature is enabled.

    result.add_warning(ComplianceWarning::new(
        WarningCode::PartialCheck,
        "Font embedding check requires rendering feature for full validation",
    ));

    // Suppress unused variable warning
    let _ = level;
    let _ = document;

    Ok(())
}

/// Validate color space requirements.
pub fn validate_colors(
    document: &mut PdfDocument,
    _level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    // Check for output intent
    let catalog_dict = match get_catalog_dict(document)? {
        Some(d) => d,
        None => return Ok(()),
    };

    let has_output_intent = catalog_dict.contains_key("OutputIntents");

    if !has_output_intent {
        // Without output intent, device colors may not be allowed
        result.add_warning(ComplianceWarning::new(
            WarningCode::MissingRecommendedMetadata,
            "No output intent specified; device-dependent colors may cause issues",
        ));
    }

    // Validate color spaces in content streams
    let page_count = document.page_count()?;
    for page_idx in 0..page_count {
        let content_data = match document.get_page_content_data(page_idx) {
            Ok(data) => data,
            Err(_) => continue,
        };

        let operators = match crate::content::parser::parse_content_stream(&content_data) {
            Ok(ops) => ops,
            Err(_) => continue,
        };

        for op in &operators {
            let uses_device_color = matches!(
                op,
                crate::content::operators::Operator::SetFillRgb { .. }
                    | crate::content::operators::Operator::SetStrokeRgb { .. }
                    | crate::content::operators::Operator::SetFillCmyk { .. }
                    | crate::content::operators::Operator::SetStrokeCmyk { .. }
                    | crate::content::operators::Operator::SetFillGray { .. }
                    | crate::content::operators::Operator::SetStrokeGray { .. }
            );

            if uses_device_color && !has_output_intent {
                result.add_warning(ComplianceWarning::new(
                    WarningCode::MissingRecommendedMetadata,
                    format!(
                        "Page {} uses device-dependent color operators without output intent",
                        page_idx + 1
                    ),
                ));
                break; // One warning per page is sufficient
            }
        }
    }

    Ok(())
}

/// Validate encryption (must be absent).
///
/// PDF/A documents must not be encrypted.
pub fn validate_encryption(
    document: &mut PdfDocument,
    _level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    // Check the trailer for /Encrypt entry
    let trailer = document.trailer();
    let is_encrypted = if let Object::Dictionary(trailer_dict) = trailer {
        trailer_dict.contains_key("Encrypt")
    } else {
        false
    };

    if is_encrypted {
        result.add_error(
            ComplianceError::new(
                ErrorCode::EncryptionNotAllowed,
                "PDF/A documents must not be encrypted",
            )
            .with_clause("6.1.4"),
        );
    }
    Ok(())
}

/// Validate transparency usage (PDF/A-1 restriction).
///
/// Note: Full transparency validation requires the rendering feature.
pub fn validate_transparency(
    document: &mut PdfDocument,
    level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    if level.allows_transparency() {
        return Ok(());
    }

    // Full transparency check requires page resources (rendering feature).
    // For now, add a warning about partial checking.
    result.add_warning(ComplianceWarning::new(
        WarningCode::PartialCheck,
        "Full transparency validation requires rendering feature",
    ));

    // Suppress unused variable warning
    let _ = document;

    Ok(())
}

/// Validate document structure (for level A).
pub fn validate_structure(
    document: &mut PdfDocument,
    level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    if !level.requires_structure() {
        return Ok(());
    }

    let catalog_dict = match get_catalog_dict(document)? {
        Some(d) => d,
        None => return Ok(()),
    };

    // Check for MarkInfo with Marked = true
    let is_marked = if let Some(mark_info) = catalog_dict.get("MarkInfo") {
        let resolved_mark_info = document.resolve_references(mark_info, 1)?;
        if let Object::Dictionary(mi) = resolved_mark_info {
            matches!(mi.get("Marked"), Some(Object::Boolean(true)))
        } else {
            false
        }
    } else {
        false
    };

    if !is_marked {
        result.add_error(
            ComplianceError::new(
                ErrorCode::MissingDocumentStructure,
                "Document must be marked (Tagged PDF) for PDF/A level A conformance",
            )
            .with_clause("6.8"),
        );
    }

    // Check for StructTreeRoot
    if !catalog_dict.contains_key("StructTreeRoot") {
        result.add_error(
            ComplianceError::new(
                ErrorCode::MissingDocumentStructure,
                "Document must have a structure tree for PDF/A level A conformance",
            )
            .with_clause("6.8"),
        );
    }

    // Check for Lang (language specification)
    if !catalog_dict.contains_key("Lang") {
        result.add_error(
            ComplianceError::new(
                ErrorCode::MissingLanguage,
                "Document must specify a primary language for PDF/A level A conformance",
            )
            .with_clause("6.8.1"),
        );
    }

    Ok(())
}

/// Validate JavaScript (must be absent).
pub fn validate_javascript(
    document: &mut PdfDocument,
    _level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    let catalog_dict = match get_catalog_dict(document)? {
        Some(d) => d,
        None => return Ok(()),
    };

    // Check Names dictionary for JavaScript
    if let Some(names) = catalog_dict.get("Names") {
        let resolved_names = document.resolve_references(names, 1)?;
        if let Object::Dictionary(names_dict) = resolved_names {
            if names_dict.contains_key("JavaScript") {
                result.add_error(
                    ComplianceError::new(
                        ErrorCode::JavaScriptNotAllowed,
                        "JavaScript is not allowed in PDF/A documents",
                    )
                    .with_clause("6.6.1"),
                );
            }
        }
    }

    // Check for OpenAction with JavaScript
    if let Some(open_action) = catalog_dict.get("OpenAction") {
        let resolved_action = document.resolve_references(open_action, 1)?;
        if let Object::Dictionary(action) = resolved_action {
            if let Some(Object::Name(s)) = action.get("S") {
                if s == "JavaScript" {
                    result.add_error(
                        ComplianceError::new(
                            ErrorCode::JavaScriptNotAllowed,
                            "JavaScript OpenAction is not allowed in PDF/A documents",
                        )
                        .with_clause("6.6.1"),
                    );
                }
            }
        }
    }

    Ok(())
}

/// Validate embedded files (PDF/A-1/2 restriction, PDF/A-3 requirements).
pub fn validate_embedded_files(
    document: &mut PdfDocument,
    level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    let catalog_dict = match get_catalog_dict(document)? {
        Some(d) => d,
        None => return Ok(()),
    };

    // Check Names dictionary for EmbeddedFiles
    let has_embedded_files = if let Some(names) = catalog_dict.get("Names") {
        let resolved_names = document.resolve_references(names, 1)?;
        if let Object::Dictionary(names_dict) = resolved_names {
            names_dict.contains_key("EmbeddedFiles")
        } else {
            false
        }
    } else {
        false
    };

    if has_embedded_files {
        if !level.allows_embedded_files() {
            result.add_error(
                ComplianceError::new(
                    ErrorCode::EmbeddedFileNotAllowed,
                    format!("Embedded files are not allowed in {} (only PDF/A-3)", level),
                )
                .with_clause("6.9"),
            );
        } else {
            // For PDF/A-3, validate that each embedded file has AFRelationship
            if let Some(names) = catalog_dict.get("Names") {
                let resolved_names = document.resolve_references(names, 1)?;
                if let Object::Dictionary(names_dict) = resolved_names {
                    if let Some(ef) = names_dict.get("EmbeddedFiles") {
                        let resolved_ef = document.resolve_references(ef, 1)?;
                        if let Object::Dictionary(ef_dict) = resolved_ef {
                            if let Some(Object::Array(names_arr)) = ef_dict.get("Names") {
                                // Names array: [name1, filespec1, name2, filespec2, ...]
                                for (idx, item) in names_arr.iter().enumerate() {
                                    if idx % 2 == 1 {
                                        // This is a filespec
                                        let resolved_fs = document.resolve_references(item, 1)?;
                                        if let Object::Dictionary(fs_dict) = resolved_fs {
                                            if !fs_dict.contains_key("AFRelationship") {
                                                result.add_error(
                                                    ComplianceError::new(
                                                        ErrorCode::EmbeddedFileNotAllowed,
                                                        format!(
                                                            "Embedded file at index {} missing required AFRelationship",
                                                            idx / 2
                                                        ),
                                                    )
                                                    .with_clause("6.8"),
                                                );
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(())
}

/// Validate annotations.
///
/// Note: Full annotation validation requires page access. This checks catalog-level
/// info about annotations.
pub fn validate_annotations(
    document: &mut PdfDocument,
    _level: PdfALevel,
    result: &mut ValidationResult,
) -> Result<()> {
    // Full annotation validation requires page access which needs iterating through pages.
    // For now, add a note about partial checking.

    result.add_warning(ComplianceWarning::new(
        WarningCode::PartialCheck,
        "Full annotation validation requires page-level access",
    ));

    // Suppress unused variable warning
    let _ = document;

    Ok(())
}

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

    #[test]
    fn test_validation_result_updates() {
        let mut result = ValidationResult::new(PdfALevel::A2b);

        validate_encryption_mock(&mut result, false);
        assert!(!result.has_errors());

        validate_encryption_mock(&mut result, true);
        assert!(result.has_errors());
    }

    fn validate_encryption_mock(result: &mut ValidationResult, is_encrypted: bool) {
        if is_encrypted {
            result.add_error(ComplianceError::new(
                ErrorCode::EncryptionNotAllowed,
                "Document is encrypted",
            ));
        }
    }

    #[test]
    fn test_validation_result_new() {
        let result = ValidationResult::new(PdfALevel::A1a);
        assert_eq!(result.level, PdfALevel::A1a);
        assert!(!result.is_compliant);
        assert!(result.errors.is_empty());
        assert!(result.warnings.is_empty());
    }

    #[test]
    fn test_validation_result_add_warning() {
        let mut result = ValidationResult::new(PdfALevel::A2b);
        result.add_warning(ComplianceWarning::new(WarningCode::PartialCheck, "Partial check only"));
        assert!(!result.warnings.is_empty());
        assert!(!result.has_errors());
    }

    #[test]
    fn test_validation_result_add_multiple_errors() {
        let mut result = ValidationResult::new(PdfALevel::A1b);
        result.add_error(ComplianceError::new(ErrorCode::EncryptionNotAllowed, "Encrypted"));
        result.add_error(ComplianceError::new(ErrorCode::JavaScriptNotAllowed, "Has JavaScript"));
        assert_eq!(result.errors.len(), 2);
        assert!(result.has_errors());
    }

    #[test]
    fn test_compliance_error_with_clause() {
        let error =
            ComplianceError::new(ErrorCode::MissingXmpMetadata, "No XMP").with_clause("6.7.2");
        assert_eq!(error.code, ErrorCode::MissingXmpMetadata);
        assert_eq!(error.clause.as_deref(), Some("6.7.2"));
    }

    #[test]
    fn test_compliance_warning_codes() {
        let warn = ComplianceWarning::new(
            WarningCode::MissingRecommendedMetadata,
            "Missing recommended metadata",
        );
        assert_eq!(warn.code, WarningCode::MissingRecommendedMetadata);

        let warn2 = ComplianceWarning::new(WarningCode::PartialCheck, "Partial");
        assert_eq!(warn2.code, WarningCode::PartialCheck);
    }

    #[test]
    fn test_all_error_codes_debug() {
        let codes = vec![
            ErrorCode::MissingXmpMetadata,
            ErrorCode::MissingPdfaIdentification,
            ErrorCode::FontNotEmbedded,
            ErrorCode::MissingOutputIntent,
            ErrorCode::DeviceColorWithoutIntent,
            ErrorCode::JavaScriptNotAllowed,
            ErrorCode::EncryptionNotAllowed,
            ErrorCode::TransparencyNotAllowed,
            ErrorCode::EmbeddedFileNotAllowed,
            ErrorCode::MissingDocumentStructure,
            ErrorCode::MissingLanguage,
            ErrorCode::MissingAppearanceStream,
        ];
        for code in codes {
            let debug = format!("{:?}", code);
            assert!(!debug.is_empty());
        }
    }

    #[test]
    fn test_pdfa_level_allows_transparency() {
        assert!(!PdfALevel::A1a.allows_transparency());
        assert!(!PdfALevel::A1b.allows_transparency());
        assert!(PdfALevel::A2a.allows_transparency());
        assert!(PdfALevel::A2b.allows_transparency());
        assert!(PdfALevel::A3a.allows_transparency());
    }

    #[test]
    fn test_pdfa_level_requires_structure() {
        assert!(PdfALevel::A1a.requires_structure());
        assert!(!PdfALevel::A1b.requires_structure());
        assert!(PdfALevel::A2a.requires_structure());
        assert!(!PdfALevel::A2b.requires_structure());
        assert!(PdfALevel::A3a.requires_structure());
        assert!(!PdfALevel::A3b.requires_structure());
    }

    #[test]
    fn test_pdfa_level_allows_embedded_files() {
        assert!(!PdfALevel::A1a.allows_embedded_files());
        assert!(!PdfALevel::A1b.allows_embedded_files());
        assert!(!PdfALevel::A2a.allows_embedded_files());
        assert!(!PdfALevel::A2b.allows_embedded_files());
        assert!(PdfALevel::A3a.allows_embedded_files());
        assert!(PdfALevel::A3b.allows_embedded_files());
    }
}