Flatten a PDF using JavaScript

To flatten a PDF using default settings.

JavaScript

1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3 const fl = await PDFNet.Flattener.create();
4 await fl.process(doc, PDFNet.Flattener.Mode.e_fast);
5}
6PDFNet.runWithCleanup(main);

Compress & optimize PDF files
Full code sample which shows how to use 'pdftron.PDF.Flattener' by simplifying page content (e.g. flattening complex graphics into images) while maintaining vector text.

About PDF flattener (or mobile PDF optimizer)

PDF Flattener component (pdftron.PDF.Flattener) can be used to speed-up PDF rendering on mobile devices and on the Web.

PDF documents can frequently contain very complex page description (e.g. thousands of paths, different shadings, color spaces, blend modes, large images etc.) that may not be suitable for interactive viewing on mobile devices.

Flattener can be used to speed-up PDF rendering on mobile devices and on the Web by simplifying page content (e.g. flattening complex graphics into images) while maintaining vector text whenever possible.

Flattener can also be used to simplify process of writing custom converters from PDF to other formats. In this case, Flattener can be used as first step in the conversion pipeline to reduce any PDF to a very simple representation (e.g. vector text on top of a background image).

A quick way to try Flattener is by using DocPub CLI (e.g. docpub -f pdf my.pdf).

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales