本文内容用的 xhtml2pdf库,可以pip安装; 还有别的方法:https://github.com/JazzCore/python-pdfkit
好像页面的css与异步加载不会有效果
(一)主要生成部分代码:
# 开始渲染PDF页面
# 传给html的数据字典
context = {"er_code":er_code,"big_money":big_money,"account_make":account_make,"net_node":net_node,
"flow_num":flow_num,"elec_num":elec_num,"date":date,"exchange_maney":exchange_maney,
"receive_people":receive_people,"receive_account":receive_account,"pay_people":pay_people,
"pay_account":pay_account,"print_date":print_date}
response = HttpResponse(content_type="application/pdf")
response["Content-Disposition"] = \
f"attachment; filename=huidan.pdf"
#传过来页面与字典数据,渲染成页面并生成PDF,huidan_pdf.html会被生成PDF
html = render_to_string("account_manage/huidan_pdf.html", context=context)
font_patch()
status = pisa.CreatePDF(html,
dest=response,
link_callback=link_callback)
if status.err:
return HttpResponse("PDF文件生成失败")
return response
(二)整个业务代码
别看代码多,只要路由指向views里的huidan函数就行了,其他代码都是huidan函数调用的罢了
from xhtml2pdf import pisa
#