How to Create PDF Files in Node.js

Creating PDF files programmatically in Node.js requires a library that handles HTML rendering accurately, supports modern CSS, and integrates cleanly with Node's async patterns. IronPDF uses a Chromium-based rendering engine to convert HTML content into PDFs that match Chrome's print output, supporting full CSS, inline JavaScript, and responsive layouts out of the box.

This guide covers the complete workflow: installation, generating PDFs from HTML strings, HTML files, and URLs, configuring output options, and applying enterprise features like headers, footers, digital signatures, and encryption.

Quickstart: Create a PDF in Node.js

  1. Install IronPDF via npm: npm install @ironsoftware/ironpdf
  2. Import PdfDocument from @ironsoftware/ironpdf
  3. Call PdfDocument.fromHtml() with your HTML content
  4. Call .saveAs() to write the PDF file to disk
//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/quickstart.js
import { PdfDocument } from "@ironsoftware/ironpdf";

const pdf = await PdfDocument.fromHtml("<h1>Hello, PDF!</h1><p>Generated with IronPDF.</p>");
await pdf.saveAs("output.pdf");

Configure IronPdfEngine for your platform before running in production. See the IronPdfEngine setup guide for Windows, Linux, macOS, and Docker instructions.

How Do I Install IronPDF for Node.js?

Install the @ironsoftware/ironpdf package using npm. The package works with Node.js 12.0 or higher and supports both ESM and CommonJS module formats.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/install.sh
npm install @ironsoftware/ironpdf
//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/install.sh
npm install @ironsoftware/ironpdf
SHELL

IronPDF for Node.js depends on the IronPDF Engine binary, which handles the underlying Chromium-based rendering. The engine downloads automatically on first run, but you can pre-install the platform-specific package to avoid runtime downloads. This is useful in restricted network environments or Docker-based CI pipelines:

IronPDF Engine platform packages for pre-installation in offline or restricted environments
PlatformPackage
Windows x64@ironsoftware/ironpdf-engine-windows-x64
Linux x64@ironsoftware/ironpdf-engine-linux-x64
macOS x64@ironsoftware/ironpdf-engine-macos-x64
macOS ARM@ironsoftware/ironpdf-engine-macos-arm64

Please noteA valid license key removes the watermark from generated PDFs. Apply it before your first PdfDocument call by setting IronPdfGlobalConfig.setConfig({ licenseKey: "YOUR-KEY" }). Follow the full license key setup instructions.

What Are the System Requirements for IronPDF?

IronPDF for Node.js runs on Node.js 12.0 or higher on Windows, Linux, macOS, and Docker environments. The library supports both x64 and ARM64 architectures, making it suitable for local development and containerized deployments. For cloud environments, IronPDF also supports connecting to a remote IronPDF Engine instead of running the engine locally.


How Do I Create a PDF from HTML in Node.js?

Use PdfDocument.fromHtml() to generate a PDF from an HTML string. The method accepts any valid HTML, including inline <style> blocks, external fonts loaded via CDN, and layout systems like CSS Grid or Flexbox. The Chromium renderer resolves all resources before generating the PDF.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/html-string-to-pdf.js
import { PdfDocument } from "@ironsoftware/ironpdf";

const htmlContent = `
  <!DOCTYPE html>
  <html>
  <head>
    <style>
      body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
      .header { background: #2b4c8c; color: white; padding: 24px 32px; }
      .header h1 { margin: 0; font-size: 22px; }
      .body { padding: 32px; }
      table { width: 100%; border-collapse: collapse; margin-top: 16px; }
      th { background: #f0f4fb; text-align: left; padding: 8px 12px; }
      td { padding: 8px 12px; border-bottom: 1px solid #e0e0e0; }
    </style>
  </head>
  <body>
    <div class="header"><h1>Invoice #INV-2025-0042</h1></div>
    <div class="body">
      <p>Date: ${new Date().toLocaleDateString()}</p>
      <table>
        <tr><th>Item</th><th>Qty</th><th>Unit Price</th><th>Total</th></tr>
        <tr><td>IronPDF Enterprise License</td><td>1</td><td>$499.00</td><td>$499.00</td></tr>
        <tr><td>Priority Support (1 year)</td><td>1</td><td>$199.00</td><td>$199.00</td></tr>
      </table>
      <p style="text-align:right; margin-top:16px;"><strong>Total: $698.00</strong></p>
    </div>
  </body>
  </html>`;

