Skip to content Skip to sidebar Skip to footer

How To Get Wkhtmltopdf Working On Heroku?

I created a website which generates PDF using PDFKIT and I know how to install and setup environment variable path on Window. I managed to deploy my first website on Heroku but now

Solution 1:

It's non-trivial. If you want to avoid all of the below's headache, you can just use my service, api2pdf: https://github.com/api2pdf/api2pdf.python. Otherwise, if you want to try and work through it, see below.

1) Add this to your requirements.txt to install a special wkhtmltopdf pack for heroku as well as pdfkit.

git+git://github.com/johnfraney/wkhtmltopdf-pack.git
pdfkit==0.6.1

2) I created a pdf_manager.py in my flask app. In pdf_manager.py I have a method:

def_get_pdfkit_config():
     """wkhtmltopdf lives and functions differently depending on Windows or Linux. We
      need to support both since we develop on windows but deploy on Heroku.

     Returns:
         A pdfkit configuration
     """if platform.system() == 'Windows':
         return pdfkit.configuration(wkhtmltopdf=os.environ.get('WKHTMLTOPDF_BINARY', 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'))
     else:
         WKHTMLTOPDF_CMD = subprocess.Popen(['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf')], stdout=subprocess.PIPE).communicate()[0].strip()
         return pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_CMD)

The reason I have the platform statement in there is that I develop on a windows machine and I have the local wkhtmltopdf binary on my PC. But when I deploy to Heroku, it runs in their linux containers so I need to detect first which platform we're on before running the binary.

3) Then I created two more methods - one to convert a url to pdf and another to convert raw html to pdf.

defmake_pdf_from_url(url, options=None):
    """Produces a pdf from a website's url.
    Args:
        url (str): A valid url
        options (dict, optional): for specifying pdf parameters like landscape
            mode and margins
    Returns:
        pdf of the website
    """return pdfkit.from_url(url, False, configuration=_get_pdfkit_config(), options=options)

defmake_pdf_from_raw_html(html, options=None):
    """Produces a pdf from raw html.
    Args:
        html (str): Valid html
        options (dict, optional): for specifying pdf parameters like landscape
            mode and margins
    Returns:
        pdf of the supplied html
    """return pdfkit.from_string(html, False, configuration=_get_pdfkit_config(), options=options)

I use these methods to convert to PDF.

Solution 2:

Just follow these steps to Deploy Django app(pdfkit) on Heroku:

Step 1:: Add following packages in requirements.txt file

wkhtmltopdf-pack==0.12.3.0pdfkit==0.6.0

Step 2: Add below lines in the views.py to add path of binary file

import os, sys, subprocess, platform

if platform.system() == "Windows":
        pdfkit_config = pdfkit.configuration(wkhtmltopdf=os.environ.get('WKHTMLTOPDF_BINARY', 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'))
else:
        os.environ['PATH'] += os.pathsep + os.path.dirname(sys.executable) 
        WKHTMLTOPDF_CMD = subprocess.Popen(['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf')], 
            stdout=subprocess.PIPE).communicate()[0].strip()
        pdfkit_config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_CMD)

Step 3: And then pass pdfkit_config as argument as below

pdf = pdfkit.from_string(html,False,options, configuration=pdfkit_config)

Post a Comment for "How To Get Wkhtmltopdf Working On Heroku?"