Blog Post

How to Merge PDFs Using Python

Jonathan D. Rhyne
Illustration: How to Merge PDFs Using Python

In this post, you’ll learn how to combine multiple PDF files using a Python PDF merging API. With our API, you receive 100 credits with the free plan. Different operations on a document consume different amounts of credits, so the number of PDF documents you can generate may vary. You’ll just need to create a free account to get access to your API key.

This post will be especially helpful for developers working with document-centric workflows where users upload a large number of documents. With this API, you’ll be able to automate the process of merging documents in your workflow.

A simple example could be an HR application where users upload resumes, cover letters, and references. By integrating a PDF merging API into the workflow, you’ll be able to automatically merge these documents and provide a consolidated document to your end users.

PSPDFKit API

Document merging is just one of our 30+ PDF API tools. You can combine our merging tool with other tools to create complex document processing workflows:

  • Convert MS Office files and images into PDFs before merging

  • OCR several documents before merging

  • Merge, watermark, and flatten PDFs

Once you create your account, you’ll be able to access all our PDF API tools.

Getting Started

To get started, you’ll need to:

  • Create a free account to access your live API key.

  • Install Python on your system. You can download Python here.

Step 1 — Creating a Free Account on PSPDFKit

Go to our website and create your free account. You’ll see the page below.

Free account PSPDFKit API

Once you’ve created your account, you’ll be welcomed by the page below.

Free plan PSPDFKit API

As you can see in the bottom-left corner, you’ll start with 100 credits to process.

Step 2 — Obtaining the API Key

After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API Keys. You’ll see the following:

Merge PDFs Python API Key

Copy the Live API Key, because you’ll need this for the Merge PDF API.

Step 3 — Setting Up Folders and Files

Now, create a folder called merge_pdf and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside merge_pdf and name them input_documents and processed_documents.

Then, in the root folder, merge_pdf, create a file called processor.py. This is the file where you’ll keep your code.

Now your folder structure will look like this:

Merge PDFs PSPDFKit API

Step 4 — Writing the Code

Assuming you already have Python installed, open the processor.py file in your code editor. Paste the code below into your file:

import requests
import json

instructions = {
  'parts': [
    {
      'file': 'first_half'
    },
    {
      'file': 'second_half'
    }
  ]
}

response = requests.request(
  'POST',
  'https://api.pspdfkit.com/build',
  headers = {
    'Authorization': 'Bearer YOUR_TOKEN_HERE'
  },
  files = {
    'first_half': open('input_documents/first_half.pdf', 'rb'),
    'second_half': open('input_documents/second_half.pdf', 'rb')
  },
  data = {
    'instructions': json.dumps(instructions)
  },
  stream = True
)

if response.ok:
  with open('processed_documents/result.pdf', 'wb') as fd:
    for chunk in response.iter_content(chunk_size=8096):
      fd.write(chunk)
else:
  print(response.text)
  exit()

In the first two lines of your code, you’re importing the two dependencies:

  • requests — You’ll use this module to make API calls.

  • json — This will convert a dictionary to JSON.

After that, prepare the JSON that’ll be sent as a payload to the Merge PDF API. You’re also passing the input files from the input document folder in the JSON. This calls the API and stores the response in the processed_documents folder of your Python application.

ℹ️Note: You can find this code on our API website. You’ll also find the code written in other popular languages, like PHP, C#, Java, JavaScript, and Shell.

Now, copy and paste the PDF files you want to process in the input_documents folder. For this tutorial, you’ll use these two PDF files: first_half.pdf and second_half.pdf.

Your updated folder structure will look like this:

Updated folder Merge PDFs Using Python PSPDFKit API

Once done, paste your API key into the code.

Step 5 — Output

To execute the Python code, use the command below:

python3 processor.py

Once the code has been executed, you’ll see a new PDF file under the processed_documents folder.

Final Merge PDFs Using Python PSPDFKit API

If you open the result.pdf file, you’ll see that the number of pages is equal to the sum of both input file pages.

Final Words

That’s it! In this post, you learned how to easily and seamlessly merge files for your Python application into a single PDF using our Merge PDF API.

If you have a more complex use case, you can use our other tools to add watermarks, perform OCR, and edit (split, flatten, delete, duplicate) documents — and you can even combine these tools. To get started with a free trial, sign up here.

Author
Jonathan D. Rhyne Co-Founder and CEO

Jonathan joined Nutrient in 2014. As CEO, Jonathan defines the company’s vision and strategic goals, bolsters the team culture, and steers product direction. When he’s not working, he enjoys being a dad, photography, and soccer.

Explore related topics

Related products

PSPDFKit API

Product page Guides

Share post
Free trial Ready to get started?
Free trial