1

Is it possible that merge PDFs without overwriting?

for example in this code that took from Merge PDF files :

from PyPDF2 import PdfFileMerger

pdfs = ['file1.pdf', 'file2.pdf', 'file3.pdf', 'file4.pdf']

merger = PdfFileMerger()

for pdf in pdfs:
    merger.append(pdf)

merger.write("result.pdf")  # writing line
merger.close()

in the "writing line", we create a completely new PDF file, Therefore, the more files we have, the more time it takes and this is not optimum. I think, if we have the existing PDF file instead of empty PdfFileMerger() and append every new PDF to it and so on, the code can be optimized a lot.

if it's possible with PDFs?

ahmdnz
  • 11
  • 3

1 Answers1

0

If you are open to other options then you can use Aspose.PDF Cloud SDK for Python to append/merge PDF documents without creating a new empty PDF file. Currently, the API process files from cloud storage(Amazon S3, DropBox, Google Drive Storage, Google Cloud Storage, Windows Azure Storage, FTP Storage and Aspose Cloud Storage). However, in the future, we have a plan to support the request body(stream).

import os
import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi
from shutil import copyfile

# Get Client ID and Secret from https://cloud.aspose.com
pdf_api_client = asposepdfcloud.api_client.ApiClient(
    ClientSecret='xxxxxxxxxxxxxxxxxxxxx',
    ClientId='xxxx-xxxx-xxxx-xxxx-xxxxxxxx')

pdf_api = PdfApi(pdf_api_client)

file_name_list = ['4pages.pdf', '02_pages.pdf', 'Sample.pdf']
for file_name in file_name_list:
    pdf_api.upload_file(file_name,file_name)
        
result_name = 'MergingResult.pdf'
        
i = 0
for el in file_name_list:
        file_name_list[i] = el
        i += 1

merge_documents = asposepdfcloud.models.MergeDocuments(file_name_list)

opts = {
              "merge_documents" : merge_documents
        }

response = pdf_api.put_merge_documents(result_name, **opts)

#download PDF file from storage
response_download = pdf_api.download_file(result_name)
copyfile(response_download, result_name)
print(response_download)

P.S: I'm a developer evangelist at Aspose.

Tilal Ahmad
  • 940
  • 5
  • 9