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!