Merge PDF stored in Remote server using Python

Prerequisites: Working with PDF files in Python

There are many libraries for manipulating PDF files in Python but all are using when that all PDF files already downloaded in your local machine. But what if your target PDF files are in Remote server, you have only URLs of files and no required download in your machine or compute server. Here We will discuss this problem and solution for the same.

Here we use the library of Python PyPDF2 for merging PDFs.

pip install PyPDF2

We will merge the same pdf file twice. Link to the pdf file used is here

Below is the implementation.





from io import BytesIO, SEEK_SET, SEEK_END
import PyPDF2 
import requests
# Create a class which convert PDF in
# BytesIO form
class ResponseStream(object):
    def __init__(self, request_iterator):
        self._bytes = BytesIO()
        self._iterator = request_iterator
    def _load_all(self):, SEEK_END)
        for chunk in self._iterator:
    def _load_until(self, goal_position):
        current_position =, SEEK_END)
        while current_position < goal_position:
                current_position = self._bytes.write(next(self._iterator))
            except StopIteration:
    def tell(self):
        return self._bytes.tell()
    def read(self, size = None):
        left_off_at = self._bytes.tell()
        if size is None:
            goal_position = left_off_at + size
    def seek(self, position, whence = SEEK_SET):
        if whence == SEEK_END:
  , whence)
# Merge PDFs using URL List
url_list = ["lis of URL"]
target_pdf_path = './Merged.pdf'
pdf_writer = PyPDF2.PdfFileWriter()
for url in url_list:
    response = requests.get(url)
    reader = PyPDF2.PdfFileReader(ResponseStream(response.iter_content(64)))
    for page in range(reader.getNumPages()):
# write to output file
with open(target_pdf_path, 'wb') as g:



The merged pdf file created is here

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.