Welcome folks today in this blog post we will be rotating pdf
document by any angle
in any direction
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 werkzeug.utils import secure_filename import os import PyPDF2 app = Flask(__name__) @app.route('/') def index(): 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 rotate
pdf document using any angle
. 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 5 6 |
<form method="post" action="/rotate" enctype="multipart/form-data"> <input type="file" name="file"> <label for="angle">Angle:</label><br> <input type="text" id="angle" name="angle"><br> <input type="submit" value="Rotate"> </form> |
As you can see we have the simple input field
where we allow the user to select pdf
files and then we have the input
field where we allow the user to enter any angle
to which we need to rotate
the pdf document and then we have the submit
button to rotate the pdf document. Now if you start the flask
app using the below command
python app.py
Now we need to write the post
request to actually rotate
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 |
@app.route('/rotate', methods=['POST']) def rotate(): # Get the uploaded file and the rotation angle file = request.files['file'] angle = request.form['angle'] filename = secure_filename(file.filename) # Save the file to a temporary directory file.save(os.path.join('/tmp', filename)) # Open the PDF and rotate each page with open(os.path.join('/tmp', filename), 'rb') as f: pdf = PyPDF2.PdfFileReader(f) output = PyPDF2.PdfFileWriter() for i in range(pdf.getNumPages()): page = pdf.getPage(i) page.rotateClockwise(float(angle)) output.addPage(page) with open(os.path.join('/tmp', 'rotated.pdf'), 'wb') as f: output.write(f) # Return a link to download the rotated PDF return '<a href="/download">Download Rotated PDF</a>' |
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 PDFFileReader()
method to read the content of the pdf document which is selected and then we are using the for loop
to rotate
the pages using custom
angle and then we are using the rotateClockwise()
method to rotate the pdf document and we are passing the angle
as an argument. And then we are allowing the user to download
the pdf document as an attachment inside the browser.
Now we need to write the download
request inside the app.py
to allow the user to download
the rotated pdf document pages as separated pdf
documents as shown below
1 2 3 4 |
@app.route('/download') def download(): # Send the rotated PDF as a response return send_file('/tmp/rotated.pdf', as_attachment=True) |
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 40 41 42 |
from flask import Flask, render_template, request, send_file from werkzeug.utils import secure_filename import os import PyPDF2 app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/rotate', methods=['POST']) def rotate(): # Get the uploaded file and the rotation angle file = request.files['file'] angle = request.form['angle'] filename = secure_filename(file.filename) # Save the file to a temporary directory file.save(os.path.join('/tmp', filename)) # Open the PDF and rotate each page with open(os.path.join('/tmp', filename), 'rb') as f: pdf = PyPDF2.PdfFileReader(f) output = PyPDF2.PdfFileWriter() for i in range(pdf.getNumPages()): page = pdf.getPage(i) page.rotateClockwise(float(angle)) output.addPage(page) with open(os.path.join('/tmp', 'rotated.pdf'), 'wb') as f: output.write(f) # Return a link to download the rotated PDF return '<a href="/download">Download Rotated PDF</a>' @app.route('/download') def download(): # Send the rotated PDF as a response return send_file('/tmp/rotated.pdf', as_attachment=True) if __name__ == '__main__': app.run() |
templates/index.html
1 2 3 4 5 6 |
<form method="post" action="/rotate" enctype="multipart/form-data"> <input type="file" name="file"> <label for="angle">Angle:</label><br> <input type="text" id="angle" name="angle"><br> <input type="submit" value="Rotate"> </form> |