VOOZH about

URL: https://ironpdf.com/nodejs/examples/image-to-pdf/

⇱ Code Example: Convert Image to PDF | IronPDF for Node.js & .NET 10


Skip to footer content
Images To PDF
import {PdfGenerator} from "@ironsoftware/ironpdf";
import fs from 'fs';

(async () => {
 // Specify the directory path
 const directoryPath = './images';

 // Read the contents of the directory
 fs.readdir(directoryPath, (err, files) => {
 if (err) {
 console.error('Error reading directory:', err);
 return;
 }

 // Filter file names to include only .jpg and .jpeg extensions
 const jpegFiles = files.filter((file) =>
 file.toLowerCase().endsWith('.jpg') || file.toLowerCase().endsWith('.jpeg')
 );

 // Construct full file paths for the filtered files
 const filePaths = jpegFiles.map((file) => `${directoryPath}/${file}`);

 // Converts the images to a PDF and save it.
 const pdf = PdfGenerator.imageToPdf(filePaths).then(
 (returnedPdf)=> {
 returnedPdf.saveAs("composite.pdf");
 });
 
 // Also see PdfDocument.rasterizeToImageFiles() method to flatten a PDF to images or thumbnails
 });
})();

Images To PDF

To convert a single image to a PDF document, simply input the file path into the PdfGenerator.imageToPdf method and export the resulting PDF document.

Converting multiple images to a PDF involves using an array of file paths. Below is the complete guide with a properly formatted code snippet.

Code Explanation

  1. Import Required Modules:

    • fs: A core Node.js module for interacting with the file system.
    • path: A core module for handling and transforming file paths.
    • PdfGenerator: A module from the IronPDF library used for generating PDFs.
  2. Read Directory:

    • fs.readdir: Reads the directory specified by imageDirectoryPath.
    • Filters and returns files that have .jpg or .jpeg extensions.
  3. Build Full File Paths:

    • Uses path.join to construct the full paths of filtered image files.
  4. Convert Images to PDF:

    • The PdfGenerator.imageToPdf method takes the array of image paths and converts them to a single PDF.
  5. Save the PDF:
    • The saveAs method saves the generated PDF as composite.pdf.

For more details on converting images to PDFs using IronPDF, visit the IronPDF product page.

Explore Python PDF to Image Conversion Guide

Ready to Get Started?
Version: 2026.5 just released

Still Scrolling?

Want proof fast?
run a sample watch your HTML become a PDF.

Try IronPDF for Free
Get Set Up in 5 Minutes
  1. Download and install Node.js 12+.
  2. Execute the above command in the terminal.
Licenses from $749

Have a question? Get in touch with our development team.

Now you've installed with NPM
Your browser is now downloading IronPDF

Next step: Start free 30-day Trial

No credit card required

  • Test in a live environment
  • Fully-functional product
  • 24/5 technical support

Thank You

Your trial key should be in the email.
If it is not, please contact
support@ironsoftware.com
Get your free 30-day Trial Key instantly.
Thank you.
If you'd like to speak to our licensing team:
πŸ‘ badge_greencheck_in_yellowcircle
The trial form was submitted
successfully.

Your trial key should be in the email.
If it is not, please contact
support@ironsoftware.com

Have a question? Get in touch with our development team.
No credit card or account creation required
Now you've installed with NPM
Your browser is now downloading IronPDF

Next step: Start free 30-day Trial

No credit card required

  • Test in a live environment
  • Fully-functional product
  • 24/5 technical support
Thank you.
View your license options:
Thank you.
If you'd like to speak to our licensing team:
Have a question? Get in touch with our development team.
Have a question? Get in touch with our development team.
Talk to Sales Team

Book a No-obligation Consult

How we can help:
  • Consult on your workflow & pain points
  • See how other companies solve their .NET document needs
  • All your questions answered to make sure you have all the information you need. (No commitment whatsoever.)
  • Get a tailored quote for your project's needs
Get Your No-Obligation Consult

Complete the form below or email sales@ironsoftware.com

Your details will always be kept confidential.

Trusted by Millions of Engineers Worldwide
Book Free Live Demo

Book a 30-minute, personal demo.

No contract, no card details, no commitments.

Here's what to expect:
  • A live demo of our product and its key features
  • Get project specific feature recommendations
  • All your questions are answered to make sure you have all the information you need.
    (No commitment whatsoever.)
CHOOSE TIME
YOUR INFO
Book your free Live Demo

Trusted by Millions of Engineers Worldwide

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me