从模板生成pdf的django应用程序
django-xhtml2pdf的Python项目详细描述
注意
这个项目是django项目和xhtml2pdf之间的包装器代码 项目,两者都有一些维护,所以可能不是理想的项目。
如果你想接手维修工作,请与我们联系。
它所做的只是允许人们使用所有 很酷的django东西,比如静态url等等。(就像一个网页一样 模板),而utils函数使所有图像和资源显示在 pdf。
用法
只需执行以下操作:
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')
更改de pdf文件名:
from django_xhtml2pdf.utils import pdf_decorator @pdf_decorator(pdfname='new_filename.pdf') def myview(request): return render(request, 'mytemplate.html')