Skip to content

Instantly share code, notes, and snippets.

@JohnRDOrazio
Last active January 25, 2022 13:08
Show Gist options
  • Save JohnRDOrazio/8d20c94b04575e234b0d9c481e8a3bac to your computer and use it in GitHub Desktop.
Save JohnRDOrazio/8d20c94b04575e234b0d9c481e8a3bac to your computer and use it in GitHub Desktop.
from flask import make_response, Flask
import pdfkit
import os
path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
pdfoptions = {
"print-media-type": None,
"page-size": "A4",
"dpi": "300",
"margin-top": "0",
"margin-bottom": "0",
"margin-left": "0",
"margin-right": "0",
"zoom": "1.25"
}
validRoutes = [ 'sitehtml' ]
app = Flask(__name__)
@app.route("/<string:nroute>")
def html_to_pdf(nroute):
if nroute in validRoutes :
if nroute == 'sitehtml' :
pdfkit.from_file('C:\Apache24\htdocs\SitoHtmlToPdf\HTMLIntermedio.html','C:\Apache24\htdocs\SitoHtmlToPdf\HTMLIntermedio.pdf', configuration=config, options=pdfoptions)
if os.path.exists('C:\Apache24\htdocs\SitoHtmlToPdf\HTMLIntermedio.pdf'):
with open('C:\Apache24\htdocs\SitoHtmlToPdf\HTMLIntermedio.pdf', 'rb', buffering=0) as static_file:
r = make_response(static_file.read())
r.mimetype = 'application/pdf'
return r
@app.route("/")
def nothinguseful():
r = make_response( {"message":"INVALID_ROUTE"} )
r.mimetype = 'application/json'
return r
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment