【发布时间】:2020-09-20 21:31:57
【问题描述】:
我正在尝试使用 WeasyPrint 库从 html 创建一个 pdf 文件,我正在关注本教程:
https://www.bedjango.com/blog/how-generate-pdf-django-weasyprint/
但是给出了描述的错误。有人可以帮我吗?
编辑: 调试发现应用output.read()时出现错误
def termoPDFView(request, id):
termo = TermoReferenciaPregao.objects.get(id=id)
html_string = render_to_string('painel/report/termo-referencia-pdf.html', {'termo': termo})
pdf = HTML(string=html_string).write_pdf()
response = HttpResponse(content_type='application/pdf;')
response['Content-Disposition'] = 'inline; filename=termo-referencia.pdf'
response['Content-Transfer-Encoding'] = 'utf-8'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(pdf)
output.flush()
output = open(output.name, 'r')
response.write(output.read())
return response
【问题讨论】:
标签: django python-3.x weasyprint