How to Merge PDF Files Using Python

In this tutorial, we explore how to merge multiple PDF files into a single document using Python and IronPDF. After ensuring IronPDF is installed, we begin by setting up the library with a license key to access its full functionality. We prepare sample HTML content representing different PDF documents and instantiate the Chrome PDF renderer to convert the HTML into PDFs. The merging process involves using PDFDocument.merge to combine documents. Initially, two PDFs are merged and saved as 'merged_to_pdf.pdf'. To merge three PDFs, we compile a list of documents and again utilize the merge method, saving the result as 'merged.pdf'. Running the script confirms the merged PDFs are saved in the current directory. This tutorial provides a clear walkthrough of the process, demonstrating how all documents can be combined into one file using IronPDF's capabilities. This tutorial concludes with an invitation to try out the software, encouraging viewers to like the video and subscribe for more content.

# Import necessary module from IronPDF
from ironpdf import IronPdf  # Assuming the module is imported like this as a placeholder, replace with the actual module path.

def merge_pdfs():
    # Initialize IronPDF with a license key (ensure to replace 'your-license-key' with an actual valid key)
    IronPdf.set_license('your-license-key')

    # Sample HTML content for demonstration purposes
    html_content1 = '<html><body><h1>Document 1</h1><p>This is the first document.</p></body></html>'
    html_content2 = '<html><body><h1>Document 2</h1><p>This is the second document.</p></body></html>'
    html_content3 = '<html><body><h1>Document 3</h1><p>This is the third document.</p></body></html>'

    # Initialize a ChromePdfRenderer 
    renderer = IronPdf.ChromePdfRenderer()

    # Convert HTML content to PDF documents
    pdf1 = renderer.render_html_as_pdf(html_content1)
    pdf2 = renderer.render_html_as_pdf(html_content2)
    pdf3 = renderer.render_html_as_pdf(html_content3)

    # Merge two PDFs and save the output
    merged_pdf_2 = IronPdf.PdfDocument.merge([pdf1, pdf2])
    merged_pdf_2.save_as('merged_to_pdf.pdf')

    print("Successfully merged two PDF files into 'merged_to_pdf.pdf'.")

    # Merge three PDFs and save the output
    merged_pdf_3 = IronPdf.PdfDocument.merge([pdf1, pdf2, pdf3])
    merged_pdf_3.save_as('merged.pdf')

    print("Successfully merged three PDF files into 'merged.pdf'.")

if __name__ == "__main__":
    merge_pdfs()
# Import necessary module from IronPDF
from ironpdf import IronPdf  # Assuming the module is imported like this as a placeholder, replace with the actual module path.

def merge_pdfs():
    # Initialize IronPDF with a license key (ensure to replace 'your-license-key' with an actual valid key)
    IronPdf.set_license('your-license-key')

    # Sample HTML content for demonstration purposes
    html_content1 = '<html><body><h1>Document 1</h1><p>This is the first document.</p></body></html>'
    html_content2 = '<html><body><h1>Document 2</h1><p>This is the second document.</p></body></html>'
    html_content3 = '<html><body><h1>Document 3</h1><p>This is the third document.</p></body></html>'

    # Initialize a ChromePdfRenderer 
    renderer = IronPdf.ChromePdfRenderer()

    # Convert HTML content to PDF documents
    pdf1 = renderer.render_html_as_pdf(html_content1)
    pdf2 = renderer.render_html_as_pdf(html_content2)
    pdf3 = renderer.render_html_as_pdf(html_content3)

    # Merge two PDFs and save the output
    merged_pdf_2 = IronPdf.PdfDocument.merge([pdf1, pdf2])
    merged_pdf_2.save_as('merged_to_pdf.pdf')

    print("Successfully merged two PDF files into 'merged_to_pdf.pdf'.")

    # Merge three PDFs and save the output
    merged_pdf_3 = IronPdf.PdfDocument.merge([pdf1, pdf2, pdf3])
    merged_pdf_3.save_as('merged.pdf')

    print("Successfully merged three PDF files into 'merged.pdf'.")

if __name__ == "__main__":
    merge_pdfs()
PYTHON

Further Reading: Merge PDF Files into a Single PDF Using Python

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
< PREVIOUS
How to Convert PDF to Image in Python
NEXT >
How to Create PDF Files in Python

Install with pip

Version: 2025.6

> pip install ironpdf

Ready to get started? Version: 2025.6 just released

View Licenses >