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
Some Simple Examples:
import pdfkit
pdfkit.from_url('http://testpage.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
My Sample Script to generate PDF from HTML page.
import os
import sys
from flask import Flask,request
import pdfkit
from flask import make_response
import logging.config
app = Flask(__name__)
from decouple import config
# Logger and handlers
LOGGING = {
'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 convertor
def 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 response
else:
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 function
if __name__ == "__main__":
app.run()