| 1 |
use lopdf::{Document, Object}; |
| 2 |
use std::env; |
| 3 |
use std::collections::HashMap; |
| 4 |
|
| 5 |
#[cfg(feature = "async")] |
| 6 |
use tokio::runtime::Builder; |
| 7 |
|
| 8 |
#[cfg(not(feature = "async"))] |
| 9 |
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> { |
| 10 |
Ok(Document::load(path)?) |
| 11 |
} |
| 12 |
|
| 13 |
#[cfg(feature = "async")] |
| 14 |
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> { |
| 15 |
Ok(Builder::new_current_thread() |
| 16 |
.enable_all() |
| 17 |
.build() |
| 18 |
.unwrap() |
| 19 |
.block_on(async move { |
| 20 |
Document::load(path).await |
| 21 |
})?) |
| 22 |
} |
| 23 |
|
| 24 |
fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 25 |
let args: Vec<String> = env::args().collect(); |
| 26 |
let input_file = if args.len() > 1 { |
| 27 |
&args[1] |
| 28 |
} else { |
| 29 |
"assets/example.pdf" |
| 30 |
}; |
| 31 |
|
| 32 |
println!("Analyzing PDF: {}", input_file); |
| 33 |
let doc = load_document(input_file)?; |
| 34 |
|
| 35 |
// Count object types |
| 36 |
let mut type_counts: HashMap<String, usize> = HashMap::new(); |
| 37 |
let mut stream_count = 0; |
| 38 |
let mut compressible_count = 0; |
| 39 |
|
| 40 |
for ((_id, _gen), obj) in &doc.objects { |
| 41 |
let type_name = match obj { |
| 42 |
Object::Null => "Null", |
| 43 |
Object::Boolean(_) => "Boolean", |
| 44 |
Object::Integer(_) => "Integer", |
| 45 |
Object::Real(_) => "Real", |
| 46 |
Object::Name(_) => "Name", |
| 47 |
Object::String(_, _) => "String", |
| 48 |
Object::Array(_) => "Array", |
| 49 |
Object::Dictionary(dict) => { |
| 50 |
// Try to get the Type entry to be more specific |
| 51 |
if let Ok(Object::Name(name)) = dict.get(b"Type") { |
| 52 |
let type_str = String::from_utf8_lossy(name); |
| 53 |
&format!("Dict/{}", type_str) |
| 54 |
} else { |
| 55 |
"Dictionary" |
| 56 |
} |
| 57 |
}, |
| 58 |
Object::Stream(_) => { |
| 59 |
stream_count += 1; |
| 60 |
"Stream" |
| 61 |
}, |
| 62 |
Object::Reference(_) => "Reference", |
| 63 |
}; |
| 64 |
|
| 65 |
// Check if object can be compressed |
| 66 |
if !matches!(obj, Object::Stream(_)) { |
| 67 |
compressible_count += 1; |
| 68 |
} |
| 69 |
|
| 70 |
*type_counts.entry(type_name.to_string()).or_insert(0) += 1; |
| 71 |
} |
| 72 |
|
| 73 |
println!("\nPDF Statistics:"); |
| 74 |
println!("Version: {}", doc.version); |
| 75 |
println!("Total objects: {}", doc.objects.len()); |
| 76 |
println!("Stream objects: {} (cannot be compressed)", stream_count); |
| 77 |
println!("Compressible objects: {}", compressible_count); |
| 78 |
println!("Compression potential: {:.1}%", (compressible_count as f64 / doc.objects.len() as f64) * 100.0); |
| 79 |
|
| 80 |
println!("\nObject type breakdown:"); |
| 81 |
let mut sorted_types: Vec<_> = type_counts.iter().collect(); |
| 82 |
sorted_types.sort_by_key(|(_, count)| *count); |
| 83 |
sorted_types.reverse(); |
| 84 |
|
| 85 |
for (type_name, count) in sorted_types { |
| 86 |
println!(" {}: {}", type_name, count); |
| 87 |
} |
| 88 |
|
| 89 |
// Check if already using object streams |
| 90 |
let has_objstm = doc.objects.values().any(|obj| { |
| 91 |
if let Object::Dictionary(dict) = obj { |
| 92 |
if let Ok(Object::Name(name)) = dict.get(b"Type") { |
| 93 |
return name == b"ObjStm"; |
| 94 |
} |
| 95 |
} |
| 96 |
false |
| 97 |
}); |
| 98 |
|
| 99 |
println!("\nAlready using object streams: {}", if has_objstm { "Yes" } else { "No" }); |
| 100 |
|
| 101 |
Ok(()) |
| 102 |
} |