Html is very handy to make beautiful graphic and it is nice to have some way to make a pdf file out of it, instead of using just text and images like we did in one of our recent post. It would be so easy. You could say:
- “Hey, I just can make a pdf out of an html from the browser, I can print and save it as a pdf, right?”
- “Yes, but you want to make a lot of rendering, like making a certificate with the stundents names and you have a lot of them, it would be very tedious to make alle the html pages and then print each one as a pdf file…”
Wouldn’t it be nice to do it automatically with a script. Well, this is what this script does, so let’s see how to do it. There is the very basic use of it, that you can easily personalize.
from flask import Flask from flask import render_template from flask import make_response import pdfkit app = Flask(__name__) @app.route("/") def index(): name = "Giovanni Smith" html = render_template( "certificate.html", name=name) pdf = pdfkit.from_string(html, False) response = make_response(pdf) response.headers["Content-Type"] = "application/pdf" response.headers["Content-Disposition"] = "inline; filename=output.pdf" return response if __name__ == "__main__": app.run(debug=True)
The video to render pdf from html with Flask
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame