英文:
How to Merge Byte Files Together in Python
问题
我遇到了一个问题,我需要生成多个已填充的PDF表单,我已经填写了数据并且'bytes'文件可访问,但当我尝试组合这两个文件的字节表示时,什么都没有发生,文件被覆盖,只显示原始文件。
我做错了什么,这似乎应该很简单。
# 不要担心这些,它们是填充后的PDF字节形式,这部分按预期工作。
pdf1 = PDFFormManager.fill_with_jinja(file=template, data=data)
pdf2 = PDFFormManager.fill_with_jinja(file=template, data={})
# 这里出现了问题
print(len(pdf1), len(pdf2)) # 177354 177354
print(type(pdf1), type(pdf2)) # <class 'bytes'> <class 'bytes'>
print(len(pdf1+ pdf2)) # 354708
# 当我返回这个时,我只得到单个的PDF,而不是连接起来的PDF
response = HttpResponse(pdf1+pdf2, content_type="application/pdf")
英文:
I am running into a situation where I need to generate multiple filled PDF forms, I have the data filled out and the 'bytes' file is accessible, but when I attempt to combine the 2 files in their byte representation, nothing happens, the file is overridden and the original is the only thing shown.
What am I doing wrong, this seems like it should be easy.
# don't worry about these, they are filled PDFs in byte form, this works as expected.
pdf1 = PDFFormManager.fill_with_jinja(file=template, data=data)
pdf2 = PDFFormManager.fill_with_jinja(file=template, data={})
# here is the issue
print(len(pdf1), len(pdf2)) # 177354 177354
print(type(pdf1), type(pdf2)) # <class 'bytes'> <class 'bytes'>
print(len(pdf1+ pdf2)) # 354708
# when I return this, I only get the single pdf, not the concatenated one
response = HttpResponse(pdf1+pdf2, content_type=f"application/pdf")
答案1
得分: 0
解决方案是退一步,使用pypdf
合并来自writer
对象的内容。
例如:
pdf = PDFFormManager.fill_with_jinja(file=template, data=data)
pdf_merged_buffer = BytesIO()
merger = PdfMerger()
# merger.append(pdf)
merger.append(PdfReader(io.BytesIO(pdf)))
merger.append(PdfReader(io.BytesIO(pdf)))
merger.write(pdf_merged_buffer) # 现在显示所有页面合并在一起!
response = HttpResponse(pdf_merged_buffer.getvalue(), content_type="application/pdf")
response['Content-Disposition'] = 'inline; filename="file.pdf"'
response['Content-Length'] = len(response.content)
英文:
The solution was to take a step back and use pypdf
to merge the content from the writer
object.
For example:
pdf = PDFFormManager.fill_with_jinja(file=template, data=data)
pdf_merged_buffer = BytesIO()
merger = PdfMerger()
# merger.append(pdf)
merger.append(PdfReader(io.BytesIO(pdf)))
merger.append(PdfReader(io.BytesIO(pdf)))
merger.write(pdf_merged_buffer) # now shows all pages combined!
response = HttpResponse(pdf_merged_buffer.getvalue(), content_type=f"application/pdf")
response['Content-Disposition'] = 'inline; filename="{}"'.format('file.pdf')
response['Content-Length'] = len(response.content)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论