Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:使用pisa reportlab更改pdf文件名_Python_Django_Pdf_Views_Pisa - Fatal编程技术网

Python Django:使用pisa reportlab更改pdf文件名

Python Django:使用pisa reportlab更改pdf文件名,python,django,pdf,views,pisa,Python,Django,Pdf,Views,Pisa,我有一个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() p

我有一个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})
def write\u pdf(模板、上下文):
模板=获取模板(模板src)
上下文=上下文(上下文)
html=template.render(上下文)
结果=StringIO.StringIO()
pdf=pisa.pisaDocument(StringIO.StringIO(html.encode(“ISO-8859-1”)),结果)
如果不是pdf.err:
返回HttpResponse(result.getvalue(),mimetype='application/pdf')
响应['Content-Disposition']='附件;filename=Client_Summary.pdf'
返回响应
返回HttpResponse('我们有一些错误%s'%cgi.escape(html))
if not pdf.err:
    response = HttpResponse(result.getvalue(), mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=Client_Summary.pdf'
    return response
def客户摘要pdf(请求,客户id): client=models.client.objects.get(pk=client\u id) items=client.storageitem\u set.all() 返回write\u pdf('client\u summary.html'{ “页面大小”:“A4”, “客户”:客户, 'items':items})

如你所见,我试着说
response['Content-Disposition']='attachment;filename=Client_Summary.pdf'
,但这不起作用。它基本上打开/保存pdf文件时没有名字。

您的代码中有一个错误,因为您返回得有点早:


您的代码中有一个bug,因为您返回得有点早:


有什么方法可以克服这个问题吗?只需将第9行(在您的代码片段中)的“return”改为“response=”就可以了。遗憾的是,xhtml2pdf的文档太少了:(有没有办法克服这个问题?只需将第9行(在您的代码片段中)上的“return”改为“response=”就可以了。遗憾的是,xhtml2pdf的文档太少了:(