| 1 |
use lopdf::{Document, Object, SaveOptions, dictionary}; |
| 2 |
use std::io::Write; |
| 3 |
|
| 4 |
fn main() { |
| 5 |
println!("Debugging save_with_object_streams...\n"); |
| 6 |
|
| 7 |
// Create a simple document |
| 8 |
let mut doc = Document::with_version("1.5"); |
| 9 |
|
| 10 |
// Add some simple objects that can be compressed |
| 11 |
let obj1_id = doc.add_object(Object::Integer(42)); |
| 12 |
let obj2_id = doc.add_object(Object::String(b"Hello".to_vec(), lopdf::StringFormat::Literal)); |
| 13 |
let obj3_id = doc.add_object(dictionary! { |
| 14 |
"Type" => "TestObject", |
| 15 |
"Value" => 123 |
| 16 |
}); |
| 17 |
|
| 18 |
println!("Created objects:"); |
| 19 |
println!(" {} 0 R: Integer", obj1_id.0); |
| 20 |
println!(" {} 0 R: String", obj2_id.0); |
| 21 |
println!(" {} 0 R: Dictionary", obj3_id.0); |
| 22 |
|
| 23 |
// Add required catalog structure |
| 24 |
let pages_id = doc.add_object(dictionary! { |
| 25 |
"Type" => "Pages", |
| 26 |
"Kids" => vec![], |
| 27 |
"Count" => 0 |
| 28 |
}); |
| 29 |
|
| 30 |
let catalog_id = doc.add_object(dictionary! { |
| 31 |
"Type" => "Catalog", |
| 32 |
"Pages" => pages_id |
| 33 |
}); |
| 34 |
|
| 35 |
doc.trailer.set("Root", catalog_id); |
| 36 |
|
| 37 |
println!("\nSaving with object streams..."); |
| 38 |
|
| 39 |
// Save with object streams |
| 40 |
let options = SaveOptions { |
| 41 |
use_object_streams: true, |
| 42 |
..Default::default() |
| 43 |
}; |
| 44 |
|
| 45 |
let mut buffer = Vec::new(); |
| 46 |
doc.save_with_options(&mut buffer, options).unwrap(); |
| 47 |
|
| 48 |
println!("Saved {} bytes", buffer.len()); |
| 49 |
|
| 50 |
// Write to file for inspection |
| 51 |
let mut file = std::fs::File::create("debug_objstream_save.pdf").unwrap(); |
| 52 |
file.write_all(&buffer).unwrap(); |
| 53 |
|
| 54 |
// Load it back and check |
| 55 |
println!("\nLoading back and analyzing..."); |
| 56 |
let loaded = Document::load_mem(&buffer).unwrap(); |
| 57 |
|
| 58 |
println!("Loaded {} objects", loaded.objects.len()); |
| 59 |
|
| 60 |
// Find object streams |
| 61 |
for (id, obj) in &loaded.objects { |
| 62 |
if let Object::Stream(stream) = obj { |
| 63 |
if let Ok(type_obj) = stream.dict.get(b"Type") { |
| 64 |
if let Ok(type_name) = type_obj.as_name() { |
| 65 |
if type_name == b"ObjStm" { |
| 66 |
println!("\nFound object stream {} 0 R:", id.0); |
| 67 |
println!(" Dictionary: {:?}", stream.dict); |
| 68 |
println!(" Has Filter: {}", stream.dict.get(b"Filter").is_ok()); |
| 69 |
println!(" Content size: {} bytes", stream.content.len()); |
| 70 |
|
| 71 |
// Check if it looks compressed |
| 72 |
let looks_compressed = stream.content.iter().take(10).any(|&b| b > 127 || b < 32); |
| 73 |
println!(" Content looks compressed: {}", looks_compressed); |
| 74 |
|
| 75 |
if !looks_compressed { |
| 76 |
println!(" First 50 bytes: {:?}", &stream.content[..stream.content.len().min(50)]); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
println!("\nDone! Check debug_objstream_save.pdf"); |
| 85 |
} |