How to Merge PDF Files Using Python | IronPDF

Merge PDF Files into a Single PDF Using Python

IronPDF for Python provides a direct solution to merge multiple PDF documents into a single file using the PdfDocument.Merge() method, supporting both two-file merging and batch operations for combining numerous PDFs efficiently.

PDF (Portable Document Format) is the standard for sharing documents that must look identical across platforms and applications. Whether you are consolidating reports, combining scanned documents, or assembling multi-part forms, merging PDF content from various sources is a recurring requirement in business and data-processing workflows.

IronPDF handles this operation in Python with a single method call. This guide covers installation, basic two-file merging, and batch operations for combining multiple documents at once.

Quickstart: Merge PDF Files in Python

#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/quickstart.py
from ironpdf import *

renderer = ChromePdfRenderer()
pdf_a = renderer.RenderHtmlAsPdf("<p>Document A</p>")
pdf_b = renderer.RenderHtmlAsPdf("<p>Document B</p>")

merged = PdfDocument.Merge([pdf_a, pdf_b])
merged.SaveAs("merged.pdf")
#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/quickstart.py
from ironpdf import *

renderer = ChromePdfRenderer()
pdf_a = renderer.RenderHtmlAsPdf("<p>Document A</p>")
pdf_b = renderer.RenderHtmlAsPdf("<p>Document B</p>")

merged = PdfDocument.Merge([pdf_a, pdf_b])
merged.SaveAs("merged.pdf")
PYTHON

How Do I Install IronPDF for Python?

IronPDF is available as a Python package via pip. It requires Python 3.x and works on Windows and Linux without any additional system dependencies for standard merge operations. The package ships with a Chrome-based rendering engine that handles HTML-to-PDF conversion internally.

Install the IronPdf library using pip with the following command:

#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/install.sh
pip install ironpdf
#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/install.sh
pip install ironpdf
SHELL

For detailed installation instructions and help with common issues such as "Module Not Defined" errors or permission problems, refer to the official documentation.

What Import Statements Are Required?

In your Python script, include the following import statements to access IronPDF's PDF generation and merging features:

#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/imports.py
from ironpdf import *

# Set your license key for production use
License.LicenseKey = "YOUR-LICENSE-KEY-HERE"
#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/imports.py
from ironpdf import *

# Set your license key for production use
License.LicenseKey = "YOUR-LICENSE-KEY-HERE"
PYTHON

For production applications, configure your license key to unlock the full IronPDF feature set. A free trial is available - start your free trial to begin without a purchase.

How Do I Merge Two PDF Files in Python?

Merging PDF files in Python involves two steps: creating the source PDF documents, then combining them with PdfDocument.Merge(). The method accepts a list of PdfDocument objects and produces a new combined document in the order the items appear in the list.

Below is a complete working example:

#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/merge-two-pdfs.py
from ironpdf import *

# HTML content for the first PDF
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>"""

# HTML content for the second PDF
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>"""

# Initialize renderer
renderer = ChromePdfRenderer()

# Convert each HTML string to a PDF document
pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)

# Merge the two documents into one
merged = PdfDocument.Merge([pdfdoc_a, pdfdoc_b])

# Save the merged result to disk
merged.SaveAs("Merged.pdf")
#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/merge-two-pdfs.py
from ironpdf import *

# HTML content for the first PDF
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>"""

# HTML content for the second PDF
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>"""

# Initialize renderer
renderer = ChromePdfRenderer()

# Convert each HTML string to a PDF document
pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)

# Merge the two documents into one
merged = PdfDocument.Merge([pdfdoc_a, pdfdoc_b])

# Save the merged result to disk
merged.SaveAs("Merged.pdf")
PYTHON

Why Use RenderHtmlAsPdf for PDF Generation?

The example above uses two HTML strings, each representing a two-page document. RenderHtmlAsPdf converts each string into a PdfDocument object using IronPDF's Chrome-based rendering engine, which produces accurate results from HTML, CSS, and JavaScript content. This approach is well-suited for generating reports or data-driven documents from web templates. For more complex rendering scenarios, see the HTML to PDF tutorial.

