| 1 |
use lopdf::{Document, Object, SaveOptions, dictionary}; |
| 2 |
|
| 3 |
fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 4 |
println!("Testing structural object compression...\n"); |
| 5 |
|
| 6 |
// Create a simple PDF with structural objects |
| 7 |
let mut doc = Document::with_version("1.4"); |
| 8 |
|
| 9 |
// Create catalog (should be compressed) |
| 10 |
let catalog_id = doc.add_object(dictionary! { |
| 11 |
"Type" => "Catalog", |
| 12 |
"Pages" => Object::Reference((2, 0)) |
| 13 |
}); |
| 14 |
|
| 15 |
// Create pages tree (should be compressed) |
| 16 |
let _pages_id = doc.add_object(dictionary! { |
| 17 |
"Type" => "Pages", |
| 18 |
"Kids" => vec![Object::Reference((3, 0))], |
| 19 |
"Count" => 1 |
| 20 |
}); |
| 21 |
|
| 22 |
// Create page (should be compressed) |
| 23 |
let _page_id = doc.add_object(dictionary! { |
| 24 |
"Type" => "Page", |
| 25 |
"Parent" => Object::Reference((2, 0)), |
| 26 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 27 |
"Resources" => dictionary! {} |
| 28 |
}); |
| 29 |
|
| 30 |
// Add some font objects for good measure |
| 31 |
for i in 10..15 { |
| 32 |
doc.add_object(dictionary! { |
| 33 |
"Type" => "Font", |
| 34 |
"Subtype" => "Type1", |
| 35 |
"BaseFont" => format!("Font{}", i) |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
// Set up trailer |
| 40 |
doc.trailer.set("Root", catalog_id); |
| 41 |
doc.max_id = 15; |
| 42 |
doc.renumber_objects(); |
| 43 |
|
| 44 |
// Save without object streams |
| 45 |
println!("Saving without object streams..."); |
| 46 |
let mut no_objstm = Vec::new(); |
| 47 |
doc.save_to(&mut no_objstm)?; |
| 48 |
|
| 49 |
// Save with object streams |
| 50 |
println!("Saving with object streams..."); |
| 51 |
let mut with_objstm = Vec::new(); |
| 52 |
let options = SaveOptions::builder() |
| 53 |
.use_object_streams(true) |
| 54 |
.use_xref_streams(true) |
| 55 |
.max_objects_per_stream(100) |
| 56 |
.compression_level(6) |
| 57 |
.build(); |
| 58 |
doc.save_with_options(&mut with_objstm, options)?; |
| 59 |
|
| 60 |
// Analyze results |
| 61 |
let no_objstm_str = String::from_utf8_lossy(&no_objstm); |
| 62 |
let with_objstm_str = String::from_utf8_lossy(&with_objstm); |
| 63 |
|
| 64 |
println!("\n=== Analysis ==="); |
| 65 |
println!("Without object streams: {} bytes", no_objstm.len()); |
| 66 |
println!("With object streams: {} bytes", with_objstm.len()); |
| 67 |
println!("Size reduction: {:.1}%", |
| 68 |
(1.0 - with_objstm.len() as f64 / no_objstm.len() as f64) * 100.0); |
| 69 |
|
| 70 |
// Check for object stream markers |
| 71 |
let objstm_count = with_objstm_str.matches("/ObjStm").count(); |
| 72 |
println!("\nObject streams created: {}", objstm_count); |
| 73 |
|
| 74 |
// Check that individual objects are NOT present in compressed version |
| 75 |
println!("\n=== Checking for individual object definitions ==="); |
| 76 |
|
| 77 |
// These patterns should NOT appear in the compressed version |
| 78 |
let patterns = vec![ |
| 79 |
"1 0 obj", // Catalog |
| 80 |
"2 0 obj", // Pages |
| 81 |
"3 0 obj", // Page |
| 82 |
"10 0 obj", // Font |
| 83 |
"11 0 obj", // Font |
| 84 |
]; |
| 85 |
|
| 86 |
for pattern in patterns { |
| 87 |
let in_original = no_objstm_str.contains(pattern); |
| 88 |
let in_compressed = with_objstm_str.contains(pattern); |
| 89 |
|
| 90 |
println!("{}: original={}, compressed={}", |
| 91 |
pattern, in_original, in_compressed); |
| 92 |
|
| 93 |
if in_compressed { |
| 94 |
println!(" WARNING: Object {} should have been compressed!", pattern); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// Save to files for manual inspection |
| 99 |
std::fs::write("test_no_objstm.pdf", &no_objstm)?; |
| 100 |
std::fs::write("test_with_objstm.pdf", &with_objstm)?; |
| 101 |
|
| 102 |
println!("\nSaved test files:"); |
| 103 |
println!(" - test_no_objstm.pdf"); |
| 104 |
println!(" - test_with_objstm.pdf"); |
| 105 |
|
| 106 |
// Load and verify the compressed PDF |
| 107 |
println!("\n=== Verifying compressed PDF ==="); |
| 108 |
let compressed_doc = Document::load_mem(&with_objstm[..])?; |
| 109 |
|
| 110 |
let mut objstm_found = 0; |
| 111 |
let mut compressed_objects = 0; |
| 112 |
|
| 113 |
for (_, obj) in &compressed_doc.objects { |
| 114 |
if let Object::Stream(stream) = obj { |
| 115 |
if let Ok(type_obj) = stream.dict.get(b"Type") { |
| 116 |
if let Ok(type_name) = type_obj.as_name() { |
| 117 |
if type_name == b"ObjStm" { |
| 118 |
objstm_found += 1; |
| 119 |
|
| 120 |
// Count objects in this stream |
| 121 |
if let Ok(n) = stream.dict.get(b"N") { |
| 122 |
if let Ok(n_val) = n.as_i64() { |
| 123 |
compressed_objects += n_val as usize; |
| 124 |
println!("Object stream found with {} objects", n_val); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
println!("\nTotal object streams: {}", objstm_found); |
| 134 |
println!("Total compressed objects: {}", compressed_objects); |
| 135 |
|
| 136 |
if compressed_objects < 5 { |
| 137 |
println!("\nERROR: Expected at least 5 objects to be compressed (Catalog, Pages, Page, and Fonts)"); |
| 138 |
println!("Only {} objects were compressed!", compressed_objects); |
| 139 |
} else { |
| 140 |
println!("\n✓ SUCCESS: Structural objects are being compressed!"); |
| 141 |
} |
| 142 |
|
| 143 |
Ok(()) |
| 144 |
} |