Python3 Django -> HTML to PDF

前端 未结 4 570
陌清茗
陌清茗 2020-12-15 00:28

There are a lot of different ways of generating pdfs from a django webpage in python2. The most clean, probably, is pisa and reportlab. These do not work for python3 though.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 00:53

    You could use Weasyprint. You could easily render directly.

    You could do something like that:

        html = HTML(string=htmlstring)
        main_doc = html.render()
        pdf = main_doc.write_pdf()
        return HttpResponse(pdf, content_type='application/pdf')
    

    To render your Django view to HTML, you could simply use the shortcut render_to_string(self.template_name, context, context_instance=RequestContext(self.request))

    Be aware, when using This with a Synchronous Webserver/WSGI Server ALL requests will be blocked until the PDF is rendered. So consider using an ASYNC Worker.

提交回复
热议问题