django动态HTML转PDF方法

#从数据库中选择数据
sub_sy2_zts = Sub_SY2_ZT.objects.filter(student=request.user)
sub_sy2_chooses = Sub_SY2_Choose.objects.filter(student=request.user)
sub_sy2_TKs = Sub_SY2_TK.objects.filter(student=request.user)

if sub_sy2_zts.count() == 1 and sub_sy2_chooses.count() == 1 and sub_sy2_TKs.count() == 1: #若数据库中存在数据,将数据读取到前端页面,并生成pdf文件
options = { #关于pdfkit的一些配置
'page-size': 'A3',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}
t = render_to_response("look_experiment2.html", locals()) #变量t为byte类型
config = pdfkit.configuration(wkhtmltopdf=r"D:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")
pdfkit.from_string(t.content.decode("utf-8"), "out.pdf", options=options, configuration=config) # 字符串方式,必须加decode(“utf-8”),否则文件格式不对
return render(request,"look_experiment2.html",locals()) #结束返回读取到数据之后的页面
以上是关于如何动态生成Html到pdf的一段代码示例,希望对大家有所帮助。

转载于:https://www.cnblogs.com/gglll/p/10446711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值