| 1 |
use lopdf::{Document, SaveOptions}; |
| 2 |
use std::path::Path; |
| 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 |
// Test with the PDF from the Downloads folder |
| 25 |
let pdf_path = "/Users/nicolasdao/Downloads/pdfs/pdf-demo.pdf"; |
| 26 |
|
| 27 |
if !Path::new(pdf_path).exists() { |
| 28 |
eprintln!("Test PDF not found at: {}", pdf_path); |
| 29 |
return Ok(()); |
| 30 |
} |
| 31 |
|
| 32 |
println!("Testing object stream compression with real PDF..."); |
| 33 |
println!("Input: {}", pdf_path); |
| 34 |
|
| 35 |
// Load the PDF |
| 36 |
let mut doc = load_document(pdf_path)?; |
| 37 |
|
| 38 |
// Save without object streams |
| 39 |
let mut normal_output = Vec::new(); |
| 40 |
doc.save_to(&mut normal_output)?; |
| 41 |
|
| 42 |
// Save with object streams |
| 43 |
let options = SaveOptions::builder() |
| 44 |
.use_object_streams(true) |
| 45 |
.use_xref_streams(true) |
| 46 |
.build(); |
| 47 |
|
| 48 |
let mut compressed_output = Vec::new(); |
| 49 |
doc.save_with_options(&mut compressed_output, options)?; |
| 50 |
|
| 51 |
// Calculate results |
| 52 |
let original_size = std::fs::metadata(pdf_path)?.len(); |
| 53 |
let normal_size = normal_output.len(); |
| 54 |
let compressed_size = compressed_output.len(); |
| 55 |
|
| 56 |
println!("\nFile sizes:"); |
| 57 |
println!(" Original file: {} bytes", original_size); |
| 58 |
println!(" After re-save (no compression): {} bytes", normal_size); |
| 59 |
println!(" With object streams: {} bytes", compressed_size); |
| 60 |
|
| 61 |
let reduction_from_original = (1.0 - (compressed_size as f64 / original_size as f64)) * 100.0; |
| 62 |
let reduction_from_resave = (1.0 - (compressed_size as f64 / normal_size as f64)) * 100.0; |
| 63 |
|
| 64 |
println!("\nSize reduction:"); |
| 65 |
println!(" From original: {:.1}%", reduction_from_original); |
| 66 |
println!(" From re-saved: {:.1}%", reduction_from_resave); |
| 67 |
|
| 68 |
// Check compression details |
| 69 |
let content = String::from_utf8_lossy(&compressed_output); |
| 70 |
let objstm_count = content.matches("/ObjStm").count(); |
| 71 |
|
| 72 |
println!("\nCompression details:"); |
| 73 |
println!(" Object streams created: {}", objstm_count); |
| 74 |
|
| 75 |
// Count how many objects are in the PDF |
| 76 |
let total_objects = doc.objects.len(); |
| 77 |
println!(" Total objects in PDF: {}", total_objects); |
| 78 |
|
| 79 |
// Save to file for manual inspection |
| 80 |
let output_path = "/Users/nicolasdao/Downloads/pdfs/pdf-demo_fixed_compression.pdf"; |
| 81 |
std::fs::write(output_path, &compressed_output)?; |
| 82 |
println!("\nCompressed PDF saved to: {}", output_path); |
| 83 |
|
| 84 |
if reduction_from_original > 20.0 { |
| 85 |
println!("\n✅ SUCCESS: Achieved {:.1}% size reduction!", reduction_from_original); |
| 86 |
} else { |
| 87 |
println!("\n⚠️ Size reduction is {:.1}% (expected 26-38%)", reduction_from_original); |
| 88 |
} |
| 89 |
|
| 90 |
Ok(()) |
| 91 |
} |