| 1 |
use lopdf::{Document, Object}; |
| 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 |
let compressed_path = "/Users/nicolasdao/Downloads/pdfs/pdf-demo_compressed.pdf"; |
| 24 |
|
| 25 |
println!("Checking if Page objects are in object streams...\n"); |
| 26 |
|
| 27 |
// Load the compressed PDF |
| 28 |
let doc = load_document(compressed_path)?; |
| 29 |
|
| 30 |
// Check for object streams |
| 31 |
let mut objstm_count = 0; |
| 32 |
let mut total_compressed = 0; |
| 33 |
|
| 34 |
for (id, obj) in &doc.objects { |
| 35 |
if let Object::Stream(stream) = obj { |
| 36 |
if let Ok(type_obj) = stream.dict.get(b"Type") { |
| 37 |
if let Ok(type_name) = type_obj.as_name() { |
| 38 |
if type_name == b"ObjStm" { |
| 39 |
objstm_count += 1; |
| 40 |
|
| 41 |
// Get number of objects in this stream |
| 42 |
if let Ok(n) = stream.dict.get(b"N") { |
| 43 |
if let Ok(n_val) = n.as_i64() { |
| 44 |
total_compressed += n_val; |
| 45 |
println!("Object stream {} 0 R contains {} objects", id.0, n_val); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
println!("\nTotal object streams: {}", objstm_count); |
| 55 |
println!("Total compressed objects: {}", total_compressed); |
| 56 |
|
| 57 |
// Check if we can find Page objects as top-level objects |
| 58 |
let mut page_count = 0; |
| 59 |
let mut pages_count = 0; |
| 60 |
let mut catalog_count = 0; |
| 61 |
|
| 62 |
for (_id, obj) in &doc.objects { |
| 63 |
if let Object::Dictionary(dict) = obj { |
| 64 |
if let Ok(type_obj) = dict.get(b"Type") { |
| 65 |
if let Ok(type_name) = type_obj.as_name() { |
| 66 |
match type_name { |
| 67 |
b"Page" => page_count += 1, |
| 68 |
b"Pages" => pages_count += 1, |
| 69 |
b"Catalog" => catalog_count += 1, |
| 70 |
_ => {} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
println!("\nTop-level objects found:"); |
| 78 |
println!(" Page objects: {}", page_count); |
| 79 |
println!(" Pages objects: {}", pages_count); |
| 80 |
println!(" Catalog objects: {}", catalog_count); |
| 81 |
|
| 82 |
if page_count == 0 && pages_count == 0 { |
| 83 |
println!("\n✓ SUCCESS: Page and Pages objects are compressed into object streams!"); |
| 84 |
} else { |
| 85 |
println!("\n✗ WARNING: Some structural objects may not be compressed"); |
| 86 |
} |
| 87 |
|
| 88 |
// Check the raw PDF content |
| 89 |
let content = std::fs::read_to_string(compressed_path)?; |
| 90 |
|
| 91 |
// Look for individual page definitions |
| 92 |
let page_defs = content.matches(" 0 obj\n<</Type/Page").count(); |
| 93 |
println!("\nPage definitions found in raw PDF: {}", page_defs); |
| 94 |
|
| 95 |
if page_defs == 0 { |
| 96 |
println!("✓ No individual Page object definitions found - they're in object streams!"); |
| 97 |
} |
| 98 |
|
| 99 |
Ok(()) |
| 100 |
} |