首页 > 解决方案 > Django zip 使用 xhtml2pdf 生成的多个 PDF 文件

问题描述

我成功地xhtml2pdf用来生成一个PDF文件以供下载。

我现在想要实现的是能够生成多个(单独的)PDF文件以供下载。我猜想实现这一目标的最佳方法是压缩PDF's,但我不知道如何实现这一目标。有人能指出我正确的方向吗?

我的渲染功能PDF是:

def render_to_pdf(template_src, context_dict={}): 
    template =   get_template(template_src) 
    html = template.render(context_dict) 
    result = BytesIO() 
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")),result, link_callback=link_callback) 
    if not pdf.err: 
        return HttpResponse(result.getvalue(), content_type='application/pdf') 

    return None

我对生成单个PDF文件的看法是:

class GeneratePDF(View):
    def get(self, request, *args, **kwargs):
        date = datetime.datetime.now()
        context = { 'date':date }
        pdf = render_to_pdf('pdf/my_first_file.html', context)
        if pdf:
            response = HttpResponse(pdf, content_type='application/pdf')
            filename = "First_File.pdf"
            content = "attachment; filename='%s'" %(filename)
            response['Content-Disposition'] = content
            return response

        return HttpResponse("Not found")

我想作为下载的PDF一部分渲染的其他文件zip都在该pdf/文件夹中。其中最多有 12 个。每个人的上下文都不同:

pdf/my_second_file.html
pdf/my_third_file.html
...

任何可以提供的指导将不胜感激!

标签: djangozipfilexhtml2pdf

解决方案


推荐阅读