| 1 |
use lopdf::{Document, Object, ObjectId}; |
| 2 |
use std::collections::{HashMap, HashSet}; |
| 3 |
|
| 4 |
#[cfg(feature = "async")] |
| 5 |
use tokio::runtime::Builder; |
| 6 |
|
| 7 |
#[cfg(not(feature = "async"))] |
| 8 |
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> { |
| 9 |
Ok(Document::load(path)?) |
| 10 |
} |
| 11 |
|
| 12 |
#[cfg(feature = "async")] |
| 13 |
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> { |
| 14 |
Ok(Builder::new_current_thread() |
| 15 |
.enable_all() |
| 16 |
.build() |
| 17 |
.unwrap() |
| 18 |
.block_on(async move { |
| 19 |
Document::load(path).await |
| 20 |
})?) |
| 21 |
} |
| 22 |
|
| 23 |
fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 24 |
let pdf_path = "/Users/nicolasdao/Downloads/pdfs/RFQ - SDS WebApp.docx.pdf"; |
| 25 |
println!("Analyzing references in: {}", pdf_path); |
| 26 |
|
| 27 |
let doc = load_document(pdf_path)?; |
| 28 |
|
| 29 |
// Build reference graph |
| 30 |
let mut references: HashMap<ObjectId, HashSet<ObjectId>> = HashMap::new(); |
| 31 |
let mut referenced_by: HashMap<ObjectId, HashSet<ObjectId>> = HashMap::new(); |
| 32 |
|
| 33 |
// Collect all references |
| 34 |
for (&id, obj) in &doc.objects { |
| 35 |
let refs = collect_references_from_object(obj); |
| 36 |
references.insert(id, refs.clone()); |
| 37 |
|
| 38 |
// Build reverse mapping |
| 39 |
for ref_id in refs { |
| 40 |
referenced_by.entry(ref_id).or_insert_with(HashSet::new).insert(id); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Also check trailer references |
| 45 |
let trailer_refs = collect_references_from_dict(&doc.trailer); |
| 46 |
for ref_id in &trailer_refs { |
| 47 |
referenced_by.entry(*ref_id).or_insert_with(HashSet::new).insert((0, 0)); |
| 48 |
} |
| 49 |
|
| 50 |
// Analyze specific problematic objects |
| 51 |
println!("\nAnalyzing problematic objects:"); |
| 52 |
|
| 53 |
// Object 427 (referenced by catalog 431) |
| 54 |
if let Some(refs_to_427) = referenced_by.get(&(427, 0)) { |
| 55 |
println!("\nObject 427 0 R is referenced by:"); |
| 56 |
for &ref_id in refs_to_427 { |
| 57 |
println!(" {} {} R", ref_id.0, ref_id.1); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Check what object 427 is |
| 62 |
if let Ok(obj_427) = doc.get_object((427, 0)) { |
| 63 |
println!("Object 427 is: {:?}", describe_object(obj_427)); |
| 64 |
} |
| 65 |
|
| 66 |
// Check fonts that reference orphaned objects |
| 67 |
println!("\nChecking font references:"); |
| 68 |
for &font_id in &[(8, 0), (9, 0), (10, 0), (11, 0), (12, 0)] { |
| 69 |
if let Ok(Object::Dictionary(font_dict)) = doc.get_object(font_id) { |
| 70 |
println!("\nFont {} {} R:", font_id.0, font_id.1); |
| 71 |
for (key, value) in font_dict.iter() { |
| 72 |
if let Object::Reference(ref_id) = value { |
| 73 |
let exists = doc.objects.contains_key(ref_id); |
| 74 |
println!(" {} -> {} {} R (exists: {})", |
| 75 |
String::from_utf8_lossy(key), ref_id.0, ref_id.1, exists); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Analyze which objects would be compressed |
| 82 |
println!("\n\nAnalyzing compression eligibility:"); |
| 83 |
let mut would_compress = Vec::new(); |
| 84 |
let mut would_not_compress = Vec::new(); |
| 85 |
|
| 86 |
for (&id, obj) in &doc.objects { |
| 87 |
if lopdf::ObjectStream::can_be_compressed(id, obj, &doc) { |
| 88 |
would_compress.push(id); |
| 89 |
} else { |
| 90 |
would_not_compress.push(id); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Check if any compressed objects are referenced by non-compressed objects |
| 95 |
println!("\nChecking for problematic compressions:"); |
| 96 |
let mut problems = Vec::new(); |
| 97 |
|
| 98 |
for &compressed_id in &would_compress { |
| 99 |
if let Some(referencers) = referenced_by.get(&compressed_id) { |
| 100 |
for &referencer_id in referencers { |
| 101 |
if referencer_id == (0, 0) { |
| 102 |
// Referenced from trailer |
| 103 |
problems.push((compressed_id, referencer_id, "trailer")); |
| 104 |
} else if would_not_compress.contains(&referencer_id) { |
| 105 |
// Referenced by a non-compressed object |
| 106 |
problems.push((compressed_id, referencer_id, "non-compressed object")); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if !problems.is_empty() { |
| 113 |
println!("\nFOUND {} PROBLEMATIC COMPRESSIONS:", problems.len()); |
| 114 |
for (compressed_id, referencer_id, reason) in problems.iter().take(20) { |
| 115 |
println!(" {} {} R would be compressed but is referenced by {} {} R ({})", |
| 116 |
compressed_id.0, compressed_id.1, referencer_id.0, referencer_id.1, reason); |
| 117 |
} |
| 118 |
if problems.len() > 20 { |
| 119 |
println!(" ... and {} more", problems.len() - 20); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
Ok(()) |
| 124 |
} |
| 125 |
|
| 126 |
fn collect_references_from_object(obj: &Object) -> HashSet<ObjectId> { |
| 127 |
let mut refs = HashSet::new(); |
| 128 |
|
| 129 |
match obj { |
| 130 |
Object::Reference(id) => { |
| 131 |
refs.insert(*id); |
| 132 |
} |
| 133 |
Object::Array(array) => { |
| 134 |
for item in array { |
| 135 |
refs.extend(collect_references_from_object(item)); |
| 136 |
} |
| 137 |
} |
| 138 |
Object::Dictionary(dict) => { |
| 139 |
refs.extend(collect_references_from_dict(dict)); |
| 140 |
} |
| 141 |
Object::Stream(stream) => { |
| 142 |
refs.extend(collect_references_from_dict(&stream.dict)); |
| 143 |
} |
| 144 |
_ => {} |
| 145 |
} |
| 146 |
|
| 147 |
refs |
| 148 |
} |
| 149 |
|
| 150 |
fn collect_references_from_dict(dict: &lopdf::Dictionary) -> HashSet<ObjectId> { |
| 151 |
let mut refs = HashSet::new(); |
| 152 |
|
| 153 |
for (_key, value) in dict.iter() { |
| 154 |
refs.extend(collect_references_from_object(value)); |
| 155 |
} |
| 156 |
|
| 157 |
refs |
| 158 |
} |
| 159 |
|
| 160 |
fn describe_object(obj: &Object) -> String { |
| 161 |
match obj { |
| 162 |
Object::Dictionary(d) => { |
| 163 |
let type_info = d.get(b"Type") |
| 164 |
.ok() |
| 165 |
.and_then(|t| t.as_name().ok()) |
| 166 |
.map(|n| String::from_utf8_lossy(n).to_string()) |
| 167 |
.unwrap_or_else(|| "Unknown".to_string()); |
| 168 |
format!("Dictionary (Type: {})", type_info) |
| 169 |
} |
| 170 |
_ => format!("{:?}", obj) |
| 171 |
} |
| 172 |
} |