在我尝试下载动态 pdf 时在 Heroku 上托管我的 Django 应用程序后,我的 Django 应用程序的 wkhtmltopdf 会导致此错误。
在本地机器(Ubuntu)中我申请了
sudo apt-get install wkhtmltopdf
我还在我的项目目录中添加了 Aptfile,以便 Heroku 在构建应用程序时安装这些依赖项和要求。
文件:
wkhtmltopdf
但不幸的是,Heroku 甚至没有构建具有给定依赖项的应用程序。
我也尝试通过运行 Heroku bash 来安装它,但 Heroku 阻止了。
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
我生成pdf的代码是:
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
服务器错误:
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