简体   繁体   English

Python3 Django -> HTML 到 PDF

[英]Python3 Django -> HTML to PDF

There are a lot of different ways of generating pdfs from a django webpage in python2.有很多不同的方法可以从 python2 中的 django 网页生成 pdf。 The most clean, probably, is pisa and reportlab.最干净的可能是 pisa 和 reportlab。 These do not work for python3 though.但是,这些不适用于 python3。

So far, the only method I've had success with is to render the template, write it to a file, and then use wkhtmltopdf via subprocess.popen.到目前为止,我成功的唯一方法是渲染模板,将其写入文件,然后通过 subprocess.popen 使用 wkhtmltopdf。 This works alright, but it doesn't load any of my static files, such as css and images.这工作正常,但它不会加载我的任何静态文件,例如 css 和图像。

Are there any proper solutions?有什么合适的解决办法吗? can wkhtmltopdf read my staticfiles from the command line, in some way, or is there a library like pisa/reportlab, which supports python3? wkhtmltopdf 可以以某种方式从命令行读取我的静态文件,或者是否有像 pisa/reportlab 这样的库支持 python3?

I haven't been able to finde such a library我一直找不到这样的图书馆

You could use Weasyprint .你可以使用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))要将 Django 视图呈现为 HTML,您可以简单地使用快捷方式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.请注意,将 This 与同步 Web 服务器/WSGI 服务器一起使用时,所有请求都将被阻止,直到呈现 PDF。 So consider using an ASYNC Worker.所以考虑使用 ASYNC Worker。

I looked into Weasyprint, wkhtmltopdf and even LaTeX, but all have external binary dependencies that are difficult to deploy to services such as Heroku.我研究了 Weasyprint、wkhtmltopdf 甚至 LaTeX,但它们都具有难以部署到 Heroku 等服务的外部二进制依赖项。

The best combination I found so far that works in Django on Python 3 is using Reportlab (now works on Python 3) + xhtml2pdf .到目前为止,我发现在 Python 3 上的 Django 中工作的最佳组合是使用 Reportlab(现在在 Python 3 上工作)+ xhtml2pdf xhtml2pdf only recently added beta Python 3 support so you need to install it with: xhtml2pdf 最近才添加了 beta Python 3 支持,因此您需要安装它:

pip install --pre xhtml2pdf

If you have these two installed, you can either use xhtml2pdf directly or install the django-easy-pdf package which provides a TemplateView to inherit from and an example base template & styling to get you started quickly.如果你安装了这两个,你可以直接使用 xhtml2pdf 或安装django-easy-pdf包,它提供了一个模板视图来继承和一个示例基本模板和样式,让你快速入门。 Follow their quickstart instructions and you can quickly prepare something like a detail view that renders to PDF like:按照他们的快速入门说明,您可以快速准备诸如细节视图之类的渲染为 PDF 的内容,例如:

class InvoicePDFView(PDFTemplateView):
    template_name = "invoice_pdf.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        myinstance = get_object_or_404(MyModel, pk=context['pk'])
        context['myinstance'] = myinstance
        return context

And in your urls.py you'd add something like:在您的urls.py 中,您将添加如下内容:

url(r'invoice/(?P<pk>[^/]+)/$', InvoicePDFView.as_view(), name='invoice')

wkhtmltopdf requires wkhtmltopdf 需要

  • CSS to be inlined要内联的 CSS
  • static files to be locally present on the server静态文件本地存在于服务器上
  • static file urls to be os paths, eg: /home/ubuntu/project/project/static/file_name静态文件 url 是操作系统路径,例如: /home/ubuntu/project/project/static/file_name

I have tried pydf in Python3, its working fine.我曾尝试pydf在Python3,其工作的罚款。

pip install python-pdf

For python2 use pip install python-pdf==0.30.0对于python2使用pip install python-pdf==0.30.0

Documentation https://github.com/tutorcruncher/pydf文档https://github.com/tutorcruncher/pydf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM