Skip to footer content

How to Convert a PDF to an Image File in Node.js

In this comprehensive tutorial, you'll learn how to convert a PDF file to image files using IronPDF in JavaScript. The process begins with ensuring IronPDF is installed and importing the necessary classes, such as PDFDocument and ImageType from the IronPDF package. We start by loading an existing PDF document using the fromFile method, specifically sample-pdf-file.pdf. Once imported, the PDF's pages are extracted and saved as image files using the rasterize method.

// Import the necessary classes from the IronPDF package
const { PDFDocument, ImageType } = require('iron-pdf');

// Function to convert an entire PDF document into image files
async function convertPdfToImages() {
  try {
    // Load the PDF document using fromFile method
    const pdfDoc = await PDFDocument.fromFile('sample-pdf-file.pdf');

    // Define the output directory for the images
    const outputDirectory = './output';

    // Rasterize the PDF to images
    await pdfDoc.rasterize(outputDirectory, ImageType.PNG);

    console.log('PDF successfully converted to images');
  } catch (error) {
    console.error('Error during PDF to image conversion:', error);
  }
}

convertPdfToImages();
// Import the necessary classes from the IronPDF package
const { PDFDocument, ImageType } = require('iron-pdf');

// Function to convert an entire PDF document into image files
async function convertPdfToImages() {
  try {
    // Load the PDF document using fromFile method
    const pdfDoc = await PDFDocument.fromFile('sample-pdf-file.pdf');

    // Define the output directory for the images
    const outputDirectory = './output';

    // Rasterize the PDF to images
    await pdfDoc.rasterize(outputDirectory, ImageType.PNG);

    console.log('PDF successfully converted to images');
  } catch (error) {
    console.error('Error during PDF to image conversion:', error);
  }
}

convertPdfToImages();
JAVASCRIPT

The tutorial then introduces additional functionality, focusing on converting specific pages of the PDF to images in a different format. By utilizing an options object, users can set the image type to BMP and select specific pages for conversion, such as pages 0, 3, 5, and 8. The fromFile method is used again to load the PDF, followed by rasterizing the selected pages to BMP images, saved in a designated directory.

// Function to convert specific pages of a PDF document into image files
async function convertSpecificPagesToImages() {
  try {
    // Load the PDF document 
    const pdfDoc = await PDFDocument.fromFile('sample-pdf-file.pdf');

    // Specify the output directory 
    const outputDirectory = './output_specific_pages';

    // Options for specific pages and image type
    const options = {
      imageType: ImageType.BMP,
      pages: [0, 3, 5, 8], // Specify pages to convert
    };

    // Rasterize specific pages to BMP images with options
    await pdfDoc.rasterize(outputDirectory, options);

    console.log('Specific PDF pages successfully converted to BMP images');
  } catch (error) {
    console.error('Error during specific page conversion:', error);
  }
}

convertSpecificPagesToImages();
// Function to convert specific pages of a PDF document into image files
async function convertSpecificPagesToImages() {
  try {
    // Load the PDF document 
    const pdfDoc = await PDFDocument.fromFile('sample-pdf-file.pdf');

    // Specify the output directory 
    const outputDirectory = './output_specific_pages';

    // Options for specific pages and image type
    const options = {
      imageType: ImageType.BMP,
      pages: [0, 3, 5, 8], // Specify pages to convert
    };

    // Rasterize specific pages to BMP images with options
    await pdfDoc.rasterize(outputDirectory, options);

    console.log('Specific PDF pages successfully converted to BMP images');
  } catch (error) {
    console.error('Error during specific page conversion:', error);
  }
}

convertSpecificPagesToImages();
JAVASCRIPT

Upon running the code, images are extracted to the specified folder, with each PDF page converted into a separate BMP image. This conversion results in high-quality images suitable for various applications. The tutorial concludes by encouraging viewers to subscribe for more content and to explore the software by downloading it from the provided link.

Further Reading: How to Convert a PDF to an Image File

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity and problem-solving.

At Iron Software, Darrius enjoys creating new things and simplifying complex concepts to make them more understandable. As one of our resident developers, he has also volunteered to teach students, sharing his expertise with the next generation.

For Darrius, his work is fulfilling because it is valued and has a real impact.

Install with npm

Version: 2025.6

> npm i @ironsoftware/ironpdf

Report an Issue