使用SimpleDocTemplate生成Django ReportLab PDF

时间:2017-11-14 06:46:00

标签: django python-2.7 pdf django-views reportlab

我正在使用SimpleDocTemplate来编写和下载pdf。我当前的代码核心生成PDF,但pdf在另一个带有空白页面的Chrome浏览器选项卡中打开,并要求在FireFox浏览器中“打开”或“保存”。 shoud我改变我的代码直接下载PDF而不在Chrome的另一个Tab中打开它。提前致谢。 这是代码..

 def GenerateTc(request):

    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename = "TC.pdf"'

    doc = SimpleDocTemplate(response,topMargin=20)
    doc.pagesize = landscape(A4)

    elements = []

    data= [

                 ['---OFFICE USE ONLY---'],
                 ['Date :','','ClassTeacher Signature: :',''],
                 ['Principal Signature :','','Accountant Signature :',''],
                 ['Student Name :'+' '+''],
                 ['Remark :','','',''],
          ]
    style =TableStyle([

                            ('SPAN',(2,15),(3,15)),
                            ('SPAN',(0,16),(1,16)),
                            ('SPAN',(2,16),(3,16)),
                            ('SPAN',(0,17),(3,17)),
                            ('SPAN',(0,18),(3,18)),
                    ])
    #Configure style and word wrap
    s = getSampleStyleSheet()
    s = s["BodyText"]
    s.wordWrap = 'CJK'
    ps = ParagraphStyle('title', fontSize=15, alignment=TA_CENTER,)
    data2 = [[Paragraph(cell, s) for cell in row] for row in data]
    t=Table(data2)
    t.setStyle(style)
    getSampleStyleSheet()
    elements.append(Paragraph("TRANSFER/DISCONTINUATION ACKNOWLEDGEMENT",ps))
    elements.append(Spacer(1,0.4*inch))
    elements.append(t)
    doc.build(elements)

    return response

这是我的网址:

url(r'^generate_tc/$',views.GenerateTc, name='generate_tc'),

这是我在视图中调用该函数的HTML模板表单:

<form action="{% url 'transfer_certificate:generate_tc' %}" id="form_sample_1" class="form-horizontal"
                  method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                <input class="form-control" id="studentid" name="studentid" type="hidden" value="">
                <table class="table table-striped table-bordered table-hover table-checkable order-column"
                       id="sample_1">
                    <thead>
                    <tr>
                        <th style="display:none">student ID</th>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Class</th>
                        <th>Section</th>

                        <th>Grant/Generate TC</th>
                    </tr>
                    </thead>
                    <tbody> {% for tc_detail in TCDetailsList %}
                        <tr style="cursor: pointer;">

                            <td style="display:none">{{ tc_detail.student.id }}</td>
                            <td>{{ forloop.counter }}</td>
                            <td>{{ tc_detail.student.first_name }} &nbsp;{{ tc_detail.student.father_name }}
                                &nbsp; {{ tc_detail.student.last_name }}</td>
                            <td>{{ tc_detail.student.academic_class_section.class_name }}</td>
                            <td>{{ tc_detail.student.academic_class_section.section_name }}</td>
                            <td><input type='submit' class="buttongenerate btn green btn-block" value='Generate TC'></td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </form>

谢谢你。

2 个答案:

答案 0 :(得分:0)

在您的模板中,您应该有一个指向该视图的链接

 <a href="url">download</a>

但是你需要将链接设置为像这样的下载链接

<a href="url" download >download</a>

答案 1 :(得分:0)

函数GenerateTc按预期工作(我已测试过)。当您提交表单时,页面重新加载,我的猜测是这会触发新选项卡的打开(带有重新加载的模板的原始选项卡和用于PDF响应的新选项卡)。我建议您更改此PDF生成的整体机制,因为您提交的所有内容都是studentid

将参数student_id添加到GenerateTc函数

def GenerateTc(request, student_id):
    ... your code ...
    return response

将网址更改为

url(r'^generate_tc/(?P<student_id>\w+)/$', views.GenerateTc, name='generate_tc')

最后,删除表单并在模板中添加一个链接:

<a href="{% url 'transfer_certificate:generate_tc' tc_detail.student.id %}">Generate TC</a>

这应该有用。