Merge PDF Files using REST API in Python

You can easily combine two or more PDF documents into a single PDF file programmatically on the cloud. It can be useful in sharing or printing multiple documents combined in a single file instead of processing all files one by one. As a Python developer, you can merge two or more PDF files into a single file in your Python applications. In this article, you will learn how to merge PDF files using a REST API in Python.

The following topics shall be covered in this article:

PDF Merger REST API and Python SDK

For merging two or more PDF files, I will be using the Python SDK of GroupDocs.Merger Cloud API. It allows you to combine two or more documents into a single document, or split up one source document into multiple resultant documents. It also enables you to shift, delete, exchange, rotate or change the page orientation either as portrait or landscape for the whole or preferred range of pages. The SDK supports merging and splitting of all popular document formats such as Word, Excel, PowerPoint, Visio, OneNote, PDF, HTML, etc.

You can install GroupDocs.Merger Cloud to your Python application using the following command in the console:

pip install groupdocs_merger_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Merge PDF Files using REST API in Python

You can combine two or more PDF files programmatically on the cloud by following the simple steps mentioned below:

  1. Upload the PDF files to the cloud
  2. Merge multiple PDF files using Python
  3. Download the merged file

Upload the PDF Files

Firstly, upload the PDF files to the cloud using the code example given below:

As a result, the uploaded PDF files will be available in the files section of your dashboard on the cloud.

Merge Multiple PDF Files using Python

You can easily merge multiple PDF files into a single file programmatically by following the steps mentioned below:

  • Create an instance of the DocumentApi
  • Create an instance of the JoinItem
  • Provide the input file path for first JoinItem in the FileInfo
  • Create another instance of the JoinItem
  • Provide the input file path for second JoinItem in the FileInfo
  • Add more JoinItems for merging more than two files
  • Create an instance of the JoinOptions
  • Add a comma separated list of created join items
  • Set the output file path
  • Create an instance of the JoinRequest with JoinOptions
  • Get results by calling the join() method of the DocumentAPI with JoinRequest

The following code snippet shows how to merge multiple PDF files using a REST API in Python.

Download the Merged File

The above code sample will save the merged PDF file on the cloud. You can download it using the following code sample:

Merge Specific Pages of Multiple PDF Files using Python

You can easily combine specific pages from multiple PDF files into a single file programmatically by following the steps mentioned below:

  • Create an instance of the DocumentApi
  • Create an instance of the JoinItem
  • Provide the input file path for first JoinItem in the FileInfo
  • Define a list of page numbers to merge
  • Create another instance of the JoinItem
  • Provide the input file path for second JoinItem in the FileInfo
  • Define start page number and end page number
  • Define the page range mode
  • Create an instance of the JoinOptions
  • Add a comma separated list of created join items
  • Set the output file path
  • Create an instance of the JoinRequest with JoinOptions
  • Get results by calling the join() method of the DocumentAPI with JoinRequest

The following code snippet shows how to merge specific pages from multiple PDF files using a REST API in Python.

Try Online

Please try the following free online PDF merging tool, which is developed using the above API. https://products.groupdocs.app/merger/pdf

Conclusion

In this article, you have learned how to merge multiple PDF files on the cloud. You have also learned how to combine specific pages of multiple PDF documents into one file using Python. Moreover, you have learned how to programmatically upload PDF files to the cloud and then download the merged file from the Cloud. The PDF merger REST API also provides .NET, Java, PHP, Ruby, Android, and Node.js SDKs as its document merger family members for the Cloud API. You can learn more about GroupDocs.Merge Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also