hi, I am developing a pdf generator app using pdfkit in a streamlit project. pdfkit runs on top of wkhtmltopdf (binary), which I installed on my windows machine, but which now is missing in my heroku environment. I found several posts on how to install wkhtmltopdf on heroku, but none seem to work. They all seem to target a ruby or django environment. Does anyone have any experience with pdfkit with streamlit on heroku? Are there better htrml>pdf libraries, where one does not have to go through the hassle of installing binaries? Thanks in advance for any advice!
Lukas
Please be more specific - what does not work?
the error is:
OSError: No wkhtmltopdf executable found: "b''" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
and further down I get a similar message : No wkhtmltopdf executable found.
clearly the binary wkhtmltopdf is not found, however I installed it using:
heroku plugins:install https://github.com/heroku/heroku-repo.git --app=pgmn-waterlevel-reports
and there were not error message, The buildback also shows when generate a list:
C:\>heroku buildpacks --app=pgmn-waterlevel-reports
=== pgmn-waterlevel-reports Buildpack URLs
1. heroku/python
2. https://github.com/dscout/wkhtmltopdf-buildpack.git
Hello @godot63 please try to read this article:
The article helps me to solved my problem with wkhtmltopdf on heroku.
Just a simple note, I downgraded my stack from heroku-20 to heroku-18 to be able to use the wkhtmltopdf package.
Hopes this helps!
thanks for this reference. I had found and followed very similar instructions and had failed. I did not know whether the instructions were out of date or if it was just me. Now I know: it was just me. Which of the proposed solutions in the article did work for you? Could you possibly share the function-code, where the pdf document is generated? Thanks very much in advance. Lukas
Hello @godot63 ,
Iâve followed the method 2 (Method 2: buildpack) in the article.
- Did you change the heroku-stack from heroku-20 to heroku-18?
- Did you add the following buildpacks?
This my simple implementation:
main.py
`import os, subprocess, platform
import pdfkit
from flask import make_response, render_template, Flask
app = Flask(name)
if platform.system() == âWindowsâ:
pdfkit_config = pdfkit.configuration(
wkhtmltopdf=os.environ.get(âWKHTMLTOPDF_PATHâ, âC:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exeâ)
)
else:
WKHTMLTOPDF_CMD = subprocess.Popen([âwhichâ, os.environ.get(âWKHTMLTOPDF_PATHâ, â/app/bin/wkhtmltopdfâ)],
stdout=subprocess.PIPE).communicate()[0].strip()
pdfkit_config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_CMD)
data = {
âorder_idâ: 123,
âorder_creation_dateâ: â2020-01-01 14:14:52â,
âcompany_nameâ: âcompany nameâ,
âcityâ: âcity nameâ,
âstateâ: âstate nameâ,
}
@app.route(â/downloads/pdfâ)
def download_report():
html = render_template(âreport.htmlâ, json_data=data)
pdf = pdfkit.from_string(html, False, configuration=pdfkit_config)
response = make_response(pdf)
content_disposition = âattachment; filename=report.pdfâ
response.headers[âContent-Typeâ] = âapplication/pdfâ
response.headers[âContent-Dispositionâ] = content_disposition
return response
if name == âmainâ:
app.run(
host=â0.0.0.0â,
port=8080,
debug=True
)
`
templates/report.html
Test
Name: {{ json_data.city }}City/State: {{ json_data.city }}, {{ json_data.state }}
Date: {{ json_data.order_creation_date }}
Order ID: {{ json_data.order_id }}
If you encounter any error building the application on heroko please share it on your comment.
it works, thank you so much for your precious help.
The app is here in case you are interested, in the end this should become a report generator for water level reports of groundwater monitroing wells across Ontario, Canada.
Now that it works on the happy path, I will continue development.
Thatâs great @godot63 , Happy coding !