Weasyprint:以base64创建pdf而不保存它

Phi*_* S. 2 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)
Run Code Online (Sandbox Code Playgroud)

Phi*_* S. 5

解决了。这是代码

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
Run Code Online (Sandbox Code Playgroud)