How to convert PDF to image in Node.js

Table of contents

    How to convert PDF to image in Node.js
    Summary
    • 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

    For the pdf2pic approach, you’ll also need:

    GraphicsMagick installation

    Check if GraphicsMagick is installed:

    Terminal window
    gm --version

    If not installed:

    • macOS (using Homebrew):
    Terminal window
    brew install graphicsmagick
    • Linux (using apt):
    Terminal window
    sudo apt-get install graphicsmagick

    Ghostscript installation

    Check if Ghostscript is installed:

    Terminal window
    gs --version

    If not installed:

    • macOS (using Homebrew):
    Terminal window
    brew install ghostscript
    • Linux (using apt):
    Terminal window
    sudo apt-get install ghostscript

    Setting up the project

    1. Create a new directory for your project.

    2. Initialize a new Node.js project:

      Terminal window
      npm init -y
    3. Install the pdf2pic library:

      Terminal window
      npm install pdf2pic
    4. Add "type": "module" to your package.json to 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

    1. Import the fromPath function from pdf2pic:

      import { fromPath } from "pdf2pic";
    2. Define conversion options:

      const options = {
      density: 100,
      saveFilename: "untitled",
      format: "png",
      width: 600,
      height: 600,
      };
    3. Initialize the conversion:

      const convert = fromPath("example.pdf", options);
      const pageToConvertAsImage = 1;
    4. 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 situationRecommendation
    Local dev or simple scriptspdf2pic is fine if you don’t mind installing dependencies
    Production deploymentNutrient avoids dependency management headaches
    Docker/serverlessNutrient — no system packages to install in your container
    PDFs with annotationsNutrient preserves annotations; pdf2pic may not
    Multiple document typesNutrient also handles Office docs and images

    Integrating Nutrient Node.js SDK

    1. Initialize a new Node.js project:

      Terminal window
      npm init -y
    2. Install the Nutrient Node.js SDK package:

      Terminal window
      npm install @nutrient-sdk/node
    3. Place your PDF document (e.g. document.pdf) in your project directory.

    4. 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 license property with key and appName when calling load(). For a trial license, contact Sales.

    5. Run the script:

    Terminal window
    node index.js

    Choosing 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:

    FAQ

    How do I install GraphicsMagick and Ghostscript?
    How do I convert a PDF to an image using pdf2pic?

    Install pdf2pic with npm install pdf2pic, import fromPath, configure options (density, format, dimensions), and call the convert function with the page number.

    What makes Nutrient Node.js SDK different from pdf2pic?

    Nutrient runs standalone without GraphicsMagick or Ghostscript. It also preserves annotations in the output and handles multiple document types beyond PDF.

    Can Nutrient export images in formats other than PNG?

    Yes, Nutrient supports PNG and WebP output formats.

    How can I try Nutrient Node.js SDK?

    Start a free trial or try the demo.

    Hulya Masharipov

    Hulya Masharipov

    Technical Writer

    Hulya is a frontend web developer and technical writer who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

    Explore related topics

    Try for free Ready to get started?