Passport Loader

How to merge multiple PDF documents


In this tutorial, we will look at how to merge multiple PDF documents into one.
We will be using PassportPDF API and Python

You should have your machine already set up using instructions from the getting started guide.

Merging PDF files using PassportPDF

To merge multiple PDF documents into one, we will use the following endpoints from PassportPDF API:

  • DocumentLoadFromURI to load a document from a URI.
  • Merge to merge the different PDF files into one.
  • SaveDocument to download the final merged document.

These endpoints are shown in the following Python code example:

PassportPDF tutorial : Merging several PDF documents into a single document.
import requests
import base64
if __name__=="__main__":
    endpoint = ""
    headers = {
        "X-PassportPDF-API-Key" : "YOUR-PASSPORT-CODE",
    URI_1 = ""
    uri1 = {
        "URI" : URI_1
    URI_2 = ""
    uri2 = {
        "URI": URI_2
    uris_list = [uri1, uri2]
    files_ids = []
    for uri in uris_list:
        response =, json=uri, headers=headers)
        if(response.status_code == 200):
            json_response = response.json()
            file_id = json_response["FileId"]
    # Merging documents
    merge_endpoint = ""
    merge_data = {
        "FilesId" : files_ids
    merge_response =, json=merge_data, headers=headers)
    if(merge_response.status_code == 200):
        json_response = merge_response.json()
        merged_file_id = json_response["FileId"]
        print("Saving merged document..")
        # Download merged document
        save_document_endpoint = ""
        save_document_response =, json={"FileId" : merged_file_id}, headers=headers)
        if(save_document_response.status_code == 200):
            json_response = save_document_response.json()
            with open(f"./data/output/merged_document.pdf", "wb") as file:
                decoded_data = base64.b64decode(json_response["Data"].encode())
            print("Could not download merged document!")
        print("Could not merge documents!")

This example shows how to merge two documents only, but you can generalize it to merge more files.
You can do this by specifying the URIs of the documents and then adding them all to the list of URIs, which in our code example is defined by the variable uris_list. Then, you should pass this list to the api/pdf/Merge endpoint.

Final remarks

The documents will be merged in the same order as the order of file IDs that are passed to the Merge endpoint.

For more information about the endpoints used in this tutorial, please visit the PassportPDF API reference.