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
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.
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.
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.
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.
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);
});
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
- Convert a PDF to Images using IronPDF: See
rasterizeToImageFiles
used in a slightly different way. - Convert Images to PDF Files using IronPDF: Learn how to convert one or more images into a single PDF file.
Frequently Asked Questions
What is IronPDF for Node.js used for?
IronPDF for Node.js is used to convert PDF files to image formats such as JPG, PNG, and others. It provides methods like 'rasterizeToImageFiles' for performing these conversions.
How do I install IronPDF in a Node.js project?
You can install IronPDF in a Node.js project using npm with the command 'npm install @ironsoftware/ironpdf'.
How can I convert a PDF to a PNG using IronPDF?
To convert a PDF to a PNG, use the 'PdfDocument.fromFile' method to load the PDF, then call 'rasterizeToImageFiles' with the destination path that includes the PNG file extension.
Can IronPDF convert specific pages of a PDF to images?
Yes, IronPDF can convert specific pages of a PDF to images by using a JSON object with the 'fromPages' property set to the desired page indices.
Is it possible to convert a PDF to a JPEG using IronPDF?
Yes, you can convert a PDF to a JPEG by specifying the destination path with a JPG extension or using the 'ImageType.JPG' option in the 'rasterizeToImageFiles' method.
Does IronPDF support batch conversion of PDF pages to images?
Yes, IronPDF supports batch conversion, where each page of a multi-page PDF can be converted to separate image files using the 'rasterizeToImageFiles' method.
What image formats does IronPDF support for conversion?
IronPDF supports converting PDFs to various image formats, including PNG, JPG, GIF, and Bitmap.
How can I specify the image format in the conversion process?
You can specify the image format in the conversion process by setting the 'type' option in a JSON object passed to 'rasterizeToImageFiles', which overrides the file extension in the path.
What is the advantage of using IronPDF's ImageType option?
Using the ImageType option allows you to specify the desired image format directly, which can be useful when the filename extension does not match the desired conversion output.
Where can I find sample PDF files for testing IronPDF?
Sample PDF files for testing IronPDF can be downloaded from websites like Learning Container and africau.edu.