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 }} {{ tc_detail.student.father_name }}
{{ 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.