0

I am using SimpleDocTemplate to genetare and download pdf. My current code generating the PDF corectly but the pdf getting open in another tab of chrome browser with empty page and Its asking for 'Open' or 'Save' in FireFox browser. Where shoud i change my code to directly download the PDF without opening it in another Tab of Chrome. Thanks in advance. This is the code..

 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

This is my URL:

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

This is my HTML template form to call that function in view:

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

Thanks You.

Javed
  • 1,613
  • 17
  • 16

2 Answers2

0

in your template you should have a link to the view like this

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

but you need to set the link as a download link like this

<a href="url" download >download</a>
mohammedgqudah
  • 568
  • 4
  • 15
0

The function GenerateTc works as expected (I have tested it). When you submit form, page reloads and my guess is that this triggers opening of a new tab (original tab with reloaded template and a new tab for PDF response). I suggest you change overall mechanism of this PDF generation, since all you are submitting is studentid.

Add argument student_id to GenerateTc function

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

Change url to

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

Finally, remove form and add a link in the template:

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

This should work.

Borut
  • 3,304
  • 2
  • 12
  • 18