Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge PDF Files using a 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. Here, you will learn how to merge PDF files using a REST API in Python.
The following topics shall be covered in this article:
1. API Configurations
2. Upload PDF Files to the Cloud
3. Merge PDF Files using REST API in Python
4. Merge Specific Pages of Multiple PDF Files using Python
5. Download the Merged File
client_id = "da0c487d-c1c0-45ae-b7bf-43eaf53c5ad5"
client_secret = "479db2b01dcb93a3d4d20efb16dea971"
configuration = groupdocs_merger_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""
# API initialization
file_api = groupdocs_merger_cloud.FileApi.from_config(configuration)
# create downlod file request
request = groupdocs_merger_cloud.DownloadFileRequest("Output/joined.pdf", my_storage)
# download file
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")
# api initialization
documentApi = groupdocs_merger_cloud.DocumentApi.from_config(configuration)
# source file 1
item1 = groupdocs_merger_cloud.JoinItem()
item1.file_info = groupdocs_merger_cloud.FileInfo("Output/joined.pdf")
# page numbers of specific pages to merge
item1.pages = [1,2]
# surce file 2
item2 = groupdocs_merger_cloud.JoinItem()
item2.file_info = groupdocs_merger_cloud.FileInfo("ten-pages.pdf")
# start page number
item2.start_page_number = 2
# end page number
item2.end_page_number = 4
# page range mode
item2.range_mode = "OddPages"
# define join options
options = groupdocs_merger_cloud.JoinOptions()
options.join_items = [item1, item2]
options.output_path = "Output/joined-pages.pdf"
# create join request
request = groupdocs_merger_cloud.JoinRequest(options)
# merge pages
result = documentApi.join(request)
# api initialization
documentApi = groupdocs_merger_cloud.DocumentApi.from_config(configuration)
# surce file 1
item1 = groupdocs_merger_cloud.JoinItem()
item1.file_info = groupdocs_merger_cloud.FileInfo("ten-pages_1.pdf")
# surce file 2
item2 = groupdocs_merger_cloud.JoinItem()
item2.file_info = groupdocs_merger_cloud.FileInfo("ten-pages_2.pdf")
# define join options
options = groupdocs_merger_cloud.JoinOptions()
options.join_items = [item1, item2]
options.output_path = "Output/joined.pdf"
# create join request
request = groupdocs_merger_cloud.JoinRequest(options)
# merge files
result = documentApi.join(request)
# Create instance of the API
file_api = groupdocs_merger_cloud.FileApi.from_config(configuration)
storage_api = groupdocs_merger_cloud.StorageApi.from_config(configuration)
# upload sample files
for filename in glob.iglob("C:\\Files\\upload\\*.pdf", recursive=True):
destFile = filename.replace("C:\\Files\\upload", "", 1)
# check if file already exist
fileExistsResponse = storage_api.object_exists(groupdocs_merger_cloud.ObjectExistsRequest(destFile))
if not fileExistsResponse.exists:
# create upload file request
request = groupdocs_merger_cloud.UploadFileRequest(destFile, filename)
# upload file
file_api.upload_file(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment