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
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