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

312 Views Asked by At

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')?

1

There are 1 best solutions below

0
On

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