IRONSOFTWAREHOME
Merge Two or More PDFs
import {PdfDocument} from "@ironsoftware/ironpdf";

(async () => {
    const html_a = `<p> [PDF_A] </p>
    <p> [PDF_A] 1st Page </p>
    <div style='page-break-after: always;'></div>
    <p> [PDF_A] 2nd Page</p>`;

    const html_b = `<p> [PDF_B] </p>
    <p> [PDF_B] 1st Page </p>
    <div style='page-break-after: always;'></div>
    <p> [PDF_B] 2nd Page</p>`;

    // Render HTML content to PDF documents
    const pdfdoc_a = await PdfDocument.fromHtml(html_a);
    const pdfdoc_b = await PdfDocument.fromHtml(html_b);

    // Merge the two PDF documents
    const merged = await PdfDocument.mergePdf([pdfdoc_a, pdfdoc_b]);

    // Save the merged PDF
    await merged.saveAs("Merged.pdf");  
})();
npm i @ironsoftware/ironpdf
Merge Two or More PDFs

Merge Two or More PDFs

The code above enables you to combine multiple PDF documents created from different HTML contents into a single PDF file.

Two HTML contents have been rendered into separate PDF documents. The PdfDocument.mergePdf method is utilized to merge the two PDF documents, pdfdoc_a and pdfdoc_b, into a single PDF document named "merged."

The merging process is not limited to freshly rendered PDFs; it can also be applied to existing PDF documents using this method.

Explanation

  • Import: uses PdfDocument from the @ironsoftware/ironpdf package
  • Opening PDFs: PdfDocument.open(...) loads existing PDF files (rather than rendering HTML) - appropriate when merging pre-existing PDFs.
  • Merging PDFs: PdfDocument.mergePdf([pdfA, pdfB]) combines the two documents into a single instance
  • Saving the result: merged.saveAs("merged.pdf") writes out the merged PDF, equivalent to Python's merger.write(...)

This code is useful for combining PDFs generated from different sources or pre-existing files, facilitating easy document management.

Explore Code Example: Merge PDFs with IronPDF for Node.js
View on GitHub

Ready to Get Started?

Version:2026.8just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
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
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
Node.js Module Download for PDF
Install with npm

Version: 2026.8

  1. Download and install Node.js 12+.
  2. Execute the above command in the terminal.

Licenses from $999