Cómo convertir un PDF a un archivo de imagen en Node.js

How to Convert a PDF to an Image File

This article was translated from English: Does it need improvement?
Translated
View the article in English

To convert PDF files to images, use the rasterizeToImageFiles method provided by IronPDF's NodeJS module. You can configure this method to support a variety of PDF-to-Image conversion operations. Convert PDFs to JPG, PNG, and other image formats. Convert every PDF page to a JPEG or PNG image, or convert only a few pages. IronPDF gives you full control.

Continue reading to learn how to do PDF to image tasks with IronPDF for Node.js!

Install IronPDF using NPM

npm install @ironsoftware/ironpdf
npm install @ironsoftware/ironpdf
SHELL

Install IronPDF's Node.js module from NPM to convert PDFs to PNG, JPG (or JPEG), GIF, and other image types.

Converting PDF to Image Format

Let's assume that we are working with a one-page sample PDF document containing placeholder text.

How to Convert a PDF to an Image File, Figure 1 An image depicting our sample PDF file opened in a PDF viewer application. Download this PDF file and others for testing purposes from Learning Container.

The source code below converts the PDF file to a PNG file.

import { PdfDocument } from "@ironsoftware/ironpdf";

// Convert PDF File to a PNG File
// The operation is performed asynchronously with promises
PdfDocument.fromFile("./sample-pdf-file.pdf").then((pdf) => {
    pdf.rasterizeToImageFiles("./images/sample-pdf-file.png");
    return pdf;
});

We use the PdfDocument.fromFile method to load our sample document into the Node library. This function gives a PdfDocument class in IronPDF representing our sample file. Since the object we need is contained in a Promise, we attach a callback function to run when the promise resolves the PdfDocument.

Inside the callback, we call the rasterizeToImageFiles function in IronPDF on the resolved object to convert the single-page document into an image. As shown above, we specify the destination path (which includes the filename and file extension) for our new image as an argument.

How to Convert a PDF to an Image File, Figure 2 The image was generated from the source code above. IronPDF converted our sample PDF into a PNG file in as little as three lines of code!

Learning Container provides sample PDF files that you can use in your projects for testing purposes. You can download the sample PDF file used in this example for free (along with similar sample files) from the website. Feel free to try the example above on other PDFs with different sizes and complexity.

The next section provides additional PDF-to-image conversion details worth considering.

Advanced Image Conversion Options

Convert PDF to JPEG

By default, rasterizeToImageFiles converts documents according to the file type specified in the destination path.

As such, to convert our sample PDF used in the previous example to a JPG file (instead of converting PDF to a PNG), we can simply change the file name extension used in the destination file path:

// Convert PDF to JPG (not to PNG)
pdf.rasterizeToImageFiles("./images/pdf-to-jpeg.jpg");

Another way to do the same thing is to specify an ImageType option in IronPDF. An ImageType value supersedes the image file type declared in the destination path. This forces rasterizeToImageFiles to not consider the filename when performing conversion.

You can see this in action in the next example. Here, we include a JSON options argument with our call to rasterizeToImageFiles that declares an ImageType.

import { PdfDocument, ImageType } from "@ironsoftware/ironpdf";

// Convert PDF to JPEG Format using ImageType.JPG
const options = {
   type: ImageType.JPG
};
PdfDocument.fromFile("./sample-pdf-file.pdf").then((pdf) => {
    pdf.rasterizeToImageFiles("./images/pdf-to-jpeg.png", options);
    return pdf;
});

Running the program above also creates a JPG image as in our previous example. Notice, however, that the destination filename still uses the PNG file extension. rasterizeToImageFiles ignored the .PNG file name extension used in the path, following instead the ImageType.JPG type value.

You can adapt this example to convert PDF into other image types, including GIF format and Bitmap format.

Tip: Using this approach can be particularly useful in situations in which changing filenames to specific types is not feasible or desired.

Converting PDF Files with Multiple Pages

To convert documents containing more than one page into a desired image type (PNG, JPG, Bitmap, etc.), we can also use the rasterizeToImageFiles method in the same manner as before. When invoked, the method will create each page as a separate image in the specified type.

How to Convert a PDF to Images, Figure 3 A two-page sample PDF document.

The next block of sample code generates two PNG files from the two-page PDF file example shown above.

import { PdfDocument } from "@ironsoftware/ironpdf";

