Convert PDFs to images in Node.js

With Nutrient Node.js SDK, 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 "@nutrient-sdk/node";

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"
);