【发布时间】:2021-12-01 11:47:36
【问题描述】:
我想在我的 django 项目中生成 pdf。我使用 pdfkit 模块将 html 页面转换为 pdf 文件。我使用此功能,但它有错误,例如:
操作系统错误
异常值:
找不到 wkhtmltopdf 可执行文件:“b''”
如果此文件存在,请检查此进程是否可以读取它,或者您可以在方法调用中手动将路径传递给它,检查自述文件。否则请安装 wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
def customer_render_pdf_view(request, *args, **kwargs):
pk = kwargs.get('pk')
customer = get_object_or_404(Person, pk=pk)
enrolment = get_object_or_404(Enrollment,pk=pk)
template_path = 'test.html'
context = {'customer': customer, 'enrolment':enrolment}
template = get_template(template_path)
html = template.render(context)
pdf = pdfkit.from_string(html, False)
response = HttpResponse(pdf, content_type='application/pdf' )
response['Content-Disposition'] = 'filename= "report.pdf"'
return response
【问题讨论】: