How to Convert a PDF to an Image File
To convert PDF files to images in Node.js, use IronPDF's rasterizeToImageFiles method. This method supports converting PDFs to JPG, PNG, GIF, and other formats, with options to convert all pages or specific pages. The library handles the conversion process efficiently, maintaining high image quality while providing flexibility in output format selection.
Quickstart: Convert PDF to Image
- Install IronPDF:
npm install @ironsoftware/ironpdf - Import the
PdfDocumentclass - Load your PDF file using
PdfDocument.fromFile() - Call
rasterizeToImageFiles()with your output path - The method automatically detects format from file extension
:title=Quick PDF to PNG Conversion
import { PdfDocument } from "@ironsoftware/ironpdf";
PdfDocument.fromFile("./sample.pdf").then((pdf) => {
pdf.rasterizeToImageFiles("./output.png");
});To convert PDF files to images, use the rasterizeToImageFiles method provided by IronPDF's Node.js module. Configure this method to convert PDFs to JPG, PNG, and other image formats. Convert every page or select specific pages. IronPDF gives you full control over the conversion process.
How Do I Install IronPDF for PDF to Image Conversion?
npm install @ironsoftware/ironpdfnpm install @ironsoftware/ironpdfInstall IronPDF's Node.js module from NPM to convert PDFs to PNG, JPG, GIF, and other image types.
Before converting PDFs to images, configure your license key if using IronPDF in production. The library requires the IronPDF Engine to be installed on your system, which handles the PDF rendering operations.
How Do I Convert a PDF to an Image?
We'll work with a one-page sample PDF document containing placeholder text. For more complex scenarios, consider converting HTML to PDF first and then converting the result to images.
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;
}).catch((error) => {
console.error("Error converting PDF to image:", error);
});We use PdfDocument.fromFile to load our sample document. This function returns a PdfDocument class representing our sample file. Since the object is contained in a Promise, we attach a callback function to run when the promise resolves.
Inside the callback, we call rasterizeToImageFiles on the resolved object to convert the single-page document into an image. We specify the destination path (including filename and file extension) as an argument. The method supports various formats similar to how IronPDF handles image embedding in PDFs.
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 for testing. Download the sample PDF file used in this example for free. Try the example above on other PDFs with different sizes and complexity. For more detailed image conversion examples, check the comprehensive PDF to Images guide.
What Advanced Image Conversion Options Are Available?
How Do I Convert PDF to JPEG Format?
By default, rasterizeToImageFiles converts documents according to the file type specified in the destination path. This automatic format detection allows easy switching between image formats without code changes.
To convert our sample PDF to a JPG file, change the file extension in the destination path:
// Convert PDF to JPG (not to PNG)
pdf.rasterizeToImageFiles("./images/pdf-to-jpeg.jpg");Alternatively, specify an ImageType option. An ImageType value overrides the image file type declared in the destination path. This forces rasterizeToImageFiles to ignore the filename when performing conversion, providing explicit control over the output format.
The next example includes 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,
dpi: 300 // High quality output
};
PdfDocument.fromFile("./sample-pdf-file.pdf").then((pdf) => {
pdf.rasterizeToImageFiles("./images/pdf-to-jpeg.png", options);
return pdf;
});This program creates a JPG image. Notice the destination filename still uses the PNG file extension. rasterizeToImageFiles ignores the .PNG extension, following the ImageType.JPG value instead. This approach is similar to how IronPDF handles other conversion operations where explicit type specification takes precedence.
You can adapt this example to convert PDFs to other image types, including GIF and Bitmap formats.
Tip: This approach is useful when changing filenames to specific types isn't feasible, such as when working with automated file naming systems or maintaining consistent naming conventions across different output formats.
How Do I Convert Multi-Page PDFs to Images?
To convert documents with multiple pages, use the rasterizeToImageFiles method in the same manner. The method creates each page as a separate image in the specified type, automatically appending page numbers to filenames. This functionality is useful when creating image galleries or working with multi-page documents.
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");
});
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.
How Do I Convert Only Specific Pages?
Use a JSON object with the fromPages property to rasterize specific pages from a multipage document. This selective conversion is useful when dealing with large PDFs where you only need certain pages, similar to how you might remove specific pages from a PDF.
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 (0-indexed)
dpi: 150 // Balance quality and file size
};
PdfDocument.fromFile("./sample-pdf-with-images.pdf").then((pdf) => {
pdf.rasterizeToImageFiles("./images/multipage-selective-pdf/multipage-pdf-page.bmp", options);
}).catch((error) => {
console.error("Failed to convert pages:", error);
});
IronPDF performed the PDF-to-Image operation on only the pages that we specified in the options argument.
The selective page conversion feature works with all supported image formats and maintains the quality settings you specify. For more complex page manipulations, explore the comprehensive API reference which details all available options and methods.
Get started with IronPDF
Where Can I Find More Resources?
Where Can I Find the API Documentation?
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. The documentation provides detailed information about all available parameters and options.
What Other Code Examples Are Available?
- Convert a PDF to Images using IronPDF: See
rasterizeToImageFilesused in a slightly different way with additional configuration options. - Convert Images to PDF Files using IronPDF: Learn how to convert one or more images into a single PDF file, essentially the reverse operation.
- Multi-Frame TIFF to PDF Conversion: Explore how to handle multi-page image formats when converting to PDF, which shares similar concepts with multi-page PDF to image conversion.
Frequently Asked Questions
How do I convert a PDF to an image in Node.js?
Use IronPDF's `rasterizeToImageFiles` method to convert PDF files to images in Node.js. First install IronPDF with `npm install @ironsoftware/ironpdf`, then load your PDF using `PdfDocument.fromFile()` and call `rasterizeToImageFiles()` with your desired output path. The method automatically detects the image format from the file extension.
What image formats are supported when converting PDFs?
IronPDF supports converting PDFs to multiple image formats including JPG, PNG, GIF, and other common formats. The `rasterizeToImageFiles` method automatically determines the output format based on the file extension you specify in the output path.
Can I convert specific pages of a PDF to images?
Yes, IronPDF allows you to convert either all pages or select specific pages when using the `rasterizeToImageFiles` method. This gives you full control over which pages of your PDF document are converted to image files.
What are the installation requirements for PDF to image conversion?
To convert PDFs to images, you need to install IronPDF using `npm install @ironsoftware/ironpdf`. The library also requires the IronPDF Engine to be installed on your system, which handles the PDF rendering operations. For production use, you'll need to configure your license key.
Does the conversion maintain high image quality?
Yes, IronPDF handles the conversion process efficiently while maintaining high image quality. The `rasterizeToImageFiles` method ensures that your PDF content is accurately rendered as images without significant quality loss.
Is the PDF to image conversion process asynchronous?
Yes, the conversion operation in IronPDF is performed asynchronously using promises. You can use `.then()` to handle the successful conversion or `.catch()` to handle any errors that may occur during the PDF to image conversion process.





