How-to guides/Optimization

Subset embedded fonts to reduce PDF size in Rust

Strip unused glyphs from embedded fonts so the PDF only carries the characters that actually appear in the document.

rust
use pdfluent::{Document, optimizer::FontSubsetOptions};

fn main() -> pdfluent::Result<()> {
    let mut doc = Document::open("input.pdf")?;

    let opts = FontSubsetOptions {
        subset_type1: false,  // Type1 subsetting is lossy; skip by default
        subset_truetype: true,
        subset_opentype: true,
    };

    doc.subset_fonts(opts)?;
    doc.save("subsetted.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the PDF and inspect font usage

Before subsetting, you can list embedded fonts and their sizes to understand what will change.

rust
let doc = Document::open("input.pdf")?;
for font in doc.fonts() {
    println!(
        "{} ({:?}) embedded={} size={}B",
        font.name(),
        font.font_type(),
        font.is_embedded(),
        font.embedded_size_bytes().unwrap_or(0),
    );
}
2

Build subsetting options

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.

rust
use pdfluent::optimizer::FontSubsetOptions;

let opts = FontSubsetOptions {
    subset_type1: false,
    subset_truetype: true,
    subset_opentype: true,
};
3

Run the subsetter

PDFluent scans every page content stream, collects the Unicode codepoints actually used, then rewrites each embedded font to contain only those glyphs.

rust
let mut doc = Document::open("input.pdf")?;
doc.subset_fonts(opts)?;
4

Compare sizes

Check the font sizes again after subsetting to measure the reduction.

rust
for font in doc.fonts() {
    println!(
        "{} size after={}B",
        font.name(),
        font.embedded_size_bytes().unwrap_or(0),
    );
}
5

Save the output

Write the subsetted file. Combine with compress_streams() for maximum size reduction.

rust
doc.compress_streams()?;
doc.save("subsetted.pdf")?;

Notes and tips

  • A font with 80,000 glyphs used for two characters will be reduced from several MB to a few KB after subsetting.
  • Subsetting marks the font with a 6-character prefix tag (e.g. ABCDEF+FontName) per the PDF spec.
  • Subsetting is safe for archiving: PDF/A-3 conformance allows subset fonts.
  • Do not subset fonts in documents where end users may add text later; they would need the full font to type new characters.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions