Flatten PDF Annotations in Node.js

Flattening is the process of embedding annotations in a PDF so that they can no longer be edited or removed. This leaves the visual representation of the annotations intact, but the annotations themselves are removed from the document.

Before you get started, make sure PSPDFKit for Node.js is up and running.

You can download and use either of the following sample documents for the examples in this guide:

To flatten a PDF document, specify { flatten: true } as a parameter when calling instance.exportPDF().

Example:

import fs from 'node:fs';
import { load } from '@pspdfkit/nodejs';

const pdfDoc = fs.readFileSync('source.pdf');
const myInstantJSON = JSON.parse(
	fs.readFileSync('instantjson.json', 'utf-8'),
);

const instance = await load({
	document: pdfDoc,
	instantJSON: myInstantJSON,
});
const buffer = await instance.exportPDF({ flatten: true });

fs.writeFileSync('output.pdf', Buffer.from(buffer));
await instance.close();