Strip unused glyphs from embedded fonts so the PDF only carries the characters that actually appear in the document.
use pdfluent::PdfDocument;
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open("report.pdf")?;
let _report = doc.subset_fonts()?;
doc.save("subset.pdf")?;
Ok(())
}Before subsetting, you can list embedded fonts and their sizes to understand what will change.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("input.pdf")?;
println!("{} pages", doc.page_count());FontSubsetOptions controls which font types are processed. Type 1 fonts often have limited glyph sets already; focus on TrueType and OpenType where gains are largest.
// subset_fonts() removes unused glyphs from embedded fonts.
// It is core-tier and takes no configuration in 1.0.PDFluent scans every page content stream, collects the Unicode codepoints actually used, then rewrites each embedded font to contain only those glyphs.
let report = doc.subset_fonts()?;
println!(
"Subsetted {} of {} fonts, saved {} bytes",
report.fonts_subsetted, report.fonts_processed, report.bytes_saved,
);Check the font sizes again after subsetting to measure the reduction.
// The FontSubsetReport summarises the result:
// fonts_processed - fonts_subsetted - bytes_saved
println!("bytes saved: {}", report.bytes_saved);Write the subsetted file. Combine with compress_streams() for maximum size reduction.
use pdfluent::CompressOptions;
doc.compress(CompressOptions::strict())?;
doc.save("subsetted.pdf")?;No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.