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

Fah*_*mal 5 django heroku python-pdfkit

在 Heroku 上托管我的 Django 应用程序后,当我尝试下载动态 pdf 时,我的 Django 应用程序的 wkhtmltopdf 会导致此错误。

在本地机器(Ubuntu)中我已经申请了

sudo apt-get install wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)

我还在项目目录中添加了 Aptfile,以便 Heroku 在构建应用程序时安装这些依赖项和要求。

apt文件:

wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)

但不幸的是 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
Run Code Online (Sandbox Code Playgroud)

我生成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
Run Code Online (Sandbox Code Playgroud)

服务器错误:

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
Run Code Online (Sandbox Code Playgroud)

woo*_*uwu 0

从这里找到答案:链接

首先,您需要确保安装了 wkhtmltopdf(不是通过 pip,请检查此链接以了解为您的操作系统安装它的方法:链接

您可以将可执行文件的路径添加到系统路径中,但它仍然不起作用。所需的必要额外代码是:

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

我尝试在成功运行后删除配置,如果没有配置参数值,它就无法工作,所以看起来每次使用该函数时都需要执行此操作。