0

当用户单击下载按钮时,我想生成多个 pdf 目前我只能生成一个 PDF

我想要的是当用户单击带有 weasyprint 的下载按钮时从 Django 视图生成两个 PDF。

下面的代码只生成单个 PDF

def get(self, *args, **kwargs):
    obj = self.get_object()
    html_result = super(GenerateInvoicePDFView, self).get(*args, 
    **kwargs)
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="%s.pdf"' % 
    obj.name
    weasyprint.HTML(string= html_result.getvalue()).write_pdf(response)
    return response

这个响应应该生成两个 PDF,有可能吗?请帮忙谢谢

4

1 回答 1

0

您不能在响应中返回多个文件。我看到的唯一解决方案是压缩它们,通过电子邮件将它们发送给用户,或者创建两个单独的下载按钮。

怎么样:

看法:

def get(self, *args, **kwargs):
    if 'file-1' in self.request.GET:
        obj = self.get_object(file='file-1')
    else:  # I assume you always want to download some file
        obj = self.get_object(file='file-2')

    html_result = super(GenerateInvoicePDFView, self).get(*args, 
    **kwargs)
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="%s.pdf"' % 
    obj.name
    weasyprint.HTML(string= html_result.getvalue()).write_pdf(response)
    return response

模板:

<form action="" method="get">
    {{ form }}
    <input type="submit" name="file-1" value="Download file 1" />
    <input type="submit" name="file-2" value="Download file 2" />
</form>
于 2017-09-02T09:02:32.907 回答