Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Compress & Optimize a PDF File ​

Pass the optimize option to Document.getFileData() to get compressed PDF data. Compression runs on a copy of the document, so the document open in the viewer is not changed.

javascript
// Import the JS file of ComPDFKit Web Demo.
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";

const viewer = document.getElementById('webviewer');
ComPDFKitViewer({
  path: '/',
  initialDoc: 'Your PDF Url',
  licenseKey: 'Input your license here'
}, viewer)
  .then((instance) => {
    const { documentViewer } = instance.Core;
    documentViewer.addEventListener('documentLoaded', async () => {
      // Compress with the default options.
      const bufferData = await documentViewer.getDocument().getFileData({ optimize: {} });
      const blobData = new Blob([bufferData], { type: 'application/pdf' });
    });
  });

The compressed data can be saved to the local file system or uploaded to a server in the same way as in Save a Document.

Optimization Options ​

NameRequiredTypeDescriptionDefault Value
imageQualitynonumberImage recompression quality, from 0 to 1. A lower value gives a smaller file0.4
flagsnonumberOptimization flags combined with bitwise OR (|). See Optimization Flags. The value replaces the default flags5767283
ppinoobjectImage resolution for color, grayscale, and monochrome images: { color, gray, mono }. Each is [upper, target]: images above upper PPI are downsampled to target PPIcolor: [225, 150]
gray: [225, 150]
mono: [450, 300]
fontSubsetnobooleanEmbed only the glyphs used in the documenttrue

The default flags only apply lossless cleanup: remove unused resources, empty objects, invalid links, and invalid bookmarks, compress images, use Flate compression, replace LZW with Flate, and optimize page content. Annotations, forms, bookmarks, and embedded fonts are kept.

This example compresses images more strongly and also removes page thumbnails, JavaScript actions, and file attachments:

javascript
const bufferData = await documentViewer.getDocument().getFileData({
  optimize: {
    imageQuality: 0.3,
    // Default flags + remove file attachments (1 << 14), JavaScript actions (1 << 25) and page thumbnails (1 << 26).
    flags: 5767283 | (1 << 14) | (1 << 25) | (1 << 26),
    ppi: { color: [120, 96], gray: [120, 96], mono: [120, 96] }
  }
});

optimize can be combined with the other options of getFileData(), such as flatten and xfdfString.

Get the Engine Defaults ​

Document.getOptimizeDefaults() returns the optimization defaults of the engine in the same shape as the optimize option, so they can be adjusted and passed back. Note that the engine's default flags (175767807) go beyond the defaults above: they also remove bookmarks, hidden layers, and JavaScript actions.

javascript
const doc = documentViewer.getDocument();
const defaults = await doc.getOptimizeDefaults();
// { flags: number, imageQuality: number, ppi: { color: [upper, target], gray: [upper, target], mono: [upper, target] } }

const bufferData = await doc.getFileData({ optimize: { ...defaults, imageQuality: 0.6 } });

Optimization Flags ​

ValueDescription
1Remove unused resources and elements
1 << 1Remove empty objects
1 << 2Remove special attributes
1 << 3Optimize embedded font memory usage
1 << 4Remove invalid links
1 << 5Remove invalid bookmarks
1 << 6Enable image compression
1 << 7Remove bookmarks
1 << 8Remove annotations
1 << 9Remove forms
1 << 10Remove multimedia content
1 << 11Remove document info (author, title, etc.)
1 << 12Remove metadata
1 << 13Remove object data
1 << 14Remove file attachments
1 << 15Remove external cross-references
1 << 16Remove other application data
1 << 17Remove hidden layers
1 << 18Merge visible layers
1 << 19Use Flate (ZIP) compression
1 << 20Replace LZW compression with Flate
1 << 21Remove unused target objects
1 << 22Optimize page content
1 << 23Enable fast web view optimization (linearization)
1 << 24Remove form submit/import/reset actions
1 << 25Remove JavaScript actions
1 << 26Remove page thumbnails
1 << 27Remove and replace images
1 << 28Remove labels
1 << 29Remove print settings
1 << 30Remove search index