django - 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
...
任何可以提供的指导将不胜感激!
解决方案
推荐阅读
- php - 如何在 php 中使用 shell_exec() 函数运行 ansible 命令
- flutter - Flutter - 可滚动的小部件列表
- c++ - 有没有办法在不使用两个 for 循环的情况下迭代两个容器
- python - df.groupby 一列,计算另一列中的值
- java - Tomcat 不排除在 tomcat.util.scan.StandardJarScanFilter.jarsToSkip 中指定的 JAR
- java - 如何在 onResponse 方法中有意图地传递数据
- python - OpenNI 和 OpenCV:cv2.imshow() 崩溃并出现错误:(-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage'
- java - 如何显示我的 ArrayList
我的 ScrollView 的联系人? - xml - 从 XML 节点转换 XHTML 并调用模板将 XHTML 作为上下文传递
- c - 数组中的最小元素