Merge PDF in Python

The PDF split and merge operations are usually offered in paid software and most free PDF viewers do not provide these capabilities. Also, in case you need to merge certain PDF documents together, you need to first install, configure and purchase the complete license subscription. You may only have a requirement to merge the PDF files but you will have to pay the complete licensing price. Apart from this, you may stumble upon a scenario where you need to combine the PDF files but the software is not available on a particular platform. Therefore, in order to mitigate all such scenarios, we recommend using Aspose.PDF Cloud API to Merge PDF files online. No software download or installation required and perform all the operations in the cloud.

PDF processing API in Python

Aspose.PDF Cloud SDK for Python is a wrapper around Aspose.PDF Cloud API offering all the powers and capabilities of PDF file processing for Python developers. Similarly, the SDK also provides the feature to merge/combine multiple PDF files into a single output document and this all can be accomplished with few code lines.

Installation

Before we move further, the first step is to install the SDK either from PIP or GitHub repository. In this article, we are going to discuss the steps of installation from PIP. Simply execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

pip install asposepdfcloud

Installation in Pycharm

PyCharm is a popular IDE for Python development. So in order to use the SDK directly in Python IDE, please follow the steps given below.

Click Preferences menu item under PyCharm menu.

Preferences menu item

From left tree, select Python Interpreter under Project section.

Python Interpreter option

Now click the + (plus) sign on the right section and enter asposepdfcloud in the search field over the available packages dialog.

Available packages dialog

Now click the Install Package button. Once the SDK is installed, the success message is displayed.

Merge PDF files in Python

Aspose.PDF Cloud uses cloud-based storage services for document storage. Therefore, in order to ensure data integrity and privacy, only authorized persons are eligible to access the APIs. So the first step is to create an account by visiting Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Given below are the steps to merge the PDF documents.

  • First of all, create an object of ApiClient while passing ClientSecret and ClientID as arguments
  • Secondly, create a PdfApi instance while passing the ApiClient object as an argument
  • Then create an Opts object where we define the target PDF to be appended, the start and end page of the PDF file
  • Finally, call the post_append_document(…) method to perform PDF merge operations

Merge PDF files using the cURL command

The cURL commands are one of the convenient ways of accessing REST APIs through the command line terminal of any platform. You even do not need to install the SDK but simply execute the command in the terminal application and fulfill your requirements. So in order to use the cURL commands, the first step is to generate JSON Web Token (JWT) so that the APIs are accessible through the command prompt. The same JWT token is used to access APIs through cURL commands.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4&client_secret=d87269aade6a46cdc295b711e26809af" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Use the following command and target PDF is merged to source/input PDF files using PostAppendDocument API. The resultant output is saved on Cloud storage.

curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithAnnotations.pdf/appendDocument?appendFile=PdfWithTable.pdf&startPage=2&endPage=3" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>"

Conclusion

In this article, we have discussed the steps to perform PDF merge operations using Python REST API. Owing to the opensource type, the complete source code of SDK can be downloaded from the GitHub repository.

For further details about the capabilities of SDK, please visit the following documentation topic Aspose.PDF Cloud Features. Nevertheless, if you encounter any issues while using the API, please feel free to contact us via a free technical support forum.

We also recommend going through following articles explaining