PDF to Image Conversion Utility

3.2.0 · active · verified Sun Apr 19

pdf2pic is a robust Node.js utility library designed for converting PDF documents into various image formats (like PNG, JPEG), base64 encoded strings, or raw image buffers. The current stable version is 3.2.0, with the project maintaining an active release cadence, frequently delivering patches and minor updates. A key differentiator of pdf2pic is its flexibility in output types and its ability to handle conversions from file paths, buffers, or base64 input. It acts as a wrapper around powerful external system dependencies: GraphicsMagick (or ImageMagick) for image processing and Ghostscript for PDF rendering. This design necessitates prior installation of these tools on the host system. The library ships with comprehensive TypeScript type definitions, enhancing the development experience for TypeScript users, and requires Node.js version 14 or higher.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize pdf2pic from a file path, convert a specific page to an image file, and includes robust error handling for common issues like missing external dependencies. It also shows an example of converting all pages in a PDF document.

import { fromPath } from "pdf2pic";
import path from "path";
import { promises as fs } from 'fs';

// Ensure the output directory exists
const savePath = path.resolve("./images");
await fs.mkdir(savePath, { recursive: true });

// In a real application, provide a full, valid path to your PDF.
// For this example, ensure 'sample.pdf' exists in your project root.
const pdfFilePath = path.resolve("./sample.pdf");

const options = {
  density: 100,
  saveFilename: "converted_page",
  savePath: savePath,
  format: "png",
  width: 600,
  height: 600
};

const convert = fromPath(pdfFilePath, options);
const pageToConvertAsImage = 1;

try {
  const result = await convert(pageToConvertAsImage, { responseType: "image" });
  console.log(`Page ${pageToConvertAsImage} converted successfully.`);
  console.log(`Output: ${path.join(result.path, result.name)}`);
} catch (error) {
  console.error("Error converting PDF page:", error.message);
  // Provide detailed error messages for common prerequisites issues
  if (error.message.includes("gm ENOENT")) {
    console.error("GraphicsMagick/ImageMagick not found. Ensure it's installed and in PATH.");
    console.error("See: https://github.com/yakovmeister/pdf2image#prerequisites");
  } else if (error.message.includes("gs ENOENT")) {
    console.error("Ghostscript not found. Ensure it's installed and in PATH.");
    console.error("See: https://github.com/yakovmeister/pdf2image#prerequisites");
  }
}

// Optional: Example for converting all pages
try {
  console.log("\nAttempting to convert all pages...");
  // To run this, you must have a 'sample.pdf' in your project root.
  // For testing, consider using a small dummy PDF.
  const bulkResult = await fromPath(pdfFilePath, options).bulk(-1, { responseType: "image" });
  console.log(`Successfully converted ${bulkResult.length} pages.`);
} catch (error) {
  console.error("Error converting all PDF pages:", error.message);
}

view raw JSON →