weasyprint 生成发票pdf文件

谁借莪1个温暖的怀抱¢ 2022-06-09 08:15 276阅读 0赞
  • 语言Python

    from django.conf import settings
    from django.http import HttpResponse
    from django.template.loader import render_to_string
    import weasyprint

    @staff_member_required
    def admin_order_pdf(request, order_id):

    1. order = get_object_or_404(Order, id=order_id)
    2. # 渲染
    3. html = render_to_string('orders/order/pdf.html',
    4. {

    ‘order’: order})

    1. # 构造response
    2. response = HttpResponse(content_type='application/pdf')
    3. response['Content-Disposition'] = 'filename=\ "order_{}.pdf"'.format(order.id)
    4. # 写入
    5. weasyprint.HTML(string=html).write_pdf(response,
    6. stylesheets=[weasyprint.CSS(
    7. settings.STATIC_ROOT + 'css/pdf.css')])
    8. return response

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot实现电子发票生成

    在本文中,我们将介绍如何使用Spring Boot开发一个仿真电子发票生成的应用程序。我们将会使用vue作为前端框架,后端使用Spring Boot,并借助微信二维码扫描功能来

    相关 Itext生成PDF文件

    最近做一个项目的报表,设计导出问题,要求pdf、excel、word。说实话一个字,烦。写个备忘录吧。。。虽然还是很烦 所需依赖:itext-asian-5.2.0.jar