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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! ISO Section 7.1-7.4: File Structure Tests
//!
//! Tests for basic PDF file structure: header, body, cross-reference table,
//! and trailer as defined in ISO 32000-1:2008 Sections 7.1-7.4

use super::super::{
    create_basic_test_pdf, get_available_validators, iso_test, run_external_validation,
};
use crate::verification::{parser::parse_pdf, VerificationLevel};
use crate::{Document, Font, Page, Result as PdfResult};

iso_test!(
    test_pdf_header_level_2,
    "7.1.1",
    VerificationLevel::GeneratesPdf,
    "PDF header Level 2 verification",
    {
        let pdf_bytes = create_basic_test_pdf("Header Test", "Testing PDF header compliance")?;

        // Check if PDF starts with correct header
        let pdf_string = String::from_utf8_lossy(&pdf_bytes[..20]);
        let has_header = pdf_string.starts_with("%PDF-");

        let passed = has_header && pdf_bytes.len() > 1000;
        let level_achieved = if passed { 2 } else { 1 };
        let notes = if passed {
            format!("PDF header valid: {}", &pdf_string[..8])
        } else {
            "PDF header missing or invalid format".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_pdf_header_level_3,
    "7.347",
    VerificationLevel::ContentVerified,
    "PDF header format and version compliance verification",
    {
        let pdf_bytes =
            create_basic_test_pdf("Header Version Test", "Testing PDF header version format")?;

        // Verify header format at byte level (ISO 32000-1:2008 Section 7.5.2)
        let header_line = if let Some(newline_pos) = pdf_bytes.iter().position(|&b| b == b'\n') {
            String::from_utf8_lossy(&pdf_bytes[..newline_pos])
        } else {
            String::from_utf8_lossy(&pdf_bytes[..std::cmp::min(20, pdf_bytes.len())])
        };

        // Check ISO requirements for PDF header
        let header_valid = header_line.starts_with("%PDF-") && header_line.len() >= 8;
        let version_valid = if header_valid {
            let version_part = &header_line[5..]; // After "%PDF-"
            version_part.len() >= 3
                && version_part.chars().nth(1) == Some('.')
                && version_part[..3]
                    .chars()
                    .all(|c| c.is_ascii_digit() || c == '.')
        } else {
            false
        };

        // Parse and verify internal structure
        let parsed = parse_pdf(&pdf_bytes)?;
        let internal_version_valid = parsed.version.len() >= 3
            && parsed.version.chars().nth(1) == Some('.')
            && parsed
                .version
                .chars()
                .nth(0)
                .map_or(false, |c| c.is_ascii_digit())
            && parsed
                .version
                .chars()
                .nth(2)
                .map_or(false, |c| c.is_ascii_digit());

        // Final validation - all components must pass for Level 3
        let all_valid = header_valid && version_valid && internal_version_valid;

        let passed = all_valid;
        let level_achieved = if all_valid {
            3
        } else if header_valid && version_valid {
            2 // Header is valid but internal parsing might have issues
        } else {
            1 // Basic PDF generation works but header is wrong
        };

        let notes = if all_valid {
            format!(
                "PDF header fully compliant: '{}', internal version: '{}'",
                header_line.trim(),
                parsed.version
            )
        } else if !header_valid {
            format!("Invalid PDF header format: '{}'", header_line.trim())
        } else if !version_valid {
            format!("Invalid version in header: '{}'", header_line.trim())
        } else {
            format!(
                "Header valid but internal version parsing failed: internal='{}', header='{}'",
                parsed.version,
                header_line.trim()
            )
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_pdf_eof_marker_level_3,
    "7.127",
    VerificationLevel::ContentVerified,
    "PDF EOF marker Level 3 content verification with parsing validation",
    {
        let pdf_bytes = create_basic_test_pdf(
            "EOF Marker Level 3 Test",
            "Testing PDF end-of-file marker compliance with ISO 32000-1:2008",
        )?;

        // Level 3 verification: parse and verify complete structure
        let parsed = parse_pdf(&pdf_bytes)?;

        let has_sufficient_objects = parsed.object_count >= 4;
        let has_catalog = parsed.catalog.is_some();
        let has_page_tree = parsed.page_tree.is_some();
        let has_sufficient_content = pdf_bytes.len() > 1000;
        let has_pdf_header = pdf_bytes.starts_with(b"%PDF-");
        let has_eof_marker = pdf_bytes.windows(5).any(|w| w == b"%%EOF");
        let has_xref = pdf_bytes.windows(4).any(|w| w == b"xref");

        // Additional EOF marker verification
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let eof_at_end = pdf_string.trim_end().ends_with("%%EOF");

        let all_checks_passed = has_sufficient_objects
            && has_catalog
            && has_page_tree
            && has_sufficient_content
            && has_pdf_header
            && has_eof_marker
            && has_xref
            && eof_at_end;

        let passed = all_checks_passed;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("EOF marker fully compliant: {} objects, catalog: {}, page_tree: {}, content: {} bytes, structure: valid",
                parsed.object_count, has_catalog, has_page_tree, pdf_bytes.len())
        } else {
            format!(
                "Level 3 verification failed - objects: {}, catalog: {}, content: {} bytes",
                parsed.object_count,
                has_catalog,
                pdf_bytes.len()
            )
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_cross_reference_table_level_3,
    "7.391",
    VerificationLevel::ContentVerified,
    "Cross-reference table structure and xref keyword verification per ISO 32000-1:2008",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Cross-Reference Table Test",
            "Testing cross-reference table structure and xref keyword compliance",
        )?;

        // Level 3 verification: Parse and verify cross-reference table structure
        let parsed = parse_pdf(&pdf_bytes)?;

        // ISO requirement validation: xref table must be valid
        let xref_valid = parsed.xref_valid;

        // ISO requirement: must have objects in the xref table
        let has_objects = parsed.object_count > 0;

        // Additional validation: check for xref keyword in PDF bytes
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_xref_keyword = pdf_string.contains("xref");

        // Check for startxref keyword (ISO requirement)
        let has_startxref = pdf_string.contains("startxref");

        // Final Level 3 validation - all ISO requirements must be met
        let all_checks_passed = xref_valid && has_objects && has_xref_keyword && has_startxref;

        let level_achieved = if all_checks_passed {
            3
        } else if xref_valid && has_objects {
            2 // Basic xref structure valid but missing keywords
        } else if has_objects {
            1 // Objects exist but xref structure invalid
        } else {
            0 // No valid PDF structure
        };

        let notes = if all_checks_passed {
            format!(
                "Cross-reference table fully compliant: {} objects, xref valid, keywords present",
                parsed.object_count
            )
        } else if !xref_valid {
            format!(
                "Invalid cross-reference table structure (objects: {}, xref: {}, startxref: {})",
                parsed.object_count, has_xref_keyword, has_startxref
            )
        } else if !has_xref_keyword {
            "Cross-reference structure valid but missing 'xref' keyword".to_string()
        } else if !has_startxref {
            "Cross-reference structure valid but missing 'startxref' keyword".to_string()
        } else {
            format!(
                "Partial compliance: objects={}, xref_valid={}",
                parsed.object_count, xref_valid
            )
        };

        let passed = all_checks_passed;

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_object_structure_level_2,
    "7.3",
    VerificationLevel::GeneratesPdf,
    "Test passed".to_string(),
    {
        let pdf_bytes = create_basic_test_pdf(
            "Object Structure Test",
            "Testing PDF object structure compliance",
        )?;

        // Basic verification - PDF should contain objects
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_objects = pdf_string.contains(" obj") && pdf_string.contains("endobj");

        let passed = has_objects && pdf_bytes.len() > 1000;
        let level_achieved = if passed { 2 } else { 1 };
        let notes = if passed {
            "Test passed".to_string()
        } else {
            "Test failed - implementation error".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_object_structure_level_3,
    "7.3",
    VerificationLevel::ContentVerified,
    "Verify object numbering and reference integrity",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Object Integrity Test",
            "Testing PDF object integrity and references",
        )?;

        // Parse and count objects
        let parsed = parse_pdf(&pdf_bytes)?;

        // Verify we have essential objects
        let has_catalog = parsed.catalog.is_some();
        let has_pages = parsed.page_tree.is_some();
        let sufficient_objects = parsed.object_count >= 3; // At minimum: catalog, pages, page

        let passed = has_catalog && has_pages && sufficient_objects;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!(
                "Object structure valid: {} objects with catalog and pages",
                parsed.object_count
            )
        } else {
            "Test failed - implementation error".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_trailer_structure_level_3,
    "7.431",
    VerificationLevel::ContentVerified,
    "PDF trailer structure Level 3 verification with parsing validation",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Trailer Structure Level 3 Test",
            "Testing PDF trailer structure compliance with ISO 32000-1:2008",
        )?;

        // Level 3 verification: parse and verify complete structure
        let parsed = parse_pdf(&pdf_bytes)?;

        let has_sufficient_objects = parsed.object_count >= 4;
        let has_catalog = parsed.catalog.is_some();
        let has_page_tree = parsed.page_tree.is_some();
        let has_sufficient_content = pdf_bytes.len() > 1000;
        let has_pdf_header = pdf_bytes.starts_with(b"%PDF-");
        let has_eof_marker = pdf_bytes.windows(5).any(|w| w == b"%%EOF");
        let has_xref = pdf_bytes.windows(4).any(|w| w == b"xref");

        // ISO trailer validation
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_trailer = pdf_string.contains("trailer");
        let has_startxref = pdf_string.contains("startxref");

        let all_checks_passed = has_sufficient_objects
            && has_catalog
            && has_page_tree
            && has_sufficient_content
            && has_pdf_header
            && has_eof_marker
            && has_xref
            && has_trailer
            && has_startxref;

        let passed = all_checks_passed;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!("Trailer structure fully compliant: {} objects, catalog: {}, page_tree: {}, content: {} bytes, trailer: {}, startxref: {}",
                parsed.object_count, has_catalog, has_page_tree, pdf_bytes.len(), has_trailer, has_startxref)
        } else {
            format!("Level 3 verification failed - objects: {}, catalog: {}, trailer: {}, startxref: {}",
                parsed.object_count, has_catalog, has_trailer, has_startxref)
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_complete_file_structure_level_4,
    "7.1-7.4",
    VerificationLevel::IsoCompliant,
    "Complete PDF file structure validation with external tools",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Complete Structure Test",
            "Testing complete PDF file structure for ISO compliance",
        )?;

        // First verify internal parsing works (Level 3)
        let parsed = parse_pdf(&pdf_bytes)?;
        let internal_valid =
            parsed.xref_valid && parsed.catalog.is_some() && parsed.object_count > 0;

        let (passed, level_achieved, notes) = if !internal_valid {
            (false, 3, "Test failed - implementation error".to_string())
        } else {
            // Try external validation for Level 4
            let validators = get_available_validators();
            if validators.is_empty() {
                (
                    true,
                    3,
                    "Level 3 achieved - no external validators available".to_string(),
                )
            } else {
                // Try qpdf validation first
                if let Some(qpdf_result) = run_external_validation(&pdf_bytes, "qpdf") {
                    let passed = qpdf_result;
                    let level_achieved = if passed { 4 } else { 3 };
                    let notes = if passed {
                        "Test passed".to_string()
                    } else {
                        "Test failed - implementation error".to_string()
                    };
                    (passed, level_achieved, notes)
                } else {
                    // Fallback to Level 3 if external validation unavailable
                    (
                        true,
                        3,
                        "Level 3 achieved - external validation unavailable".to_string(),
                    )
                }
            }
        };

        Ok((passed, level_achieved, notes))
    }
);

// Additional critical file structure tests

iso_test!(
    test_pdf_version_consistency_level_3,
    "7.2.1.1",
    VerificationLevel::ContentVerified,
    "PDF version in header and catalog must be consistent",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Version Consistency Test",
            "Testing PDF version consistency between header and catalog",
        )?;

        // Parse header version
        let header_line = if let Some(newline_pos) = pdf_bytes.iter().position(|&b| b == b'\n') {
            String::from_utf8_lossy(&pdf_bytes[..newline_pos])
        } else {
            String::from_utf8_lossy(&pdf_bytes[..std::cmp::min(20, pdf_bytes.len())])
        };

        let header_version = if header_line.starts_with("%PDF-") && header_line.len() >= 8 {
            header_line[5..8].to_string()
        } else {
            "invalid".to_string()
        };

        // Parse internal version
        let parsed = parse_pdf(&pdf_bytes)?;
        let internal_version = &parsed.version;

        // Check consistency
        let versions_consistent = header_version == *internal_version
            || header_version.starts_with(&internal_version[..3]);
        let header_valid = header_version != "invalid";
        let internal_valid = internal_version.len() >= 3;

        let passed = versions_consistent && header_valid && internal_valid;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!(
                "Version consistency verified: header '{}', internal '{}'",
                header_version, internal_version
            )
        } else {
            format!(
                "Version inconsistency: header '{}', internal '{}', consistent: {}",
                header_version, internal_version, versions_consistent
            )
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_linearized_pdf_level_0,
    "7.2.1.2",
    VerificationLevel::NotImplemented,
    "Linearized PDF structure for fast web view",
    {
        // Linearized PDFs are not implemented
        let passed = false;
        let level_achieved = 0;
        let notes = "Linearized PDF structure not implemented".to_string();

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_object_streams_level_0,
    "7.2.2.1",
    VerificationLevel::NotImplemented,
    "Object streams for PDF compression (PDF 1.5+)",
    {
        // Object streams are not implemented
        let passed = false;
        let level_achieved = 0;
        let notes = "Object streams (PDF 1.5+) not implemented".to_string();

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_xref_streams_level_0,
    "7.2.2.2",
    VerificationLevel::NotImplemented,
    "Cross-reference streams instead of traditional xref table",
    {
        // XRef streams are not implemented
        let passed = false;
        let level_achieved = 0;
        let notes =
            "Cross-reference streams not implemented - using traditional xref table".to_string();

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_indirect_objects_level_3,
    "7.2.3.1",
    VerificationLevel::ContentVerified,
    "Indirect objects must be properly numbered and referenced",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Indirect Objects Test",
            "Testing proper indirect object numbering and referencing",
        )?;

        // Check for proper object structure in PDF content
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);

        // Look for object definitions (simple pattern matching)
        let obj_count = pdf_string.matches(" obj").count();

        // Look for indirect references (simple pattern matching)
        let ref_count = pdf_string.matches(" R").count();

        // Parse and verify internal structure
        let parsed = parse_pdf(&pdf_bytes)?;
        let has_sufficient_objects = parsed.object_count >= 3;

        let passed = obj_count >= 3 && ref_count >= 1 && has_sufficient_objects;
        let level_achieved = if passed { 3 } else { 2 };
        let notes = if passed {
            format!(
                "Indirect objects valid: {} objects defined, {} references, {} parsed objects",
                obj_count, ref_count, parsed.object_count
            )
        } else {
            format!(
                "Indirect object validation failed: {} objects, {} refs, {} parsed",
                obj_count, ref_count, parsed.object_count
            )
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_free_objects_level_1,
    "7.2.3.2",
    VerificationLevel::CodeExists,
    "Free objects in cross-reference table for deleted objects",
    {
        // Free objects are handled by the writer but not explicitly tested
        let pdf_bytes = create_basic_test_pdf(
            "Free Objects Test",
            "Testing free object handling in xref table",
        )?;

        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_xref = pdf_string.contains("xref");

        let passed = has_xref;
        let level_achieved = if passed { 1 } else { 0 };
        let notes = if passed {
            "Basic xref table generated - free object handling exists".to_string()
        } else {
            "No xref table found - free object handling not implemented".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_incremental_updates_level_0,
    "7.2.4.1",
    VerificationLevel::NotImplemented,
    "Incremental PDF updates without rewriting entire file",
    {
        // Incremental updates are not implemented
        let passed = false;
        let level_achieved = 0;
        let notes = "Incremental PDF updates not implemented".to_string();

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_generation_numbers_level_2,
    "7.2.4.2",
    VerificationLevel::GeneratesPdf,
    "Object generation numbers for versioning",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Generation Numbers Test",
            "Testing object generation number handling",
        )?;

        // Look for generation numbers in object definitions (simple matching)
        let pdf_string = String::from_utf8_lossy(&pdf_bytes);
        let has_generation_numbers = pdf_string.contains(" 0 obj");

        let passed = has_generation_numbers && pdf_bytes.len() > 1000;
        let level_achieved = if passed { 2 } else { 1 };
        let notes = if passed {
            "Object generation numbers (0) properly used in PDF structure".to_string()
        } else {
            "Generation number handling may be incomplete".to_string()
        };

        Ok((passed, level_achieved, notes))
    }
);

iso_test!(
    test_pdf_structure_integrity_level_4,
    "7.2.5.1",
    VerificationLevel::IsoCompliant,
    "Complete PDF structure integrity with external validation",
    {
        let pdf_bytes = create_basic_test_pdf(
            "Structure Integrity Test",
            "Complete PDF structure integrity validation",
        )?;

        // Level 3 internal verification
        let parsed = parse_pdf(&pdf_bytes)?;
        let internal_valid = parsed.xref_valid
            && parsed.catalog.is_some()
            && parsed.page_tree.is_some()
            && parsed.object_count >= 4;

        if !internal_valid {
            let notes = format!("Internal structure validation failed: xref_valid={}, catalog={}, page_tree={}, objects={}",
                               parsed.xref_valid, parsed.catalog.is_some(),
                               parsed.page_tree.is_some(), parsed.object_count);
            Ok((false, 3, notes))
        } else {
            // Level 4 external verification
            use std::fs;
            use std::process::Command;

            let temp_file = format!(
                "/tmp/structure_test_{}.pdf",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or(std::time::Duration::from_secs(0))
                    .as_secs()
            );

            fs::write(&temp_file, &pdf_bytes)?;

            let qpdf_result = Command::new("qpdf")
                .arg("--check")
                .arg("--show-data")
                .arg(&temp_file)
                .output();

            let _ = fs::remove_file(&temp_file);

            let (passed, level_achieved, notes) = match qpdf_result {
                Ok(output) => {
                    if output.status.success() {
                        (
                            true,
                            4,
                            "PDF structure passes comprehensive qpdf validation".to_string(),
                        )
                    } else {
                        let error = String::from_utf8_lossy(&output.stderr);
                        (false, 3, format!("qpdf validation failed: {}", error))
                    }
                }
                Err(_) => {
                    // qpdf not available, use internal validation
                    (
                        true,
                        3,
                        "Internal validation passed - qpdf not available for Level 4".to_string(),
                    )
                }
            };

            Ok((passed, level_achieved, notes))
        }
    }
);

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

    #[test]
    fn test_complete_file_structure() -> PdfResult<()> {
        println!("🔍 Testing Complete PDF File Structure");

        // Create a comprehensive test document
        let mut doc = Document::new();
        doc.set_title("Complete File Structure Test");
        doc.set_author("ISO Test Suite");
        doc.set_subject("Testing PDF file structure compliance");

        // Add content to ensure substantial file
        let mut page = Page::a4();

        page.text()
            .set_font(Font::Helvetica, 16.0)
            .at(50.0, 750.0)
            .write("Test passed")?;

        page.text()
            .set_font(Font::TimesRoman, 12.0)
            .at(50.0, 700.0)
            .write("This document tests compliance with ISO 32000-1:2008")?;

        page.text()
            .set_font(Font::TimesRoman, 12.0)
            .at(50.0, 680.0)
            .write("Sections 7.1-7.4: File Structure")?;

        // Add some graphics to increase object count
        page.graphics()
            .move_to(50.0, 650.0)
            .line_to(300.0, 650.0)
            .stroke();

        doc.add_page(page);
        let pdf_bytes = doc.to_bytes()?;

        println!("✓ Generated test PDF: {} bytes", pdf_bytes.len());

        // Test header
        let pdf_start = String::from_utf8_lossy(&pdf_bytes[..20]);
        assert!(pdf_start.starts_with("%PDF-"), "Must have PDF header");
        println!("✓ PDF header: {}", &pdf_start[..8]);

        // Test trailer
        let pdf_end = String::from_utf8_lossy(&pdf_bytes);
        assert!(pdf_end.contains("trailer"), "Must have trailer");
        assert!(pdf_end.contains("startxref"), "Must have startxref");
        assert!(pdf_end.trim_end().ends_with("%%EOF"), "Must end with %%EOF");
        println!("✓ PDF trailer and EOF marker present");

        // Test parsing
        let parsed = parse_pdf(&pdf_bytes)?;
        assert!(parsed.xref_valid, "Cross-reference table must be valid");
        assert!(parsed.catalog.is_some(), "Must have document catalog");
        assert!(parsed.object_count > 0, "Must have objects");

        println!("✓ Parsed successfully:");
        println!("  - Version: {}", parsed.version);
        println!("  - Objects: {}", parsed.object_count);
        println!("  - XRef valid: {}", parsed.xref_valid);

        // Test external validation if available
        let validators = get_available_validators();
        if !validators.is_empty() {
            println!("Available validators: {:?}", validators);

            for validator in &validators {
                if let Some(result) = run_external_validation(&pdf_bytes, validator) {
                    println!(
                        "{} validation: {}",
                        validator,
                        if result { "PASS" } else { "FAIL" }
                    );
                }
            }
        } else {
            println!("⚠️  No external validators available for Level 4 testing");
        }

        println!("✅ Complete file structure test passed");
        Ok(())
    }
}