heroku 找不到wkhtmltopdf可执行文件:“b''”如果此文件存在,请检查此进程是否可以读取它

oknwwptz  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(183)

在Heroku上托管我的Django应用程序后,当我尝试下载动态pdf时,我的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
efzxgjgh

efzxgjgh1#

从这里找到了答案:Link
首先,你需要确保你已经安装了wkhtmltopdf(不通过pip,检查此链接的方式来安装它为您的操作系统:link
你可以将可执行文件的路径添加到你的系统路径中,但是它仍然不起作用。需要额外的代码:

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

我试着在配置成功运行后删除它,如果没有配置参数值,它就不起作用,所以看起来你每次使用这个函数时都需要这样做。

相关问题