Embed, subset, and detect fonts in PDFs
Subset embedded fonts to reduce PDF size in Rust
use pdfluent::PdfDocument;
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open("report.pdf")?;
let _report = doc.subset_fonts()?;
doc.save("subset.pdf")?;
Ok(())
}- Step 1
Open the PDF and inspect font usage
Before subsetting, you can list embedded fonts and their sizes to understand what will change.
rustuse pdfluent::PdfDocument; let mut doc = PdfDocument::open("input.pdf")?; println!("{} pages", doc.page_count()); - Step 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// subset_fonts() removes unused glyphs from embedded fonts. // It is core-tier and takes no configuration in 1.0. - Step 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.
rustlet report = doc.subset_fonts()?; println!( "Subsetted {} of {} fonts, saved {} bytes", report.fonts_subsetted, report.fonts_processed, report.bytes_saved, ); - Step 4
Compare sizes
Check the font sizes again after subsetting to measure the reduction.
rust// The FontSubsetReport summarises the result: // fonts_processed - fonts_subsetted - bytes_saved println!("bytes saved: {}", report.bytes_saved); - Step 5
Save the output
Write the subsetted file. Combine with compress_streams() for maximum size reduction.
rustuse pdfluent::CompressOptions; doc.compress(CompressOptions::strict())?; doc.save("subsetted.pdf")?;
- 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.
- How much can subsetting reduce file size?
- Depends on the font and usage. A large CJK font embedded for Latin-only text shrinks the most; a Latin font with a few hundred glyphs shrinks much less.
- Is the visual output identical after subsetting?
- Yes. Subsetting removes glyphs that are not used, so rendering is byte-for-byte identical for the text in the document.
- Can I subset only specific fonts?
- Use doc.subset_font_by_name("FontName", opts) to target a specific font resource instead of processing all fonts at once.
- What happens to fonts that are already subsetted?
- PDFluent detects the 6-character prefix tag and skips re-processing fonts that are already subsetted.
Detect missing or unembedded fonts in a PDF in Rust
use pdfluent::PdfDocument;
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("input.pdf")?;
let missing: Vec<_> = doc
.fonts()
.filter(|f| !f.is_embedded() && !f.is_standard_14())
.collect();
if missing.is_empty() {
println!("All fonts are embedded.");
} else {
for font in &missing {
println!("Missing: {} ({:?})", font.name(), font.font_type());
}
std::process::exit(1);
}
Ok(())
}- Step 1
Open the PDF in read mode
For an audit task you only need a read-only Document.
rustlet doc = PdfDocument::open("input.pdf")?; - Step 2
Iterate over all font references
doc.fonts() returns an iterator over all font dictionaries referenced from any page resource dictionary in the document.
rustfor font in doc.fonts() { println!( "name={} type={:?} embedded={} standard14={}", font.name(), font.font_type(), font.is_embedded(), font.is_standard_14(), ); } - Step 3
Filter for unembedded non-standard fonts
Standard 14 fonts (Helvetica, Times-Roman, Courier, etc.) are provided by PDF viewers and do not need embedding. All other fonts should be embedded for reliable rendering.
rustlet unembedded: Vec<_> = doc .fonts() .filter(|f| !f.is_embedded() && !f.is_standard_14()) .collect(); - Step 4
Print a per-page report
To know which page each font appears on, iterate page by page.
rustfor (i, page) in doc.pages().enumerate() { for font in page.fonts() { if !font.is_embedded() && !font.is_standard_14() { println!("Page {}: unembedded font {}", i + 1, font.name()); } } } - Step 5
Return a non-zero exit code for CI gating
Use the missing font list to fail a CI pipeline when required fonts are absent.
rustif !unembedded.is_empty() { eprintln!("{} unembedded font(s) found", unembedded.len()); std::process::exit(1); }
- A font can be present in the resource dictionary but have an empty or missing font program stream. is_embedded() checks for the stream, not just the dictionary entry.
- Subset fonts are still considered embedded. The 6-character prefix tag does not affect the is_embedded check.
- Type3 fonts (custom glyph shapes) are always "embedded" by definition since the glyph procedures are in the PDF itself.
- Combine this check with the PDF/A validator for a full compliance audit.
- What happens when a font is missing at render time?
- The PDF viewer substitutes a fallback font, typically a sans-serif. The substitution may have different glyph widths, causing text reflow and layout differences.
- Does this tool check fonts in form fields?
- Yes. doc.fonts() includes fonts referenced from AcroForm field appearance streams, not just page content streams.
- How do I fix missing fonts after detecting them?
- Use doc.embed_font_data(name, data) with the font file bytes to embed the missing font. Then call doc.save() to write the corrected file.
- Are embedded fonts in XFA streams detected?
- XFA streams have their own font resources. Use doc.xfa_fonts() to inspect fonts referenced in the XFA subtree.