PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
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.
// 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.
| Name | Required | Type | Description | Default Value |
|---|---|---|---|---|
| imageQuality | no | number | Image recompression quality, from 0 to 1. A lower value gives a smaller file | 0.4 |
| flags | no | number | Optimization flags combined with bitwise OR (|). See Optimization Flags. The value replaces the default flags | 5767283 |
| ppi | no | object | Image resolution for color, grayscale, and monochrome images: { color, gray, mono }. Each is [upper, target]: images above upper PPI are downsampled to target PPI | color: [225, 150]gray: [225, 150]mono: [450, 300] |
| fontSubset | no | boolean | Embed only the glyphs used in the document | true |
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:
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.
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.
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 } });| Value | Description |
|---|---|
1 | Remove unused resources and elements |
1 << 1 | Remove empty objects |
1 << 2 | Remove special attributes |
1 << 3 | Optimize embedded font memory usage |
1 << 4 | Remove invalid links |
1 << 5 | Remove invalid bookmarks |
1 << 6 | Enable image compression |
1 << 7 | Remove bookmarks |
1 << 8 | Remove annotations |
1 << 9 | Remove forms |
1 << 10 | Remove multimedia content |
1 << 11 | Remove document info (author, title, etc.) |
1 << 12 | Remove metadata |
1 << 13 | Remove object data |
1 << 14 | Remove file attachments |
1 << 15 | Remove external cross-references |
1 << 16 | Remove other application data |
1 << 17 | Remove hidden layers |
1 << 18 | Merge visible layers |
1 << 19 | Use Flate (ZIP) compression |
1 << 20 | Replace LZW compression with Flate |
1 << 21 | Remove unused target objects |
1 << 22 | Optimize page content |
1 << 23 | Enable fast web view optimization (linearization) |
1 << 24 | Remove form submit/import/reset actions |
1 << 25 | Remove JavaScript actions |
1 << 26 | Remove page thumbnails |
1 << 27 | Remove and replace images |
1 << 28 | Remove labels |
1 << 29 | Remove print settings |
1 << 30 | Remove search index |