| 1 |
use lopdf::{Document, Object, dictionary, SaveOptions}; |
| 2 |
|
| 3 |
#[test] |
| 4 |
fn test_catalog_included_in_object_stream_output() { |
| 5 |
// Create a simple PDF |
| 6 |
let mut doc = Document::with_version("1.5"); |
| 7 |
let pages_id = doc.new_object_id(); |
| 8 |
|
| 9 |
let page_id = doc.add_object(dictionary! { |
| 10 |
"Type" => "Page", |
| 11 |
"Parent" => pages_id, |
| 12 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 13 |
}); |
| 14 |
|
| 15 |
doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 16 |
"Type" => "Pages", |
| 17 |
"Kids" => vec![page_id.into()], |
| 18 |
"Count" => 1, |
| 19 |
})); |
| 20 |
|
| 21 |
let catalog_id = doc.add_object(dictionary! { |
| 22 |
"Type" => "Catalog", |
| 23 |
"Pages" => pages_id, |
| 24 |
}); |
| 25 |
|
| 26 |
let info_id = doc.add_object(dictionary! { |
| 27 |
"Title" => "Integration Test PDF", |
| 28 |
"Author" => "lopdf test suite", |
| 29 |
}); |
| 30 |
|
| 31 |
doc.trailer.set("Root", catalog_id); |
| 32 |
doc.trailer.set("Info", info_id); |
| 33 |
|
| 34 |
// Save with object streams |
| 35 |
let options = SaveOptions::builder() |
| 36 |
.use_object_streams(true) |
| 37 |
.use_xref_streams(true) |
| 38 |
.build(); |
| 39 |
|
| 40 |
let mut output = Vec::new(); |
| 41 |
doc.save_with_options(&mut output, options).unwrap(); |
| 42 |
|
| 43 |
// Parse the output to verify catalog is in object stream |
| 44 |
let saved_doc = Document::load_mem(&output).unwrap(); |
| 45 |
|
| 46 |
// Count object streams |
| 47 |
let obj_stream_count = saved_doc.objects.iter() |
| 48 |
.filter(|(_, obj)| { |
| 49 |
if let Object::Stream(s) = obj { |
| 50 |
s.dict.get(b"Type").ok() == Some(&Object::Name(b"ObjStm".to_vec())) |
| 51 |
} else { |
| 52 |
false |
| 53 |
} |
| 54 |
}) |
| 55 |
.count(); |
| 56 |
|
| 57 |
assert!(obj_stream_count > 0, "Should have created object streams"); |
| 58 |
|
| 59 |
// Verify file size reduction by comparing with normal save |
| 60 |
let mut normal_output = Vec::new(); |
| 61 |
doc.save_to(&mut normal_output).unwrap(); |
| 62 |
|
| 63 |
// For very small PDFs, object streams might increase size due to overhead |
| 64 |
// The important thing is that the objects are compressed |
| 65 |
println!("Normal save size: {} bytes", normal_output.len()); |
| 66 |
println!("With object streams: {} bytes", output.len()); |
| 67 |
|
| 68 |
// The key test is that objects are in streams, not the size |
| 69 |
assert!(obj_stream_count > 0, "Object streams were created"); |
| 70 |
|
| 71 |
// Check that catalog is not an individual object |
| 72 |
let content = String::from_utf8_lossy(&output); |
| 73 |
assert!( |
| 74 |
!content.contains(&format!("{} 0 obj\n<</Type/Catalog", catalog_id.0)), |
| 75 |
"Catalog should be in object stream, not as individual object" |
| 76 |
); |
| 77 |
|
| 78 |
// Check that info is not an individual object |
| 79 |
assert!( |
| 80 |
!content.contains(&format!("{} 0 obj\n<</Title", info_id.0)), |
| 81 |
"Info dictionary should be in object stream, not as individual object" |
| 82 |
); |
| 83 |
} |