| 1 |
use lopdf::{Document, 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, lopdf::Error> { |
| 8 |
Document::load(path) |
| 9 |
} |
| 10 |
|
| 11 |
#[cfg(feature = "async")] |
| 12 |
fn load_document(path: &str) -> Result<Document, lopdf::Error> { |
| 13 |
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() { |
| 23 |
println!("Debugging object stream compression in detail...\n"); |
| 24 |
|
| 25 |
// Load the user's PDF |
| 26 |
let pdf_path = "/Users/nicolasdao/Downloads/pdfs/RFQ - SDS WebApp.docx.pdf"; |
| 27 |
println!("Loading PDF: {}", pdf_path); |
| 28 |
|
| 29 |
let mut doc = match load_document(pdf_path) { |
| 30 |
Ok(d) => d, |
| 31 |
Err(e) => { |
| 32 |
eprintln!("Failed to load PDF: {}", e); |
| 33 |
return; |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
println!("Loaded {} objects", doc.objects.len()); |
| 38 |
|
| 39 |
// Count how many objects can be compressed |
| 40 |
let mut compressible_count = 0; |
| 41 |
for (id, obj) in &doc.objects { |
| 42 |
if id.1 == 0 && lopdf::ObjectStream::can_be_compressed(*id, obj, &doc) { |
| 43 |
compressible_count += 1; |
| 44 |
} |
| 45 |
} |
| 46 |
println!("Compressible objects: {}", compressible_count); |
| 47 |
|
| 48 |
// Save with object streams |
| 49 |
println!("\nSaving with object streams (compression level 6)..."); |
| 50 |
let options = SaveOptions { |
| 51 |
use_object_streams: true, |
| 52 |
use_xref_streams: true, // MUST use xref streams with object streams! |
| 53 |
object_stream_config: lopdf::ObjectStreamConfig { |
| 54 |
max_objects_per_stream: 100, |
| 55 |
compression_level: 6, |
| 56 |
}, |
| 57 |
..Default::default() |
| 58 |
}; |
| 59 |
|
| 60 |
let mut buffer = Vec::new(); |
| 61 |
doc.save_with_options(&mut buffer, options).unwrap(); |
| 62 |
|
| 63 |
println!("Saved {} bytes", buffer.len()); |
| 64 |
|
| 65 |
// Write to file for inspection |
| 66 |
std::fs::write("debug_compression.pdf", &buffer).unwrap(); |
| 67 |
|
| 68 |
// Load back and check |
| 69 |
println!("\nLoading back and checking object streams..."); |
| 70 |
let loaded = Document::load_mem(&buffer).unwrap(); |
| 71 |
|
| 72 |
let mut obj_stream_count = 0; |
| 73 |
let mut obj_stream_with_filter = 0; |
| 74 |
|
| 75 |
for (id, obj) in &loaded.objects { |
| 76 |
if let lopdf::Object::Stream(stream) = obj { |
| 77 |
if let Ok(type_obj) = stream.dict.get(b"Type") { |
| 78 |
if let Ok(type_name) = type_obj.as_name() { |
| 79 |
if type_name == b"ObjStm" { |
| 80 |
obj_stream_count += 1; |
| 81 |
let has_filter = stream.dict.get(b"Filter").is_ok(); |
| 82 |
if has_filter { |
| 83 |
obj_stream_with_filter += 1; |
| 84 |
} |
| 85 |
|
| 86 |
println!("\nObject Stream {} 0 R:", id.0); |
| 87 |
println!(" Has Filter: {}", has_filter); |
| 88 |
if let Ok(n) = stream.dict.get(b"N").and_then(|o| o.as_i64()) { |
| 89 |
println!(" Contains {} objects", n); |
| 90 |
} |
| 91 |
if let Ok(first) = stream.dict.get(b"First").and_then(|o| o.as_i64()) { |
| 92 |
println!(" First offset: {}", first); |
| 93 |
} |
| 94 |
println!(" Content size: {} bytes", stream.content.len()); |
| 95 |
|
| 96 |
// Check if content looks compressed |
| 97 |
let looks_compressed = stream.content.iter().take(20).any(|&b| b > 127); |
| 98 |
println!(" Content looks compressed: {}", looks_compressed); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
println!("\nSummary:"); |
| 106 |
println!(" Total object streams: {}", obj_stream_count); |
| 107 |
println!(" Object streams with Filter: {}", obj_stream_with_filter); |
| 108 |
|
| 109 |
if obj_stream_with_filter < obj_stream_count { |
| 110 |
println!("\nWARNING: {} object streams are missing Filter!", obj_stream_count - obj_stream_with_filter); |
| 111 |
} |
| 112 |
} |