TipsYou can also load existing PDF files from disk using PdfDocument.FromFile("path/to/file.pdf") and pass them directly to the Merge method. This is useful when combining pre-existing reports or externally generated documents.

How Does the Merge Method Work?

PdfDocument.Merge takes a Python list of PdfDocument objects as its single argument. It combines the documents in list order, placing all pages from the first document before all pages from the second, and so on. The result is a new PdfDocument that can be further modified or saved. Page count, bookmarks, and embedded content from each source document are all preserved in the output.

The method accepts any mix of rendered and file-loaded documents in the same list. This means you can combine freshly rendered HTML output with existing PDFs from disk in a single call, which is useful when one part of a report is generated dynamically and another part is a static template.

How Do I Save the Merged PDF?

Pass the desired output path to SaveAs to write the merged document to disk:

#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/save-merged.py
# Save the merged document
merged.SaveAs("Merged.pdf")

# Optionally compress images before saving to reduce file size
merged.CompressImages(90)
merged.SaveAs("Merged_Compressed.pdf")
#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/save-merged.py
# Save the merged document
merged.SaveAs("Merged.pdf")

# Optionally compress images before saving to reduce file size
merged.CompressImages(90)
merged.SaveAs("Merged_Compressed.pdf")
PYTHON

After saving, you can apply additional post-processing such as PDF compression to reduce the file size of large merged documents.

IronPDF output showing a 4-page merged PDF with thumbnail panel confirming successful two-document merge

Merged PDF output showing pages from both source documents

How Do I Merge More Than Two PDF Files in Python?

Batch merging in IronPDF follows the same pattern as merging two documents. The only difference is that more PdfDocument objects are added to the list before the call to PdfDocument.Merge. The method scales to handle dozens or hundreds of documents in a single operation.

This is the same API regardless of whether you are merging 2 or 200 documents. For high-volume scenarios such as nightly report aggregation or document assembly pipelines, IronPDF also supports parallel PDF generation to speed up the rendering phase before merging.

The process involves two steps:

  • Create a Python list containing the PdfDocument objects to merge
  • Pass the list as the argument to PdfDocument.Merge

How Do I Combine Multiple PDFs with a List?

The code below merges four HTML-rendered documents in a single call:

#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/merge-multiple-pdfs.py
from ironpdf import *

# HTML content for each document
html_a = """<p> [PDF_A] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_A] 2nd Page</p>"""

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

html_c = """<p> [PDF_C] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_C] 2nd Page</p>"""

html_d = """<p> [PDF_D] Content Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_D] Summary Page</p>"""

renderer = ChromePdfRenderer()

# Render all four documents
pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)
pdfdoc_c = renderer.RenderHtmlAsPdf(html_c)
pdfdoc_d = renderer.RenderHtmlAsPdf(html_d)

# Collect into a list and merge
pdfs = [pdfdoc_a, pdfdoc_b, pdfdoc_c, pdfdoc_d]
pdf = PdfDocument.Merge(pdfs)

# Save the combined document
pdf.SaveAs("merged_multiple.pdf")
#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/merge-multiple-pdfs.py
from ironpdf import *

# HTML content for each document
html_a = """<p> [PDF_A] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_A] 2nd Page</p>"""

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

html_c = """<p> [PDF_C] 1st Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_C] 2nd Page</p>"""

html_d = """<p> [PDF_D] Content Page </p>
            <div style='page-break-after: always;'></div>
            <p> [PDF_D] Summary Page</p>"""

renderer = ChromePdfRenderer()

# Render all four documents
pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)
pdfdoc_c = renderer.RenderHtmlAsPdf(html_c)
pdfdoc_d = renderer.RenderHtmlAsPdf(html_d)

# Collect into a list and merge
pdfs = [pdfdoc_a, pdfdoc_b, pdfdoc_c, pdfdoc_d]
pdf = PdfDocument.Merge(pdfs)

# Save the combined document
pdf.SaveAs("merged_multiple.pdf")
PYTHON

The list passed to Merge determines the page order in the final document. Rearranging elements in the list changes the output order with no additional code. If you need to sort documents before merging - for example, by date or report number - sort the Python list first, then pass it to Merge.

How Do I Merge Existing PDF Files from Disk?

