Merging multiple PDFs seems to be a common request, and most people don’t want to spend a small fortune on a tool to do the merging. I first looked at this problem with an early Python project (https://geektechstuff.com/2018/02/17/python-3-merge-multiple-pdfs-into-one-pdf/) and later improved on that project by adding a simple GUI (https://geektechstuff.com/2018/07/04/pdf-merge-with-gui-v2-python/).
Now I have gone a step further and created the PDF merge tool in a Flask powered website. I’ve even created the relevant Docker file so that it can be containerised if needed.
Note: I’m using this in my development environment, if you are going to make it live in a production environment make sure to tidy up some of the features / security etc.
# geektechstuff PDF Merge as a Flask site
from flask import Flask, render_template, request
from werkzeug import secure_filename
now = datetime.datetime.now()
app = Flask(__name__, static_folder=”, static_url_path=”)
@app.route(‘/pdfmerge’, methods = [‘GET’, ‘POST’])
pdfWriter = PyPDF2.PdfFileWriter()
if request.method ==’POST’:
files_to_upload = request.files.getlist(“file”)
for item in files_to_upload:
f = item
pdfFileObj = open(f.filename, ‘rb’)
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
for pageNum inrange(pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
date = str(now.strftime(“%Y-%m-%d”))
rand = str(random.randint(1,10000))
seq = date+rand
file_name_pdf = ‘geektechstuff’+seq+’.pdf’
if __name__ == ‘__main__’:
Running the site:
To run the site copy the files from my Github (link below) and save them into a folder (something like “geektechstuff_pdf_merge_flask”), via the terminal navigate into the folder and either run:
FLASK_APP=pdfmerge.py flask run
Then open a web browser and navigate to the appropriate host (Flask will say what address:port it is running on).
build the included Docker file and run the container in Docker.
The files for this project can be found on my Github: https://github.com/geektechdude/Python_PDF_Merge_Flask_Site
Notes (Or things I need to sort in next version):
- Get it to clear any uploaded PDFs on merge completion
- Get it to clear any created PDFs after a time frame
- Tidy up HTML and make it look better