Debian Codegraph @

debian/missing-sources/lopdf/examples/verify_trailer_compression_fix.rs

raw
1 use lopdf::{Document, Object, ObjectStream, dictionary, SaveOptions};
2
3 fn main() -> Result<(), Box<dyn std::error::Error>> {
4 println!("Verifying trailer-referenced object compression fix...\n");
5
6 // Create a simple PDF with catalog and info
7 let mut doc = Document::with_version("1.5");
8 let pages_id = doc.new_object_id();
9
10 let page_id = doc.add_object(dictionary! {
11 "Type" => "Page",
12 "Parent" => pages_id,
13 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
14 });
15
16 doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
17 "Type" => "Pages",
18 "Kids" => vec![page_id.into()],
19 "Count" => 1,
20 }));
21
22 let catalog_id = doc.add_object(dictionary! {
23 "Type" => "Catalog",
24 "Pages" => pages_id,
25 });
26
27 let info_id = doc.add_object(dictionary! {
28 "Title" => "Test PDF",
29 "Author" => "lopdf",
30 "Creator" => "lopdf test",
31 "Producer" => "lopdf",
32 "CreationDate" => "D:20250807120000Z",
33 });
34
35 // Set in trailer
36 doc.trailer.set("Root", catalog_id);
37 doc.trailer.set("Info", info_id);
38
39 // Test compression eligibility
40 println!("Testing compression eligibility after trailer references:");
41
42 let catalog_obj = doc.objects.get(&catalog_id).unwrap();
43 let can_compress_catalog = ObjectStream::can_be_compressed(catalog_id, catalog_obj, &doc);
44 println!(" Catalog (Root): can_be_compressed = {}", can_compress_catalog);
45
46 let info_obj = doc.objects.get(&info_id).unwrap();
47 let can_compress_info = ObjectStream::can_be_compressed(info_id, info_obj, &doc);
48 println!(" Info dictionary: can_be_compressed = {}", can_compress_info);
49
50 // Save with and without object streams
51 let mut normal_output = Vec::new();
52 doc.save_to(&mut normal_output)?;
53
54 let options = SaveOptions::builder()
55 .use_object_streams(true)
56 .use_xref_streams(true)
57 .build();
58
59 let mut compressed_output = Vec::new();
60 doc.save_with_options(&mut compressed_output, options)?;
61
62 println!("\nFile sizes:");
63 println!(" Without object streams: {} bytes", normal_output.len());
64 println!(" With object streams: {} bytes", compressed_output.len());
65
66 let reduction_pct = (1.0 - (compressed_output.len() as f64 / normal_output.len() as f64)) * 100.0;
67 println!(" Size reduction: {:.1}%", reduction_pct);
68
69 // Check if catalog is in object stream
70 let content = String::from_utf8_lossy(&compressed_output);
71 let has_objstm = content.contains("/ObjStm");
72 let catalog_as_individual = content.contains(&format!("{} 0 obj\n<</Type/Catalog", catalog_id.0));
73 let info_as_individual = content.contains(&format!("{} 0 obj\n<</Title", info_id.0));
74
75 println!("\nCompression results:");
76 println!(" Has object streams: {}", has_objstm);
77 println!(" Catalog as individual object: {}", catalog_as_individual);
78 println!(" Info as individual object: {}", info_as_individual);
79
80 if can_compress_catalog && can_compress_info && has_objstm && !catalog_as_individual && !info_as_individual {
81 println!("\n✅ SUCCESS: Trailer-referenced objects are properly compressed!");
82 } else {
83 println!("\n❌ FAILURE: Fix not working correctly");
84 }
85
86 Ok(())
87 }