Convert PDFs to Images in Node.js

With PSPDFKit for Node.js, you can render independent pages of a PDF document and save them as images. If a document contains any annotations, they’ll be rendered over the page background.

Rendering and Exporting a Specific Page as an Image

The following example saves a single page from a document as a PNG file:

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

const doc = fs.readFileSync('source.pdf');

const instance = await load({ document: doc });
const pageWidth = instance.getDocumentInfo().pages[0].width;
const result = await instance.renderPage(0, { width: pageWidth });

fs.writeFileSync('page.png', Buffer.from(result));
await instance.close();

By default, the image will be exported as PNG. You can choose to export it to WebP instead by modifying the call to instance.renderPage() as follows:

const result = await instance.renderPage(0, { width: pageWidth }, 'webp');