Weasyprint:在base64中创建pdf而不保存

时间:2019-05-15 15:05:13

标签: pdf base64 weasyprint

如何将Weasyprint PDF编码为base64字符串?

这是我当前的代码,用于为Django视图生成PDF以在浏览器中打开PDF。

template = get_template("payment/invoice/default.html")
context = self.get_context_data()
html = template.render(context)
response = HttpResponse(content_type="application/pdf")
HTML(string=html).write_pdf(response)

1 个答案:

答案 0 :(得分:0)

解决了。这是代码

template = get_template("payment/invoice/default.html")
context = self.get_context_data()
html = template.render(context)
byte = HTML(string=html).write_pdf()
encoded = base64.b64encode(byte)
encoded = encoded.decode('utf-8')
return encoded