English 中文(简体)
django pdf export
原标题:django pdf export

我想产生一个PDF,以表格形式显示我的询问结果,例如:

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

我需要在生成的PDF中显示<代码> 姓名、descriptionpid的数值。

问题回答

正如其他人提到的那样,这样做的最佳方式是制作一个模板,然后将结果变成国防军,使用周围许多图书馆之一。 这种方法为你提供了对模板的通常控制,例如使用标签。

I ve使用了前面提到的《劳工/灾难报告》的设置,但认为这相当有限,大多数布局必须使用表格,而CSS2光谱的许多部分尚未执行。

http://code.google.com/p/wkhtmltopdf/“rel=“nofollow noreferer”>wkhtmltopdf。 它是网上Kit的不间断分布。 这样做的好处是,使你的模板像任何网络浏览器一样,使你能够使用网基特特定额外数据,例如网上Kit存在的CSS3光谱的一部分。

https://github.com/incuna/django-wkhtmltopdf rel=“nofollow noreferer”>django-wkhtmltopdf。 您可以查阅render_to_pdf,而不是通常的Djangorender_to_response<>。

申斥: 我是该图书馆的撰稿人。

Update

该图书馆已经改名为CBVs,以下大部分信息(我可以帮助补充某些情况)现已在图书馆本身实施方便。

quickstart。 举例说明如何执行以下编码部分。 如果需要更先进的使用,请将PDFTemplateView分类,并增加各种选择,如档案名称和间隙。

实例:

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)

有条件的发言请你通过? 页: 1 它为目前这样做提供了一种简单的方法,但计划很快释放。

采用这一看法,你可以照搬<代码>目标/代码”的内容,因为你通常甚至会延长基数。 我通常为国防军使用不同的风格,以维持和读写风格,因为你们需要为国防军做几件不同的事情,例如,如果你想把你的脚石放在同一个地方,就设置了一张min。

关于本说明,你可以制作头版和脚版模板,作为<条码>kwargs的一部分,将其输入_to_pdf。

仅产生超文本,请xhtml2pdf将其改为pdf。

Like one of the comment mentions, there is ReportLab, but that would need you to give specification in PDF s required format. It provides you more control, but if it is easier for you to generate HTML, the standard way which you need to generate anyway, you can use pisa to make it pdf.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签