How to convert PDF to image in Node.js
Table of contents
- pdf2pic — Free, good for local scripts. Requires GraphicsMagick and Ghostscript on your server.
- Nutrient Node.js SDK — Deploy anywhere with zero system dependencies. One SDK for PDF thumbnails, annotated exports, Office conversion, and more.
In this tutorial, you’ll learn how to convert PDF pages to images using Node.js. In the first part, you’ll use the pdf2pic(opens in a new tab) library. In the second part, you’ll use Nutrient Node.js SDK, which requires no external dependencies.
Prerequisites
- Node.js(opens in a new tab) installed on your system
- npm (comes bundled with Node.js)
For the pdf2pic approach, you’ll also need:
- GraphicsMagick(opens in a new tab) — Image processing library used by pdf2pic
- Ghostscript(opens in a new tab) — PDF interpreter used by pdf2pic
GraphicsMagick installation
Check if GraphicsMagick is installed:
gm --versionIf not installed:
- macOS (using Homebrew):
brew install graphicsmagick- Linux (using apt):
sudo apt-get install graphicsmagick- Windows: Download from the GraphicsMagick website(opens in a new tab).
Ghostscript installation
Check if Ghostscript is installed:
gs --versionIf not installed:
- macOS (using Homebrew):
brew install ghostscript- Linux (using apt):
sudo apt-get install ghostscript- Windows: Download from the Ghostscript website(opens in a new tab).
Setting up the project
Create a new directory for your project.
Initialize a new Node.js project:
Terminal window npm init -yInstall the pdf2pic library:
Terminal window npm install pdf2picAdd
"type": "module"to yourpackage.jsonto use ES modules:{"name": "your-project-name","version": "1.0.0","main": "index.js","type": "module","dependencies": {"pdf2pic": "^3.1.3"}}
Converting PDF to images using pdf2pic
Import the
fromPathfunction from pdf2pic:import { fromPath } from "pdf2pic";Define conversion options:
const options = {density: 100,saveFilename: "untitled",format: "png",width: 600,height: 600,};Initialize the conversion:
const convert = fromPath("example.pdf", options);const pageToConvertAsImage = 1;Perform the conversion:
async function convertPdfPageToImage() {try {const result = await convert(pageToConvertAsImage, {responseType: "image",});console.log("Page 1 converted to image successfully");return result;} catch (error) {console.error("Conversion error:", error);}}convertPdfPageToImage();
Check your project directory for the generated image file.
Nutrient Node.js SDK
pdf2pic requires installing GraphicsMagick and Ghostscript on every server that runs your code. That’s fine for local development, but becomes a maintenance burden in production — especially with containerized deployments or serverless environments.
Nutrient Node.js SDK runs standalone. No system dependencies to install, no version conflicts to debug, no additional setup in your CI/CD pipeline.
What you can build with Nutrient
- Generate PDF thumbnails — Create preview images for document libraries, search results, or file browsers without external tools.
- Render annotated documents — Export PDFs with comments, highlights, and form data visible in the output image. Useful for review workflows or archiving.
- Build document pipelines — Use the same SDK for PDF-to-image, image-to-PDF, and Office conversion. One dependency handles your entire document workflow.
- Deploy anywhere — No GraphicsMagick or Ghostscript to install. Works in Docker, AWS Lambda, Vercel, or any Node.js environment.
When to choose Nutrient over pdf2pic
| Your situation | Recommendation |
|---|---|
| Local dev or simple scripts | pdf2pic is fine if you don’t mind installing dependencies |
| Production deployment | Nutrient avoids dependency management headaches |
| Docker/serverless | Nutrient — no system packages to install in your container |
| PDFs with annotations | Nutrient preserves annotations; pdf2pic may not |
| Multiple document types | Nutrient also handles Office docs and images |
Integrating Nutrient Node.js SDK
Initialize a new Node.js project:
Terminal window npm init -yInstall the Nutrient Node.js SDK package:
Terminal window npm install @nutrient-sdk/nodePlace your PDF document (e.g. document.pdf) in your project directory.
Create a new JavaScript file named
index.js. This script handles the conversion:const { load } = require("@nutrient-sdk/node");const fs = require("fs");(async () => {const doc = fs.readFileSync("document.pdf");const instance = await load({ document: doc });const pageWidth = instance.getDocumentInfo().pages[0].width;const result = await instance.renderPage(0, { width: pageWidth });fs.writeFileSync("image.png", Buffer.from(result));instance.close();})();This reads a PDF, renders the first page at its original width, and saves it as
image.png. Annotations in the PDF are rendered onto the output image.By default, the output will include a Nutrient watermark. To remove it, specify a
licenseproperty withkeyandappNamewhen callingload(). For a trial license, contact Sales.Run the script:
node index.jsChoosing the image format
By default, Nutrient exports PNG. To export as WebP instead:
const result = await instance.renderPage(0, { width: pageWidth }, "webp");Conclusion
pdf2pic works for local scripts or when you control the server environment. But if you’re deploying to production — especially containers or serverless — managing GraphicsMagick and Ghostscript adds friction.
Nutrient Node.js SDK removes that friction. One npm install, no system dependencies. And when your requirements grow beyond PDF-to-image, the same SDK handles Office conversion, image-to-PDF, and document editing.
To get started:
- Start your free trial to test the SDK in your application.
- Try the demo to see document conversion in action.
FAQ
- macOS:
brew install graphicsmagick ghostscript - Linux:
sudo apt-get install graphicsmagick ghostscript - Windows: Download from the official websites (GraphicsMagick(opens in a new tab), Ghostscript(opens in a new tab))
Install pdf2pic with npm install pdf2pic, import fromPath, configure options (density, format, dimensions), and call the convert function with the page number.
Nutrient runs standalone without GraphicsMagick or Ghostscript. It also preserves annotations in the output and handles multiple document types beyond PDF.
Yes, Nutrient supports PNG and WebP output formats.
Start a free trial or try the demo.