在Python中如何合并字节文件

huangapple go评论85阅读模式
英文:

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&#39;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))   # &lt;class &#39;bytes&#39;&gt; &lt;class &#39;bytes&#39;&gt;
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&quot;application/pdf&quot;)

答案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&quot;application/pdf&quot;)
response[&#39;Content-Disposition&#39;] = &#39;inline; filename=&quot;{}&quot;&#39;.format(&#39;file.pdf&#39;)
response[&#39;Content-Length&#39;] = len(response.content)

huangapple
  • 本文由 发表于 2023年7月13日 23:14:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定