PDF Generation from HTML File in Python
I am sharing my script to generate PDF file from HTML file in Python using PDFKit module.
Install Python PDF-Kit
$ pip install pdfkit
import pdfkit pdfkit.from_url('http://testpage.com', 'out.pdf') pdfkit.from_file('test.html', 'out.pdf') pdfkit.from_string('Hello!', 'out.pdf')
import os
import sysfrom flask import Flask,requestimport pdfkitfrom flask import make_responseimport logging.configapp = Flask(__name__)from decouple import config# Logger and handlersLOGGING = {'version': 1,'disable_existing_loggers': False,'formatters': {'console': {'format': '%(levelname)-1s: %(asctime)s [%(name)-12s.%(funcName)s.%(lineno)s] %(message)s',},'file': {'format': '%(levelname)-1s: %(asctime)s [%(name)-12s.%(funcName)s.%(lineno)s] %(message)s',},},'handlers': {'console': {'level': "DEBUG",'class': 'logging.StreamHandler','formatter': 'console','stream': sys.stdout},'file': {'level': "DEBUG",'class': 'logging.handlers.RotatingFileHandler','formatter': 'file','filename': 'htmltopdf.log',},},'loggers': {'': {'handlers': ['file'],'level': "DEBUG",'propagate': True},}}logging.config.dictConfig(LOGGING)LOGGER = logging.getLogger(__name__)@app.route("/")# Function for html to pdf convertordef function_html_to_pdf_convertor():try:logging.debug("calling func")html_file = request.args.get('f', default="", type=str)if html_file:html_file = '{}{}'.format(html_file,'.html')filepath = os.path.join(app.static_folder, html_file)filename = os.path.splitext(os.path.basename(filepath))[0]css = "static/style.css"options = {'page-size': 'Letter','encoding': "UTF-8",'zoom': 1.80,'footer-center': 'Page [page]/[topage]','footer-font-size': 8}if os.path.isfile(filepath):#pdf = pdfkit.from_file(filepath, css=css)pdf = pdfkit.from_file(filepath, False,options=options, css=css)response = make_response(pdf)response.headers.set('Content-Disposition', 'attachment', filename=filename + '.pdf')response.headers.set('Content-Type', 'application/pdf')return responseelse:logging.debug("File not found")return {"status": 404,"message": 'File not found'}logging.debug("'Please provide file'")return{"status": 400,"message": 'Please provide file'}except Exception as e:logging.debug("Bad request {}".format(e))return {"status": 400,"error": '{}'.format(e)}# main functionif __name__ == "__main__":app.run()