How to Generate a PDF File in Node.js

In this comprehensive tutorial, we delve into generating PDF files in Node.js using the Iron PDF library. We begin by installing the necessary packages and setting up the environment. The tutorial covers importing essential modules, including PDF Generator, PDF Document, and Iron PDF Global Config, along with the FS module from Node.js. We demonstrate creating PDFs from HTML strings by converting a simple 'Hello World' HTML tag into a PDF. Next, we explore converting a Wikipedia page URL about PDFs into a PDF document, showcasing Iron PDF's ability to capture web pages in detail. The tutorial also covers converting images into a single PDF by filtering JPEGs from a directory and using PDF Generator's image-to-PDF functionality. By running the code in the terminal, we successfully generate and review the PDFs, displaying Iron PDF's capabilities to convert various sources into PDF documents. This powerful tool significantly enhances PDF generation possibilities in Node.js applications. For more tutorials and to explore IronPDF's features, subscribe to Iron Software's channel and sign up for a trial to experience the software firsthand.

Further Reading: How to Generate a PDF File in Node.js

# Bash code to install the Iron PDF library in your Node.js project.
npm install ironpdf
# Bash code to install the Iron PDF library in your Node.js project.
npm install ironpdf
SHELL
// Import necessary modules from Iron PDF and Node.js
const { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');
const fs = require('fs'); // File system module for handling file operations

// Initialize PDF Generator
const pdfGenerator = new PDFGenerator();

async function generatePdfFromHtml() {
    // HTML content to be converted into a PDF
    const htmlContent = '<h1>Hello World</h1>';

    try {
        // Convert HTML string to a PDF document
        const pdfDoc = await pdfGenerator.fromHtml(htmlContent);
        // Save the generated PDF document to a file
        await pdfDoc.saveAs('hello_world.pdf');
        console.log('PDF generated successfully from HTML string.');
    } catch (error) {
        console.error('Error generating PDF from HTML:', error);
    }
}

async function generatePdfFromWebPage() {
    // URL of the web page to be converted into a PDF
    const url = 'https://en.wikipedia.org/wiki/PDF';

    try {
        // Convert web page URL to a PDF document
        const pdfDoc = await pdfGenerator.fromUrl(url);
        // Save the generated PDF document to a file
        await pdfDoc.saveAs('wikipedia_pdf.pdf');
        console.log('PDF generated successfully from webpage URL.');
    } catch (error) {
        console.error('Error generating PDF from webpage:', error);
    }
}

async function generatePdfFromImages() {
    // Directory containing images
    const imageDir = './images';
    const imageFiles = fs.readdirSync(imageDir);

    try {
        // Filter only JPEG files from the directory
        const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg'));

        // Convert filtered images to a single PDF document
        const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`));
        // Save the generated PDF document to a file
        await pdfDoc.saveAs('images.pdf');
        console.log('PDF generated successfully from images.');
    } catch (error) {
        console.error('Error generating PDF from images:', error);
    }
}

// Execute the functions to generate PDFs
generatePdfFromHtml();
generatePdfFromWebPage();
generatePdfFromImages();
// Import necessary modules from Iron PDF and Node.js
const { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');
const fs = require('fs'); // File system module for handling file operations

// Initialize PDF Generator
const pdfGenerator = new PDFGenerator();

async function generatePdfFromHtml() {
    // HTML content to be converted into a PDF
    const htmlContent = '<h1>Hello World</h1>';

    try {
        // Convert HTML string to a PDF document
        const pdfDoc = await pdfGenerator.fromHtml(htmlContent);
        // Save the generated PDF document to a file
        await pdfDoc.saveAs('hello_world.pdf');
        console.log('PDF generated successfully from HTML string.');
    } catch (error) {
        console.error('Error generating PDF from HTML:', error);
    }
}

async function generatePdfFromWebPage() {
    // URL of the web page to be converted into a PDF
    const url = 'https://en.wikipedia.org/wiki/PDF';

    try {
        // Convert web page URL to a PDF document
        const pdfDoc = await pdfGenerator.fromUrl(url);
        // Save the generated PDF document to a file
        await pdfDoc.saveAs('wikipedia_pdf.pdf');
        console.log('PDF generated successfully from webpage URL.');
    } catch (error) {
        console.error('Error generating PDF from webpage:', error);
    }
}

async function generatePdfFromImages() {
    // Directory containing images
    const imageDir = './images';
    const imageFiles = fs.readdirSync(imageDir);

    try {
        // Filter only JPEG files from the directory
        const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg'));

        // Convert filtered images to a single PDF document
        const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`));
        // Save the generated PDF document to a file
        await pdfDoc.saveAs('images.pdf');
        console.log('PDF generated successfully from images.');
    } catch (error) {
        console.error('Error generating PDF from images:', error);
    }
}

// Execute the functions to generate PDFs
generatePdfFromHtml();
generatePdfFromWebPage();
generatePdfFromImages();
JAVASCRIPT
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.

< PREVIOUS
How to Read PDF Files in Node.js
NEXT >
How to Convert HTML to PDF in Node js without Puppeteer

Install with npm

Version: 2025.6

> npm i @ironsoftware/ironpdf

Report an Issue

Ready to get started? Version: 2025.6 just released

View Licenses >