跳转到主要内容

一个从模板生成PDF的Django应用程序

项目描述

注意

这是https://github.com/xhtml2pdf/django-xhtml2pdf项目的分支。

该项目是django项目和xhtml2pdf项目之间的包装代码,两者都有一些维护,可能不适合您的项目。

它所做的是简单地允许人们使用所有酷炫的django功能(如STATIC_URL等)创建xhtml2pdf模板(就像网页模板一样),而utils函数则使所有图像和资源都显示在PDF中。

此版本(0.0.7)支持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

基于类的视图

您可以使用提供的PdfMixin与任何子类化TemplateView的视图一起使用,例如

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')

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码发行版

lanshark-django-xhtml2pdf-0.0.7.tar.gz (45.3 kB 查看哈希值)

上传时间 源代码

构建发行版

lanshark_django_xhtml2pdf-0.0.7-py2.py3-none-any.whl (5.8 kB 查看哈希值)

上传时间 Python 2 Python 3

支持