html内容转pdf

本文内容用的  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

# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值