Forums

How to run wkhtmltopdf

Hi,

in local dev environment, I have installed wkhtmltopdf (https://wkhtmltopdf.org/downloads.html) to execute from Python App via pdfkit library.

Now, I want to deploy in my live environment at Pythonanywhere. How can I do that? I don't have control in the Server. It's possible?

Please, help me. Thanks

Yes, that should work OK. You'll need to use the pyvirtualdisplay module around any code that you have to call it, though. Something like this:

import subprocess
from pyvirtualdisplay import Display

with Display():
    subprocess.call("wkhtmltopdf XXXX YYYY ZZZZ", shell=True)

...where the XXXX and so on are the parameters that you want to pass in.

Thanks, but I don't understand. I give you more information...

If you see the code below, my doubt is in the line:

Location to wkhtmltopdf

config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')

I have to set the path to wkhtmltopdf file in Pythonanywhere. The path you see is my local machine. How I install wkhtmltopdf in Pythonanywhere?

myfunction.py:

import pdfkit
def create_pdf_invoice(invoice):
...stuffs...
# Location to wkhtmltopdf
config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
# CSS to add to template
css = os.path.join(conf_settings.STATIC_ROOT, 'shop', 'css', 'invoice-pdf.css')

# Saving the File
filepath = os.path.join(conf_settings.MEDIA_ROOT, 'invoice/')
pdf_save_path = filepath+filename
# Save the PDF
pdfkit.from_string(html, pdf_save_path, css=css, configuration=config, options=options)

The path on pythonanywhere:

13:42 PA-CONSOLE ~ $ which wkhtmltopdf
/usr/bin/wkhtmltopdf

Works fine. Thanks,

Glad to hear that!

I got this error on
pdfkit_config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf')

pdf = pdfkit.from_string(html, False, configuration=pdfkit_config, options={"enable-local-file-access": ""}) NO MATCH File "/home/EverestHomeCare/MissedEV/venv/lib/python3.10/site-packages/flask/app.py", line 867, in full_dispatch_request rv = self.dispatch_request() File "/home/EverestHomeCare/MissedEV/venv/lib/python3.10/site-packages/flask/app.py", line 852, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) File "/home/EverestHomeCare/MissedEV/app.py", line 64, in pdf_page pdf = pdfkit.from_string(html, False, configuration=pdfkit_config, options={"enable-local-file-access": ""}) File "/home/EverestHomeCare/MissedEV/venv/lib/python3.10/site-packages/pdfkit/api.py", line 75, in from_string return r.to_pdf(output_path) File "/home/EverestHomeCare/MissedEV/venv/lib/python3.10/site-packages/pdfkit/pdfkit.py", line 201, in to_pdf self.handle_error(exit_code, stderr) File "/home/EverestHomeCare/MissedEV/venv/lib/python3.10/site-packages/pdfkit/pdfkit.py", line 155, in handle_error raise IOError('wkhtmltopdf reported an error:\n' + stderr) OSError: wkhtmltopdf reported an error:

It looks like you cut off that traceback before the part where it shows what the error reported by wkhtmltopdf was.