Heroku上的Django应用用pdfkit生成PDF时提示找不到wkhtmltopdf可执行文件
Heroku平台Django应用动态PDF生成功能异常记录
背景与已执行操作
- 应用基于Django开发,托管于Heroku平台,目标为向用户提供动态PDF文件下载能力
- 本地Ubuntu开发环境已执行
sudo apt-get install wkhtmltopdf完成组件安装,本地PDF生成功能运行正常 - 为使Heroku在构建阶段自动安装对应系统依赖,已在项目根目录添加内容为
wkhtmltopdf的Aptfile,但配置未生效,应用未顺利完成构建流程 - 尝试通过Heroku bash交互环境手动安装组件,受平台文件系统只读限制,安装操作被阻断
手动安装环节报错输出
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
线上接口调用异常详情
实际访问PDF生成接口时,服务抛出OSError,完整报错栈与信息如下:
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 - 对应组件安装说明地址 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 - 对应组件安装说明地址 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
内容的提问来源于stack exchange,提问作者Fahad Md Kamal
相关产品推荐
相关产品推荐