// Generate the PDF from the HTML string
const pdf = await PdfDocument.fromHtml(htmlContent);
await pdf.saveAs("invoice.pdf");

PdfDocument.fromHtml() returns a PdfDocument instance. Call saveAs() with your desired output path to write the file. The method is asynchronous, so use await or chain a .then() handler. The Chromium engine handles font loading, external stylesheets, and asset resolution before capturing the output. For more patterns, see the HTML string to PDF example.

How Do I Create a PDF from an HTML File?

Load an existing .html file from disk using PdfDocument.fromFile(). This approach works well for templated documents where the HTML is maintained separately from the application logic.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/html-file-to-pdf.js
import { PdfDocument } from "@ironsoftware/ironpdf";

// Load an HTML file and render it as a PDF
const pdf = await PdfDocument.fromFile("./templates/report-template.html");
await pdf.saveAs("./output/report.pdf");

The renderer resolves relative asset paths from the HTML file's directory, so local CSS and image references work without additional configuration. See the HTML file to PDF example for additional options, including how to override the base URL for asset resolution.

How Do I Generate a PDF from a URL?

Use PdfDocument.fromUrl() to render a live web page as a PDF. The renderer loads the page as a full browser session, executing JavaScript, applying CSS, and waiting for dynamic content before capturing the output.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/url-to-pdf.js
import { PdfDocument } from "@ironsoftware/ironpdf";

// Render a live webpage to PDF
const pdf = await PdfDocument.fromUrl("https://ironpdf.com/nodejs/");
await pdf.saveAs("ironpdf-homepage.pdf");

For pages that load content asynchronously via JavaScript, pair this with a renderDelay option (in milliseconds) to allow additional time before capture. The URL to PDF example demonstrates delay configuration and authentication headers.


How Do I Configure PDF Output Settings?

Pass a configuration object as the second argument to PdfDocument.fromHtml(), PdfDocument.fromFile(), or PdfDocument.fromUrl(). The configuration object accepts paper size, margins, orientation, and rendering options.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/pdf-settings.js
import { PdfDocument, PdfPaperSize } from "@ironsoftware/ironpdf";

const config = {
    paperSize: PdfPaperSize.A4,
    marginTop: 25,
    marginBottom: 25,
    marginLeft: 20,
    marginRight: 20,
    landscape: false,
    printBackground: true,
};

const pdf = await PdfDocument.fromHtml("<h1>Configured PDF</h1>", config);
await pdf.saveAs("configured-output.pdf");

Key configuration options include:

  • paperSize: accepts PdfPaperSize enum values (A4, Letter, Legal, and custom sizes)
  • landscape: set true for landscape page orientation
  • printBackground: include CSS background colors and images
  • marginTop/Bottom/Left/Right: page margins in millimeters
  • renderDelay: milliseconds to wait before capture for JavaScript-heavy pages

How Do I Add Headers and Footers to a PDF?

Add headers and footers by providing htmlHeader and htmlFooter properties in the configuration object. Both accept full HTML strings, enabling styled content with page numbers, dates, and dynamic text.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/headers-footers.js
import { PdfDocument } from "@ironsoftware/ironpdf";

const config = {
    htmlHeader: {
        htmlFragment: "<div style='text-align:center; font-size:12px; color:#666;'>Quarterly Report - Confidential</div>",
        dividerLine: true,
    },
    htmlFooter: {
        htmlFragment: "<div style='text-align:right; font-size:10px;'>Page {page} of {total-pages}</div>",
        dividerLine: true,
    },
};

const pdf = await PdfDocument.fromHtml("<h1>Q3 Financial Summary</h1><p>See attached tables.</p>", config);
await pdf.saveAs("report-with-headers.pdf");

Use {page} and {total-pages} tokens in the footer HTML; IronPDF substitutes the correct values at render time. For more header and footer patterns, including HTML headers and footers and advanced header techniques, see the linked examples.


How Do I Add Security and Metadata to a PDF?

Apply password protection and encryption to a generated PDF using the PdfDocument security methods. The library supports both owner and user passwords, with granular permission controls for printing, copying, and editing.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/security.js
import { PdfDocument } from "@ironsoftware/ironpdf";

const pdf = await PdfDocument.fromHtml("<h1>Confidential Document</h1>");

// Apply password protection with granular permissions
await pdf.securePdf({
    userPassword: "view-password",
    ownerPassword: "admin-password",
    allowUserAnnotations: false,
    allowUserPrinting: true,
    allowUserCopyPasteContent: false,
});

