我有一个 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文件。