【问题标题】:'utf8' codec can't decode byte 0xb5 in position 0: invalid start byte error“utf8”编解码器无法解码位置 0 中的字节 0xb5:无效起始字节错误
【发布时间】:2020-09-20 21:31:57
【问题描述】:

我正在尝试使用 Wea​​syPrint 库从 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


    【解决方案1】:

    感谢任何看到问题并尝试解决的人。知道了!缺少“b”模式。

    output = open(output.name, 'rb')
    

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 2014-07-09
      • 2014-04-08
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多