跳转到主要内容

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

项目描述

注意

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

如果您想接管维护权,请联系我们。

它所做的是简单地允许人们使用所有酷炫的django功能,如STATIC_URL等创建xhtml2pdf模板(就像为网页模板做的那样),并且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

基于类的视图

您可以使用提供的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')

项目详情


下载文件

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

源分布

django-xhtml2pdf-0.0.4.tar.gz (4.7 kB 查看散列值)

上传

构建分布

django_xhtml2pdf-0.0.4-py3-none-any.whl (6.2 kB 查看散列值)

上传 Python 3

由以下提供支持