| 1 |
use lopdf::{Document, Object, Dictionary, SaveOptions}; |
| 2 |
|
| 3 |
#[cfg(feature = "async")] |
| 4 |
use tokio::runtime::Builder; |
| 5 |
|
| 6 |
#[cfg(not(feature = "async"))] |
| 7 |
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> { |
| 8 |
Ok(Document::load(path)?) |
| 9 |
} |
| 10 |
|
| 11 |
#[cfg(feature = "async")] |
| 12 |
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> { |
| 13 |
Ok(Builder::new_current_thread() |
| 14 |
.enable_all() |
| 15 |
.build() |
| 16 |
.unwrap() |
| 17 |
.block_on(async move { |
| 18 |
Document::load(path).await |
| 19 |
})?) |
| 20 |
} |
| 21 |
|
| 22 |
fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 23 |
// Create a simple PDF with objects that can be compressed |
| 24 |
let mut doc = Document::with_version("1.4"); |
| 25 |
|
| 26 |
// Create catalog |
| 27 |
let mut catalog = Dictionary::new(); |
| 28 |
catalog.set("Type", "Catalog"); |
| 29 |
catalog.set("Pages", Object::Reference((2, 0))); |
| 30 |
doc.objects.insert((1, 0), Object::Dictionary(catalog)); |
| 31 |
|
| 32 |
// Create pages root |
| 33 |
let mut pages = Dictionary::new(); |
| 34 |
pages.set("Type", "Pages"); |
| 35 |
pages.set("Kids", vec![Object::Reference((3, 0))]); |
| 36 |
pages.set("Count", 1); |
| 37 |
doc.objects.insert((2, 0), Object::Dictionary(pages)); |
| 38 |
|
| 39 |
// Create a page |
| 40 |
let mut page = Dictionary::new(); |
| 41 |
page.set("Type", "Page"); |
| 42 |
page.set("Parent", Object::Reference((2, 0))); |
| 43 |
page.set("MediaBox", vec![0.into(), 0.into(), 612.into(), 792.into()]); |
| 44 |
doc.objects.insert((3, 0), Object::Dictionary(page)); |
| 45 |
|
| 46 |
// Create some metadata objects that should be compressible |
| 47 |
for i in 10..20 { |
| 48 |
let mut meta = Dictionary::new(); |
| 49 |
meta.set("Type", "Metadata"); |
| 50 |
meta.set("ID", i); |
| 51 |
meta.set("Data", format!("This is metadata object {}", i)); |
| 52 |
doc.objects.insert((i as u32, 0), Object::Dictionary(meta)); |
| 53 |
} |
| 54 |
|
| 55 |
// Create some annotation objects that should be compressible |
| 56 |
for i in 20..30 { |
| 57 |
let mut annot = Dictionary::new(); |
| 58 |
annot.set("Type", "Annot"); |
| 59 |
annot.set("Subtype", "Text"); |
| 60 |
annot.set("Contents", format!("Annotation {}", i)); |
| 61 |
annot.set("Rect", vec![100.into(), 100.into(), 200.into(), 200.into()]); |
| 62 |
doc.objects.insert((i as u32, 0), Object::Dictionary(annot)); |
| 63 |
} |
| 64 |
|
| 65 |
// Set up trailer |
| 66 |
doc.trailer.set("Root", Object::Reference((1, 0))); |
| 67 |
doc.max_id = 30; |
| 68 |
doc.renumber_objects(); |
| 69 |
|
| 70 |
// Save without object streams |
| 71 |
println!("Saving without object streams..."); |
| 72 |
doc.save("test_no_objstm.pdf")?; |
| 73 |
|
| 74 |
// Save with object streams |
| 75 |
println!("Saving with object streams..."); |
| 76 |
let options = SaveOptions { |
| 77 |
use_object_streams: true, |
| 78 |
use_xref_streams: true, |
| 79 |
..Default::default() |
| 80 |
}; |
| 81 |
doc.save_with_options(&mut std::fs::File::create("test_with_objstm.pdf")?, options)?; |
| 82 |
|
| 83 |
// Check file sizes |
| 84 |
let no_objstm_size = std::fs::metadata("test_no_objstm.pdf")?.len(); |
| 85 |
let with_objstm_size = std::fs::metadata("test_with_objstm.pdf")?.len(); |
| 86 |
|
| 87 |
println!("\nFile sizes:"); |
| 88 |
println!(" Without object streams: {} bytes", no_objstm_size); |
| 89 |
println!(" With object streams: {} bytes", with_objstm_size); |
| 90 |
println!(" Reduction: {:.1}%", (1.0 - with_objstm_size as f64 / no_objstm_size as f64) * 100.0); |
| 91 |
|
| 92 |
// Load the compressed PDF and check for object streams |
| 93 |
println!("\nChecking compressed PDF..."); |
| 94 |
let compressed_doc = load_document("test_with_objstm.pdf")?; |
| 95 |
|
| 96 |
let mut objstm_count = 0; |
| 97 |
let mut compressed_count = 0; |
| 98 |
|
| 99 |
for (_id, obj) in &compressed_doc.objects { |
| 100 |
if let Object::Stream(stream) = obj { |
| 101 |
if let Ok(type_obj) = stream.dict.get(b"Type") { |
| 102 |
if let Ok(type_name) = type_obj.as_name() { |
| 103 |
if type_name == b"ObjStm" { |
| 104 |
objstm_count += 1; |
| 105 |
// Count objects in this stream |
| 106 |
if let Ok(n) = stream.dict.get(b"N") { |
| 107 |
if let Ok(n_val) = n.as_i64() { |
| 108 |
compressed_count += n_val as usize; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
println!(" Object streams found: {}", objstm_count); |
| 118 |
println!(" Total compressed objects: {}", compressed_count); |
| 119 |
|
| 120 |
// Also check the raw file content |
| 121 |
println!("\nChecking raw file for /ObjStm..."); |
| 122 |
let content = std::fs::read_to_string("test_with_objstm.pdf").unwrap_or_default(); |
| 123 |
let objstm_occurrences = content.matches("/ObjStm").count(); |
| 124 |
println!(" /ObjStm occurrences in file: {}", objstm_occurrences); |
| 125 |
|
| 126 |
Ok(()) |
| 127 |
} |