How to merge pdf files using python without storing them into the local directory

I have some pdf files which are uploaded on a remote server. I have URL for each file and we can download these PDF files by visiting those URLs.

My question is,

I want to merge all pdf files into a single file (but, without storing these files into local directory). How can I do that (in python module 'PyPDF2')?

Please move to pypdf. It's essentially the same as PyPDF2, but the development will continue there (I'm the maintainer of both projects).

Your question is answered in the docs:

Instead of writing to a file, you write to io.ByteIO stream:

from io import ByteIO

# e.g. writer = PdfWriter()
# ... do what you want to do with the PDFs

with BytesIO() as bytes_stream:
    writer.write(bytes_stream)
    bytes_stream.seek(0)
    data = bytes_stream.read()  # that is now the "bytes" represention

To merge PDF files without saving them locally, you can use the requests library to download the contents of each file, then pass the contents to the PdfFileReader class in the PyPDF2 library.

import requests
import PyPDF2
from io import BytesIO

def merge_pdfs_remotely(urls, output_filename):
    # Create a list of file-like objects from the URLs
    file_streams = [BytesIO(requests.get(url).content) for url in urls]
    
    # Create the PDF merger object
    merger = PyPDF2.PdfFileMerger()
    
    # Add each PDF file to the merger
    for stream in file_streams:
        merger.append(PyPDF2.PdfFileReader(stream))
Back to Top