When combining pre-existing PDF files rather than freshly rendered ones, load each file with PdfDocument.FromFile before merging. This is the typical pattern when working with files produced by other systems - scanned documents, third-party report exports, or PDFs generated by different tools. IronPDF reads the file into memory as a PdfDocument object, which can then be merged, modified, or inspected before the final save.

#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/merge-existing-pdfs.py
from ironpdf import *

# Load existing PDF files from disk
existing_pdf1 = PdfDocument.FromFile("report1.pdf")
existing_pdf2 = PdfDocument.FromFile("report2.pdf")
existing_pdf3 = PdfDocument.FromFile("report3.pdf")

# Merge all loaded documents
merged_existing = PdfDocument.Merge([existing_pdf1, existing_pdf2, existing_pdf3])

# Save the combined result
merged_existing.SaveAs("merged_reports.pdf")
#:path=/static-assets/pdf/content-code-examples/how-to/python-merge-pdf/merge-existing-pdfs.py
from ironpdf import *

# Load existing PDF files from disk
existing_pdf1 = PdfDocument.FromFile("report1.pdf")
existing_pdf2 = PdfDocument.FromFile("report2.pdf")
existing_pdf3 = PdfDocument.FromFile("report3.pdf")

# Merge all loaded documents
merged_existing = PdfDocument.Merge([existing_pdf1, existing_pdf2, existing_pdf3])

# Save the combined result
merged_existing.SaveAs("merged_reports.pdf")
PYTHON

Please noteIronPDF preserves fonts, images, annotations, and page layouts from the source files during merging. Documents with embedded form fields or digital signatures can also be merged, though interactive form fields in the output may require re-flattening depending on your use case.

IronPDF output showing a multi-page merged PDF with thumbnail navigation panel displaying pages from four source documents

Multi-document merge result showing pages from all four source PDFs

What Are the Next Steps for Merging PDFs in Python?

This guide covered the two main patterns for PDF merging with IronPDF: rendering HTML content to PDF documents and combining them with PdfDocument.Merge, and loading existing files from disk for batch combination. Both approaches share the same API and scale from simple two-file merges to large document batches.

IronPDF supports additional post-merge operations such as adding headers and footers, applying watermarks for branding, extracting text from the combined document, and filling PDF forms programmatically.

The library supports Python 3.x on Windows and Linux. For more Python PDF operations, explore splitting PDFs, converting PDFs to images, and printing PDFs. For the full list of Python code examples, visit the IronPDF for Python examples page.

Start your free trial to test PDF merging in your environment, or view licensing options for production deployments.

Ready to see what else you can do? Check out the IronPDF Python tutorial page here: IronPDF for Python tutorials

Download the software product.

Frequently Asked Questions

How do I merge multiple PDF files into one using Python?

Install IronPDF with pip install ironpdf, then call PdfDocument.Merge() with a Python list of PdfDocument objects. The method returns a new combined document that you save with SaveAs.

Can I merge existing PDF files from disk instead of rendered ones?

Yes. Use PdfDocument.FromFile('path/to/file.pdf') to load each existing PDF, then pass the loaded objects to PdfDocument.Merge(). You can mix file-loaded and freshly rendered documents in the same list.

Does PdfDocument.Merge preserve document formatting and fonts?

Yes. IronPDF preserves fonts, images, annotations, and page layouts from all source documents during merging. The original appearance of each source document is maintained in the final merged PDF.

How do I control the page order in the merged PDF?

The page order in the output matches the order of PdfDocument objects in the Python list passed to Merge. Rearrange the list elements before calling Merge to control the sequence.

Can I compress the merged PDF to reduce file size?

Yes. After merging, call CompressImages(quality) on the merged PdfDocument before calling SaveAs. A quality value of 90 provides a good balance between compression and visual fidelity.

Is IronPDF for Python cross-platform?

Yes. IronPDF for Python supports Python 3.x on Windows and Linux. The same PdfDocument.Merge() API works on both platforms without any configuration changes.

Can I add metadata to the merged PDF document?

Yes. After merging, set metadata properties such as pdf.MetaData.Author and pdf.MetaData.Title on the merged PdfDocument before saving.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

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

Still Scrolling?

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