Passport Loader

How to merge multiple PDF documents

Introduction

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 = "https://passportpdfapi.com/api/document/DocumentLoadFromURI"
 
    headers = {
        "X-PassportPDF-API-Key" : "YOUR-PASSPORT-CODE",
    }
 
    URI_1 = "https://passportpdfapi.com/test/invoice_with_barcode.pdf"
 
    uri1 = {
        "URI" : URI_1
    }
 
    URI_2 = "https://passportpdfapi.com/test/multiple_pages.pdf"
 
    uri2 = {
        "URI": URI_2
    }
 
    uris_list = [uri1, uri2]
    files_ids = []
 
    for uri in uris_list:
 
        response = requests.post(endpoint, json=uri, headers=headers)
 
        if(response.status_code == 200):
 
            json_response = response.json()
            file_id = json_response["FileId"]
            files_ids.append(file_id)
 
    # Merging documents
    merge_endpoint = "https://passportpdfapi.com/api/pdf/Merge"
    merge_data = {
        "FilesId" : files_ids
    }
 
    merge_response = requests.post(merge_endpoint, 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 = "https://passportpdfapi.com/api/pdf/SaveDocument"
 
        save_document_response = requests.post(save_document_endpoint, 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())
                file.write(decoded_data)
 
        else:
            print("Could not download merged document!")
 
    else:
        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.