Compress images, remove duplicate streams, and optimize cross-reference tables. Reduce file size by 40-80% on typical documents.
use pdfluent::{Sdk, compress::{CompressOptions, ImageQuality}};
fn main() -> pdfluent::Result<()> {
let sdk = Sdk::new()?;
let doc = sdk.open("presentation.pdf")?;
let original_size = doc.file_size();
let opts = CompressOptions::builder()
.image_quality(ImageQuality::Jpeg(75))
.image_dpi_target(150)
.deduplicate_streams(true)
.compress_object_streams(true)
.subset_fonts(true)
.linearize(true)
.build();
let compressed = doc.compress(opts)?;
compressed.save("presentation_compressed.pdf")?;
let new_size = compressed.file_size();
let reduction = 100 - (new_size * 100 / original_size);
println!("Reduced by {}% ({} -> {} bytes)", reduction, original_size, new_size);
Ok(())
}Run cargo add pdfluent@1.0.0-beta.5 to get started.
Resample embedded images to a target DPI and re-encode with JPEG at a configurable quality level (1-100). Images already below the target DPI are left unchanged. Supports JPEG, JPEG 2000, and lossless PNG targets.
Identify identical content streams — common in PDFs generated by tools that embed the same logo or background on every page. Duplicate streams are replaced with a single shared object, reducing file size proportionally to the number of duplicates.
Pack indirect objects into compressed object streams using Flate/zlib (PDF 1.5+ cross-reference streams). Reduces the overhead of object headers and cross-reference tables, which can account for 10-20% of file size in large documents.
Remove unused glyphs from embedded fonts. A document using only ASCII characters in a full Unicode font can have its font stream reduced by 80% or more. Subsetting preserves all glyphs that appear in the document.
Reorganize the PDF structure so the first page loads before the rest of the file is downloaded. Combined with compression, this cuts perceived load time for web-served PDFs. Linearization is compatible with all other compression options.
Compress thousands of documents in parallel. Pass a glob pattern and output directory; PDFluent processes each file in a Rayon thread pool and reports per-file size reduction. Failed files are logged without stopping the batch.