// Convert PDF with two pages to a set of images.
PdfDocument.fromFile("./multipage-pdf.pdf").then((pdf) => {
    pdf.rasterizeToImageFiles("./images/multipage-pdf/multipage-pdf-page.png");
});

How to Convert a PDF to Images, Figure 4 The result of using the rasterizeToImageFiles method on a two-page PDF file. The method creates an image for each page of the original file.

Convert Specific PDF Pages to Images

Declaring a JSON object with the fromPages property set allows us to rasterize one or more pages from a multipage document (rather than all pages).

The following code example only converts the first, fourth, sixth, and ninth page of this large sample file into bitmaps.

import { PdfDocument, ImageType } from "@ironsoftware/ironpdf";

// Convert PDF containing many pages to BMP images.
const options = {
    type: ImageType.BMP,
    fromPages: [0, 3, 5, 8]  // Select only the desired pages
};
PdfDocument.fromFile("./sample-pdf-with-images.pdf").then((pdf) => {
    pdf.rasterizeToImageFiles("./images/multipage-selective-pdf/multipage-pdf-page.bmp", options);
});

How to Convert a PDF to Images, Figure 5 IronPDF performed the PDF-to-Image operation on only the pages that we specified in the options argument.

Get started with IronPDF

Further Reading

API Reference

Read the API documentation on the PdfDocument class and its rasterizeToImageFiles methods for more insights on how to adapt the method to suit your needs.

Code Examples

Preguntas Frecuentes

¿Cómo convierto un PDF en un archivo de imagen usando Node.js?

Puedes convertir un PDF en un archivo de imagen en Node.js usando el método `rasterizeToImageFiles` de IronPDF. Primero, instala el paquete IronPDF con npm install @ironsoftware/ironpdf. Luego, carga tu documento PDF usando PdfDocument.fromFile y llama a rasterizeToImageFiles para convertirlo al formato de imagen deseado, como PNG o JPG.

¿Cuáles son los pasos para instalar IronPDF en un proyecto de Node.js?

Para instalar IronPDF en un proyecto de Node.js, utiliza el administrador de paquetes npm con el comando npm install @ironsoftware/ironpdf. Esto agregará IronPDF a las dependencias de tu proyecto, permitiéndote usar sus características de conversión de PDF.

¿Puedo convertir un archivo PDF a múltiples formatos de imagen?

Sí, con IronPDF puedes convertir un archivo PDF a múltiples formatos de imagen como PNG, JPG, GIF y Bitmap. Puedes especificar el formato deseado usando la opción ImageType en el método rasterizeToImageFiles.

¿Es posible convertir solo ciertas páginas de un PDF en imágenes?

Sí, IronPDF te permite convertir páginas específicas de un PDF en imágenes. Puedes especificar las páginas que deseas convertir usando un objeto JSON con la propiedad fromPages, indicando los índices de las páginas.

¿Cómo puedo convertir un PDF de varias páginas en archivos de imagen separados?

Puedes convertir un PDF de varias páginas en archivos de imagen separados usando IronPDF al llamar al método rasterizeToImageFiles. Este método generará una imagen separada para cada página del PDF.

¿Cuál es la ventaja de usar la opción 'ImageType' en IronPDF?

La opción 'ImageType' en IronPDF te permite especificar directamente el formato de la imagen de salida, asegurando que la conversión resulte en el tipo de imagen correcto, incluso si la extensión de archivo especificada en la ruta es diferente.

¿Dónde puedo descargar archivos PDF de muestra para probar conversiones?

Puedes descargar archivos PDF de muestra para probar conversiones desde sitios web como Learning Container y africau.edu. Estos recursos ofrecen varios PDF de muestra que pueden usarse para probar las características de IronPDF.

¿Cómo puedo asegurarme de que la imagen convertida conserve la calidad del PDF original?

Para asegurar que la imagen convertida conserve la calidad del PDF original, utiliza la configuración predeterminada de IronPDF o ajusta la configuración de resolución en el método rasterizeToImageFiles para que coincida con tus requisitos de calidad.

Darrius Serrant
Ingeniero de Software Full Stack (WebOps)

Darrius Serrant tiene una licenciatura en Ciencias de la Computación de la Universidad de Miami y trabaja como Ingeniero de Marketing WebOps Full Stack en Iron Software. Atraído por la programación desde joven, vio la computación como algo misterioso y accesible, convirtiéndolo en el ...

Leer más
¿Listo para empezar?
Versión: 2025.11 recién lanzado