如何让WKHTMLTOPDF在Heroku l的Flask中工作?

2024-06-01 01:04:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Flask创建了一个应用程序,它接收一些用户输入,计算它们,然后根据这些数据生成pdf

我遵循了这一点,得到了相同的错误:How to get WKHTMLTOPDF working on Heroku?

这是我的密码:

@app.route('/download')
def download():
    credit_hours = aaa['Credit Hours']
    rendered = render_template("report2.html", credit_hours=credit_hours)
    def _get_pdfkit_config():
     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)
         
    pdf = pdfkit.from_string(rendered, False, configuration=_get_pdfkit_config())
    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = f'inline; filename={ag_number} GPA Report.pdf'
    
    return response

Requirements.txt:

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

错误,我得到的是Heroku上的“内部服务器错误”。这是log of heroku

上面的代码在我的本地机器上运行良好。但是当我将它部署到heroku时,我得到了“内部服务器错误” 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。


Tags: git服务器应用程序getherokureturnpdfresponse