Elr*_*meo 2 python pdf django weasyprint
是否可以在django中将多个pdf与weasyprint合并?
我有这样的事情:
def verpdf(request, pk):
odet = get_object_or_404(Note, pk = pk)
template = get_template('pdfnot.html')
template1 = get_template('pdfnot2.html')
p1 = template.render({'odet': odet}).encode(encoding="ISO-8859-1")
p2 = template1.render({'note':odet}).encode(encoding="ISO-8859-1")
pdf1 = HTML(string=p1).render()
pdf2 = HTML(string=p2).render()
all_pages = [po for po in pdf1.pages for doc in pdf2.pages]
pdf_file = pdf1.copy(all_pages).write_pdf()
http_response = HttpResponse(pdf_file, content_type='application/pdf')
http_response['Content-Disposition'] = 'filename="report.pdf"'
return http_response
Run Code Online (Sandbox Code Playgroud)
但是我无法加入这两个文件,总是只输出第一个模板,是否有可能将两个文件合并为一个pdf?你能帮助我吗?谢谢。
花了我一段时间,但我解决了,这是我的错,因为我不了解文档大声笑,如果有人遇到相同的问题,这是代码:
def verpdf(request, pk):
odet = get_object_or_404(Note, pk = pk)
template = get_template('pdfnot.html')
template1 = get_template('pdfnot2.html')
p1 = template.render({'odet': odet}).encode(encoding="ISO-8859-1")
p2 = template1.render({'note':odet}).encode(encoding="ISO-8859-1")
pdf1 = HTML(string=p1)
pdf2 = HTML(string=p2)
pdf11 = pdf1.render()
pdf12 = pdf2.render()
val = []
for doc in pdf11, pdf12:
for p in doc.pages:
val.append(p)
pdf_file = pdf11.copy(val).write_pdf() # use metadata of pdf11
http_response = HttpResponse(pdf_file, content_type='application/pdf')
http_response['Content-Disposition'] = 'filename="report.pdf"'
return http_response
Run Code Online (Sandbox Code Playgroud)
并带有两页的pdf输出。
| 归档时间: |
|
| 查看次数: |
2396 次 |
| 最近记录: |