Welcome folks today in this blog post we will be converting raw text
to pdf
document using wkhtmltopdf
library in browser using html form. 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 wkhtmltopdf
pip install flask
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 |
from flask import Flask, render_template, request, send_file from subprocess import Popen, PIPE app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True) |
As you can see we are importing the flask
and also we are importing the wkhtmltopdf
library as well. And then we are writing a simple get
route where we will be loading the index.html
file when the user goes to the /
route.
Now we need to create the templates
directory where we will be storing the index.html
template as shown below
templates/index.html
1 2 3 4 5 |
<form action="/generate-pdf" method="post" enctype="multipart/form-data"> <textarea name="text"></textarea> <br> <input type="submit" value="Generate PDF"> </form> |
As you can see we have a simple textarea
widget where we will allow the user to write the raw
text to be converted to pdf document and then we have the submit button to make a simple post
request to generate the pdf document.
Generating the PDF Document Using WkhtmltoPDF
Now we need to write the below post
request where we will be generating the pdf
document and downloading it as an attachment inside the browser
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@app.route('/generate-pdf', methods=['POST']) def create_pdf(): text = request.form['text'] html = """ <html> <head> <title>My PDF</title> </head> <body> <p>{text}</p> </body> </html> """.format(text=text) with open('output.html', 'w') as f: f.write(html) p = Popen(['wkhtmltopdf', 'output.html', 'output.pdf'], stdin=PIPE, stdout=PIPE, stderr=PIPE) output, err = p.communicate() return send_file('output.pdf', as_attachment=True) |
As you can see we are taking the text
submitted inside the form and then we are using the wkhtmltopdf
command inside the terminal to create the pdf
from the text provided and then downloading it as an attachment using the send_file()
method as shown below
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 |
from flask import Flask, render_template, request, send_file from subprocess import Popen, PIPE app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/generate-pdf', methods=['POST']) def create_pdf(): text = request.form['text'] html = """ <html> <head> <title>My PDF</title> </head> <body> <p>{text}</p> </body> </html> """.format(text=text) with open('output.html', 'w') as f: f.write(html) p = Popen(['wkhtmltopdf', 'output.html', 'output.pdf'], stdin=PIPE, stdout=PIPE, stderr=PIPE) output, err = p.communicate() return send_file('output.pdf', as_attachment=True) if __name__ == '__main__': app.run(debug=True) |
templates/index.html
1 2 3 4 5 |
<form action="/generate-pdf" method="post" enctype="multipart/form-data"> <textarea name="text"></textarea> <br> <input type="submit" value="Generate PDF"> </form> |