从模板生成pdf的django应用程序
lanshark-django-xhtml2pdf的Python项目详细描述
注意
这是https://gitlab.com/lansharkconsulting/django/lanshark-django-xhtml2pdf项目的一个分支。
这个项目是django项目和xhtml2pdf之间的包装器代码 项目,两者都有一些维护,所以可能不是理想的项目。
它所做的只是允许人们使用所有 很酷的django东西,比如静态url等等。(就像一个网页一样 模板),而utils函数使所有图像和资源显示在 pdf。
这个版本(0.0.5)支持django 2.0和python 3.6。
注意:
虽然这个项目是在有限的基础上为遗留项目维护的,
我们强烈建议新项目考虑使用
WeasyPrint <https://weasyprint.org>
。
用法
只需执行以下操作:
from django_xhtml2pdf.utils import generate_pdf def myview(response): resp = HttpResponse(content_type='application/pdf') result = generate_pdf('my_template.html', file_object=resp) return result
基于类的视图
可以使用提供的PDFIXIN与子类TEMPPLATEVIEW的任何视图, 示例:
from django.views.generic.detail import DetailView from django_xhtml2pdf.views import PdfMixin from .models import Product class ProductPdfView(PdfMixin, DetailView): model = Product template_name = "product_pdf.html"
它将以pdf格式输出视图的呈现内容。
装饰工
只需执行以下操作:
from django_xhtml2pdf.utils import pdf_decorator @pdf_decorator def myview(request): return render(request, 'mytemplate.html')
更改pdf文件名:
from django_xhtml2pdf.utils import pdf_decorator @pdf_decorator(pdfname='new_filename.pdf') def myview(request): return render(request, 'mytemplate.html')