I am trying to create a pdf file from an html with the WeasyPrint library, I am following this tutorial:
https://www.bedjango.com/blog/how-generate-pdf-django-weasyprint/
But is giving the error of the description. Could someone help me?
Edit: debugging I realized that the error occurs when I apply the 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
Thanks to anyone who saw the question and tried to resolve. Got it! The "b" mode was missing.
output = open(output.name, 'rb')