How to Generate a PDF File in Node.js

Updated July 27, 2023

In the constantly evolving panorama of web development, the dynamic creation and proficient management of PDF files have evolved into a pivotal aspect of the user front. Node.js, renowned for its robust server-side capabilities, emerges as the optimal environment for handling such tasks with remarkable efficiency. At the forefront of this domain stands IronPDF – an influential Node JS PDF generator that streamlines and simplifies the intricate process of PDF generation. This article endeavors to delve deep into the nuanced intricacies of IronPDF, guiding readers through every facet from the initial installation process to its practical implementation in real-world scenarios. A simple example of a PDF generator can be found here.

1. How to use Node JS PDF Generator?

  1. Install the IronPDF for Node.js PDF Generator Library.
  2. Import the required Dependencies.
  3. Render HTML as PDF using PdfDocument.fromHtml function.
  4. Generate PDF from URL using PdfDocument.fromUrl method.
  5. Convert Images into PDF files using imageToPdf method.
  6. Save the Created PDF files using SaveAs method.

2. Exploring IronPDF For Node.js

IronPDF stands as a formidable PDF library meticulously crafted for Node.js, endowing developers with the seamless capability to effortlessly generate PDF files. Boasting an extensive array of features, IronPDF proves its versatility, catering to a spectrum of use cases that span from document generation and report creation to content rendering. Among its standout attributes lies the remarkable prowess to seamlessly convert an HTML template and images into PDF files, showcasing the library's adaptability and efficiency in handling diverse content sources.

2.1. Installing IronPDF

Before diving into the world of PDF generation, you need to install IronPDF. The process is straightforward, thanks to npm – Node.js's package manager. Open your terminal and run the following command:

npm i @ironsoftware/ironpdf

This command fetches and installs the IronPDF package, making it available for use in your Node.js project.

To install the IronPDF engine that is required for using the IronPDF library, run the following command on the console.

npm install @ironsoftware/ironpdf-engine-windows-x64

3. Adding Required Packages

Once IronPDF is installed, you need to include the necessary packages in your project using the import statement. This ensures that you have access to the functionalities provided by IronPDF. Consider the following example:

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

This import statement brings in the PdfDocument class, a fundamental building block for generating PDF files with IronPDF.

4. Generating PDF Files

Now that IronPDF is set up in your project, let's explore the basic process of generating a PDF file using the PDF creator package. There are many ways to create PDF files using IronPDF, but in this article we will discuss a few of them.

  1. HTML String to PDF
  2. URL to PDF
  3. Images to PDF

4.1. Convert HTML to PDF Document using IronPDF

One of IronPDF's standout features is its ability to convert HTML into PDF files effortlessly. This is particularly useful when you have dynamic content generated on the server and want to present it in a PDF format. Here's a simple example:

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

(async () => {
    // Create a PDF from an HTML string
    const pdf = await PdfDocument.fromHtml("<h1>Hello Developers THis is an Example PDF created with IronPDF</h1>");

    // Export the PDF to a file
    await pdf.saveAs("output.pdf");

This code snippet utilizes IronPDF, a Node.js library, to dynamically generate a PDF document from a specified HTML string. It begins by importing the PdfDocument class from the IronPDF package. The subsequent asynchronous function employs the fromHtml method to create a PDF document, using a simple heading element in this instance. The saveAs method is then utilized to export the generated PDF to a file named "output.pdf."

The asynchronous nature of the code is managed through the use of the async/await syntax, ensuring that the PDF data generation and saving processes occur in a non-blocking manner. Overall, this succinct example demonstrates how IronPDF streamlines the creation and manipulation of PDFs in a Node.js environment.


Output PDF generated from an HTML string using IronPDF library.

4.2. Create PDF Files from a URL using IronPDF

Creating PDF files from a URL using IronPDF in Node.js is a powerful feature that allows you to convert web content into PDF documents programmatically. Here's an example code snippet that demonstrates how to achieve this:

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

(async () => {
  // URL of the web page to convert to PDF
  const url = "";

  // Create a PDF doc from the specified URL
  const pdf = await PdfDocument.fromUrl(url);

  // Save the PDF to a file
  await pdf.saveAs("output_from_url.pdf");

In this code:

  1. The PdfDocument class is imported from the IronPDF package.
  2. Inside an asynchronous function (wrapped in an immediately-invoked function expression), a variable url is defined with the URL of the web page you want to convert to a PDF.
  3. The fromUrl method is then used to create a PDF document from the specified URL. IronPDF fetches the content from the URL and converts it into a PDF document.
  4. Finally, the saveAs method is employed to save the generated PDF to a file named "output_from_url.pdf."

This example showcases the simplicity of using IronPDF to convert web content from a URL into a PDF file, making it a valuable tool for scenarios where dynamic web content needs to be archived or shared in a PDF format.

Output PDF generated from a URL using IronPDF library

4.3. Generating PDF File from Images using IronPDF

In addition to HTML, IronPDF provides functionality to generate PDF files from images. This is particularly useful when dealing with scenarios where images need to be compiled into a single PDF document. Let's explore a simple example:

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);

    // 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 = => `${directoryPath}/${file}`);

    // Converts the images to a PDF and save it.
    const pdf = PdfGenerator.imageToPdf(filePaths).then(
        (returnedPdf)=> {

    // Also see PdfDocument.rasterizeToImageFiles() method to flatten a PDF to images or thumbnails

This code utilizes IronPDF, a Node.js library, to convert a collection of JPEG images in a specified directory into a single PDF doc. Initially, the code specifies the directory path containing the images. It then reads the contents of the directory using the fs.readdir function. Subsequently, it filters the file names to include only those with ".jpg" or ".jpeg" extensions.

The full file paths for the filtered images are constructed, and the PdfGenerator.imageToPdf() method is employed to convert these images into a PDF document. The resulting PDF is then saved as "composite.pdf." This script demonstrates the streamlined process offered by IronPDF for converting a batch of images into a consolidated PDF file, providing a practical solution for scenarios where image content needs to be compiled and presented in a single document.

Output PDF generated from Images using IronPDF library

5. Conclusion

Node.js, coupled with IronPDF, opens up a world of possibilities for dynamic PDF creation. From simple text-based documents to complex reports with dynamic content, IronPDF simplifies the process and enhances the capabilities of your Node.js applications. Whether you're02 converting HTML to PDF, populating templates, or compiling images into a PDF file, IronPDF provides a versatile and efficient solution for your PDF generation needs.

As you continue to explore and implement these features, you'll find that IronPDF is a valuable tool in your developer toolkit, streamlining the process of creating dynamic and visually appealing PDF documents.

IronPDF for Node.js offers a free trial for their users. To get started with IronPDF visit here. The code example and complete tutorial of HTML to PDF conversion can be found at the following tutorial page.

Convert HTML to PDF in Node.js Without Puppeteer
How to Sign A PDF File in Node.JS

Ready to get started? Version: 2024.3 just released

Free npm Install View Licenses >