如何将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)
答案 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