Deploying an app using using wkhtmltopdf on herokou

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! :grin:

Hi @namespace1100

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.

  1. Did you change the heroku-stack from heroku-20 to heroku-18?
  2. 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.

hi @namespace1100

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.

1 Like

That’s great @godot63 , Happy coding :grin:!