django pdf出口 [英] django pdf export

查看:138
本文介绍了django pdf出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个PDF,它将以表格格式显示我的查询器的输出,例如:

I want to generate a PDF which will show the output of my queryset in table format, for example:

query = ModelA.objects.filter(p_id=100)

class ModelA(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    p_id = models.IntegerField()
    description = models.TextField()

我需要显示 name description pid 在生成的PDF中。

I need to show the values for name, description and pid in the generated PDF.

推荐答案

如其他人所提到的,最好的方式是生成然后,一个模板将结果(使用周围的许多库之一)转换为PDF。此方法为您提供了对模板的一般控制权,例如使用标签。

As mentioned by other people the best way to do this is to generate a template then convert the result, using one of the many libraries around, into a PDF. This method provides you with the usual amount of control over your templates, for example using tags.

我使用了之前提到的ReportLab /比萨设置,但发现它是相当有限的是,大多数布局必须使用表格构建,并且CSS2规范的许多部分尚未实现。

I've used the previously mentioned ReportLab/Pisa setup but found it to be quite limiting, most layouts have to be built using tables and many parts of the CSS2 spec aren't implemented yet.

更易于使用的库是 wkhtmltopdf ,这是WebKit的无头发布。这样可以让您的模板像任何Webkit浏览器一样,从而让您使用webkit特定的附加功能,例如WebKit中存在的CSS3规范的一部分。

An easier to use library is wkhtmltopdf which is a headless distribution of WebKit. This has the benefit of rendering your templates like any webkit browser would and thus letting you use webkit specific extras, such as parts of the CSS3 spec that exist in WebKit.

使用包装库 django-wkhtmltopdf 您可以 render_to_pdf 在您的视图,而不是通常的Django render_to_response

Using the wrapper library django-wkhtmltopdf you can render_to_pdf in your view instead of the usual Django render_to_response.

免责声明:我是这个图书馆的贡献者。

Disclaimer: I am a contributor to this library.

此库已转换为CBV,大部分信息(我将离开帮助添加一些上下文)现在在库本身中实现方便。

This library has been converted to CBVs and most of the information below (which I'll leave to help add some context) is now implemented in the library itself for convenience.

请参阅 quickstart docs为例说明如何实现下面的代码块。如果您需要使用更高级的使用,您可以将 PDFTemplateView 继承,并添加文件名和边距等各种选项。

See the quickstart docs for an example of how to implement the below code block. If you need to use more advanced usage you can subclass PDFTemplateView and add various options like filename and the margins.

一个示例视图:

from django.shortcuts import render_to_response
from wkhtmltopdf import render_to_pdf

def pdf(request):
    context.update({'objects': ModelA.objects.filter(p_id=100)})

    kwargs = {}
    if request.GET and request.GET.get('as', '') == 'html':
        render_to = render_to_response
    else:
        render_to = render_to_pdf
        kwargs.update(dict(
            filename='model-a.pdf',
            margin_top=0,
            margin_right=0,
            margin_bottom=0,
            margin_left=0))

    return render_to('pdf.html', context, **kwargs)

这里的条件语句允许您将?as = html传递给视图,以便您可以在该视图中进行开发浏览器。这是一个很丑的方式来做,但目前有计划解决这个在一个发布很快。

The conditional statement here lets you pass ?as=html to the view so you can develop in the browser. It's a bit of an ugly way to do it currently but there are plans to fix this in a release soon.

使用这个视图,你可以循环<$ c的内容$ c>对象在您的视图,通常,甚至扩展您的基本模板。我通常使用不同的样式表为PDF的维护性和可读性的风格,因为你需要做一些不同的PDF的东西,如设置最小高度,如果你希望保持你的页脚块在同一个地方。

Using this view you could loop the contents of objects in your view as you would normally and even extend your base template. I've normally used a different stylesheet specifically for the PDFs for maintainability and readability of the styles as you need to do a few things differently for PDFs, such as setting a min-height if you want to keep your footer block in the same place.

在本笔记中,您可以创建将在PDF的每个页面上使用的页眉和页脚模板,将其传递到render_to_pdf作为 kwargs 。

On this note, you can create header and footer templates that will be used on each page of your PDF by passing them into render_to_pdf as part of kwargs.

这篇关于django pdf出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