5

After hosting my Django application on Heroku while I try to download dynamic pdf my Django app's wkhtmltopdf causes this error.

In local machine(Ubuntu) I've applied

sudo apt-get install wkhtmltopdf

I've also added Aptfile with my project directory so that Heroku installs those dependency and requirements while building the application.

Aptfile:

wkhtmltopdf

But unfortunately Heroku don't even builds the app with the given dependency.

I've also tried to install it by running Heroku bash but Heroku prevents.

W: Not using locking for read only lock file /var/lib/dpkg/lock-frontend
W: Not using locking for read only lock file /var/lib/dpkg/lock
E: Unable to locate package wkhtmltopdf

My code for generating pdf is:

def pdf_generation(request, pk):
    ''' Will generate PDF file from the html template '''
    template = get_template('bankApp/print_view.html')
    withdraw_obj = get_object_or_404(Withdraw, id=pk)
    year_month_day = withdraw_obj.withdrawn_on.split('-')
    day = year_month_day[2]
    month = year_month_day[1]
    year = year_month_day[0]
    word_amount = f'{num2words(withdraw_obj.withdrawn_amount)} Only'
    date = f'{day}{month}{year}'


    html = template.render({
        'pay_to': withdraw_obj.paid_to,
        'date': date,
        'amount': withdraw_obj.withdrawn_amount,
        'amount_word': word_amount
    })
    options = {
        'page-size': 'A4',
        'margin-top': '0.0in',
        'margin-right': '0.0in',
        'margin-bottom': '0.0in',
        'margin-left': '0.0in',
        'encoding': 'UTF-8',
    }


    pdf = pdfkit.from_string(html, False, options=options)
    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment;\
       filename="print.pdf"'

    return response

Server Error:

OSError at /bank/pdf/print/2

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

Request Method:     GET
Request URL:    https://issue-cheque.herokuapp.com/bank/pdf/print/2
Django Version:     3.1.7
Exception Type:     OSError
Exception Value:    

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

Exception Location:     /app/.heroku/python/lib/python3.6/site-packages/pdfkit/configuration.py, line 27, in __init__
Python Executable:  /app/.heroku/python/bin/python
Python Version:     3.6.13
Python Path:    

['/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python36.zip',
 '/app/.heroku/python/lib/python3.6',
 '/app/.heroku/python/lib/python3.6/lib-dynload',
 '/app/.heroku/python/lib/python3.6/site-packages']

Server time:    Sat, 13 Mar 2021 10:51:55 +0000
Fahad Md Kamal
  • 243
  • 6
  • 20

1 Answers1

0

Found the answer from here: Link

First you need to make sure you have wkhtmltopdf installed (not through pip, check this link for the way to install it for your OS: link)

You can add the path to the executable to your system path, but it still won't work. The necessary extra code needed is:

config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe") #replace with your path
pdfkit.from_file("filename.html", 'out.pdf', configuration=config) 

I tried removing the configuration after it ran successfully, it didn't work without the configuration parameter value, so it looks like you will need to do this every time you use the function.

wooshuwu
  • 133
  • 1
  • 3