我有一个Django应用程序,它打印出一个pdf文件。我想知道如何更改pdf文件输出名称。以下是我的观点。
def write_pdf(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=Client_Summary.pdf'
return response
return HttpResponse('We had some errors<pre>%s</pre>' % cgi.escape(html))
def client_summary_pdf(request, client_id):
client = models.Client.objects.get(pk = client_id)
items = client.storageitem_set.all()
return write_pdf('client_summary.html',{
'pagesize' : 'A4',
'client':client,
'items':items})
正如您所看到的,我尝试过说response['Content-Disposition'] = 'attachment; filename=Client_Summary.pdf'
,但这不起作用。它基本上打开/保存没有名称的pdf文件。
答案 0 :(得分:6)
你的代码中有一个错误,因为你过早地回复了一下:
if not pdf.err:
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=Client_Summary.pdf'
return response