modern-pdf-lib / optimizeAllImages
Function: optimizeAllImages()
optimizeAllImages(
doc,options?):Promise<OptimizationReport>
Defined in: src/assets/image/batchOptimize.ts:152
Optimize all images in a PDF document by recompressing them as JPEG.
Walks every image XObject in the document, decodes its pixel data, recompresses it as JPEG using the WASM encoder (if available), and replaces the stream data in-place when the result is smaller.
Requires the JPEG WASM module to be initialized via initJpegWasm() or initWasm({ jpeg: true }). Without it, no images will be optimized (all will be skipped).
Parameters
doc
A parsed PdfDocument (from loadPdf()).
options?
BatchOptimizeOptions = {}
Optimization settings.
Returns
Promise<OptimizationReport>
A report summarizing the optimization results.
Example
ts
import { loadPdf, initWasm, optimizeAllImages } from 'modern-pdf-lib';
await initWasm({ jpeg: true });
const doc = await loadPdf(pdfBytes);
const report = await optimizeAllImages(doc);
console.log(`Optimized ${report.optimizedImages} of ${report.totalImages} images`);
console.log(`Savings: ${report.savings.toFixed(1)}%`);
const optimizedBytes = await doc.save();