await pdf.saveAs("secured-document.pdf");

Encryption and decryption use AES 256-bit encryption by default when passwords are applied. For document authentication workflows, IronPDF also supports digital signatures using X.509 certificates.

TipsFor documents that require long-term archival compliance, use PdfDocument.fromHtml() followed by the PDF/A conversion method. IronPDF supports PDF/A compliance and PDF/UA accessibility standards.


How Do I Merge and Manipulate Existing PDFs?

PdfDocument provides methods for merging, splitting, and modifying existing PDF files. Load an existing PDF with PdfDocument.fromFile() and use the manipulation methods to add or remove pages, stamp content, or replace text.

//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/merge-pdfs.js
import { PdfDocument } from "@ironsoftware/ironpdf";

// Merge two PDF files into one
const merged = await PdfDocument.mergePdf([
    await PdfDocument.fromFile("./report-part1.pdf"),
    await PdfDocument.fromFile("./report-part2.pdf"),
]);

await merged.saveAs("./complete-report.pdf");

PdfDocument.mergePdf() accepts an array of PdfDocument instances and returns a single merged document. Page order follows the array order, so the output preserves the sequence of the input files.

Other common manipulation operations:

For high-volume scenarios, multi-threaded PDF generation lets you process multiple documents in parallel. PDF compression reduces file sizes for storage and transmission.


How Do I Generate PDFs for Complex Content Like Charts and Angular Pages?

IronPDF renders pages using a full Chromium browser session, which means JavaScript libraries that produce charts, graphs, or data visualizations render correctly, including JavaScript chart libraries. Single-page applications built with frameworks like Angular also convert cleanly using the Angular to PDF example.

For Unicode-heavy documents or multi-language output, IronPDF handles Unicode and international character sets without additional configuration. The Google Fonts integration example shows how to load web fonts in the rendered HTML for consistent typography in the output PDF.

Please noteIronPDF's Chromium renderer executes JavaScript before capturing the PDF. If your chart library or SPA requires additional time to render data after the DOM loads, set renderDelay in milliseconds to give the page time to fully render.


What Are the Next Steps for Creating PDFs in Node.js?

This guide covered the core IronPDF workflow: installing the library, generating PDFs from HTML strings, files, and URLs, configuring output settings, adding headers and footers, applying security, and manipulating existing PDFs.

To continue, try the complete HTML to PDF tutorial for a deeper walkthrough of rendering options, or browse the API Reference for the full list of PdfDocument methods and configuration properties.

Start your free trial to generate PDFs without watermarks, or view licensing options for production deployments.

Frequently Asked Questions

What is the easiest way to create PDF files in Node.js?

Install IronPDF with npm install @ironsoftware/ironpdf, then call PdfDocument.fromHtml() with your HTML string and save the result using saveAs(). The library handles all rendering automatically.

How do I install IronPDF for Node.js?

Run npm install @ironsoftware/ironpdf in your project directory. The IronPDF Engine binary downloads automatically on first use. For offline or CI environments, install the platform-specific engine package such as @ironsoftware/ironpdf-engine-linux-x64 separately.

Can I convert HTML to PDF while preserving CSS styles and formatting?

Yes. IronPDF uses a Chromium-based rendering engine that preserves all CSS styles, external fonts, and JavaScript execution when converting HTML to PDF. The output matches Chrome's print preview behavior.

What PDF features are available beyond basic HTML conversion?

IronPDF supports digital signatures, AES 256-bit encryption, password protection with granular permissions, custom headers and footers with page number tokens, PDF merging, page removal, HTML stamping, text extraction, PDF/A compliance, and multi-threaded generation.

How do I create a PDF from an HTML file on disk?

Use PdfDocument.fromFile('./path/to/template.html'). The renderer resolves relative asset paths from the HTML file's directory, so local CSS and image references work without additional configuration.

Can I extract text from existing PDF files using IronPDF?

Yes. Load an existing PDF with PdfDocument.fromFile() and use the text extraction methods to read content from any page. IronPDF supports both creating new PDFs and processing existing documents.

Does IronPDF support JavaScript execution when rendering HTML?

Yes. The Chromium renderer executes JavaScript before capturing the PDF. For pages that load content asynchronously, set the renderDelay option in milliseconds to give the page additional time before capture.

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 ...

Read More
Ready to Get Started?
Version: 2026.4 just released
Still Scrolling Icon

Still Scrolling?

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