如何将 Reportlab 与 Django 基于类的视图合并?

Ste*_*ith 1 django reportlab django-templates django-models django-views

我正在尝试将我的 HTML 转换为 PDF。我观看了本教程https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django/并且我能够成功地让教程使用硬编码值。问题是......我无法弄清楚如何将我的模型及其属性动态合并到这个示例中。

这是我遵循的步骤...

首先,我使用以下版本将 reportlab 安装到我的环境中...

pip install --pre xhtml2pdf 
Run Code Online (Sandbox Code Playgroud)

这工作...

然后我按照指示在我的项目中添加了一个 utils.py 文件。

然后我将此代码复制到我的 utils.py 文件中...

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None
Run Code Online (Sandbox Code Playgroud)

然后我创建了一个如下所示的 HTML 文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Title</title>
        <style type="text/css">
            body {
                font-weight: 200;
                font-size: 14px;
            }
            .header {
                font-size: 20px;
                font-weight: 100;
                text-align: center;
                color: #007cae;
            }
            .title {
                font-size: 22px;
                font-weight: 100;
               /* text-align: right;*/
               padding: 10px 20px 0px 20px;  
            }
            .title span {
                color: #007cae;
            }
            .details {
                padding: 10px 20px 0px 20px;
                text-align: left !important;
                /*margin-left: 40%;*/
            }
            .hrItem {
                border: none;
                height: 1px;
                /* Set the hr color */
                color: #333; /* old IE */
                background-color: #fff; /* Modern Browsers */
            }
        </style>
    </head>
    <body>
        <div class='wrapper'>
            <div class='header'>
                <p class='title'>Invoice # </p>
            </div>
        <div>
        <div class='details'>
            Bill to: {{ customer_name }} <br/>
            Amount: {{ amount }} <br/>
            Date: 
            <hr class='hrItem' />
        </div>
    </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我还创建了必要的 URL....

然后我创建了一个类似于下面定义的视图:

from django.http import HttpResponse
from django.views.generic import View

from yourproject.utils import render_to_pdf #created in step 4

class GeneratePdf(View):
    def get(self, request, *args, **kwargs):
        data = {
             'today': datetime.date.today(), 
             'amount': 39.99,
            'customer_name': 'Cooper Mann',
            'order_id': 1233434,
        }
        pdf = render_to_pdf('pdf/invoice.html', data)
        return HttpResponse(pdf, content_type='application/pdf')
Run Code Online (Sandbox Code Playgroud)

当我单击指向此视图的链接时……输出显示就像教程所说的那样……

但是,如果我试图让我的模型以这种格式显示......我不知道如何做到这一点。我尝试了一个 DetailView....然后我得到了数据但没有 PDF....我也搜索了很多其他地方,我似乎找不到一个例子可以让我动态获取我的模型并拉根据需要在属性中...提前感谢您的任何帮助或指示。

rud*_*dra 5

如果你想使用DetailView,我认为你可以这样做:

class SomeDetailView(DetailView):
    model = YourModel
    template_name = 'pdf/invoice.html'

    def get_context_data(self, **kwargs):
        context = super(SomeDetailView, self).get_context_data(**kwargs)
        # add extra context if needed
        return context

    def render_to_response(self, context, **kwargs):
        pdf = render_to_pdf(self.template_name, context)
        return HttpResponse(pdf, content_type='application/pdf')
Run Code Online (Sandbox Code Playgroud)

在这里,我render_to_response将覆盖 DetailView 的默认响应以返回 PDF 响应。在这里,传入的上下文来自get_context_data. get_context_data如果需要,您可以添加任何额外的上下文。