Welcome folks today in this blog post we will be merging multiple pdf
documents in browser using pypdf2
library in browser using flask
. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries
using the pip
command as shown below
pip install flask
pip install pypdf2
And after that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from flask import Flask, render_template, request, send_file from io import BytesIO from PyPDF2 import PdfFileMerger app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def merge_pdfs(): return render_template('index.html') if __name__ == '__main__': app.run() |
As you can see we are importing the flask
library and then we are also importing the pypdf2
library at the top to merge
multiple pdf documents. And then we have started the flask
app at the port number 5000. And then we have initialized the /
route where we are loading the index.html
template file. For this you need to create the templates
folder and inside it you need to create the index.html
file and copy paste the following code
templates/index.html
1 2 3 4 |
<form method="post" enctype="multipart/form-data"> <input type="file" name="pdf_files" multiple> <input type="submit" value="Merge"> </form> |
As you can see we have the simple input field
where we allow the user to select multiple pdf
files and then we have the submit
button to merge the pdf files. Now if you start the flask
app using the below command
python app.py
Now we need to write the post
request to actually merge
multiple pdf files which is selected by the user using the input
field as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@app.route('/', methods=['GET', 'POST']) def merge_pdfs(): if request.method == 'POST': # Get the list of PDF files to merge pdf_files = request.files.getlist("pdf_files") # Create a PDF merger object merger = PdfFileMerger() # Loop through all the PDF files and add them to the merger for pdf in pdf_files: merger.append(pdf) # Create a BytesIO object buffer = BytesIO() # Write the merged PDF to the BytesIO object merger.write(buffer) # Seek to the beginning of the BytesIO object buffer.seek(0) # Send the merged PDF file in the response return send_file( buffer, as_attachment=True, download_name="merged.pdf" ) return render_template('index.html') |
As you can see inside the above function we are first of all comparing the request
if it’s equal to POST
and then we are using the request
module to get access to all the files selected by the user. And then we are initializing the PDFFileMerger()
method to merge the pdf files into one pdf file. And then we are allowing the user to download
the output file as an attachment inside the browser.
Full Source Code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from flask import Flask, render_template, request, send_file from io import BytesIO from PyPDF2 import PdfFileMerger app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def merge_pdfs(): if request.method == 'POST': # Get the list of PDF files to merge pdf_files = request.files.getlist("pdf_files") # Create a PDF merger object merger = PdfFileMerger() # Loop through all the PDF files and add them to the merger for pdf in pdf_files: merger.append(pdf) # Create a BytesIO object buffer = BytesIO() # Write the merged PDF to the BytesIO object merger.write(buffer) # Seek to the beginning of the BytesIO object buffer.seek(0) # Send the merged PDF file in the response return send_file( buffer, as_attachment=True, download_name="merged.pdf" ) return render_template('index.html') if __name__ == '__main__': app.run() |
templates/index.html
1 2 3 4 |
<form method="post" enctype="multipart/form-data"> <input type="file" name="pdf_files" multiple> <input type="submit" value="Merge"> </form> |