简体   繁体   English

PyFPDF 在编码为字节字符串后返回空白 pdf

[英]PyFPDF returns a blank pdf after encoding as a byte string

I need to convert a PyFPDF object into a byte string first, and then save it as a file.我需要先将 PyFPDF 对象转换为字节字符串,然后将其另存为文件。 However, the following code saves a blank PDF file.但是,以下代码会保存一个空白的 PDF 文件。 When I add pages, the blank pages are added, but all text disappears.添加页面时,添加了空白页面,但所有文本都消失了。 What can I do to solve this problem?我能做些什么来解决这个问题?

The PDF creation code is taken from the Hello World example. PDF 创建代码取自Hello World示例。

pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, 'Hello World!')
pdf.close()
return_byte_string = pdf.output("output_file.pdf", 'S')

with open("output_file.pdf", "w") as pdf_file:
    pdf_file.write(return_byte_string)    

如果您使用的是 python 3.x,则需要执行以下操作。

return_byte_string = pdf.output("output_file.pdf", 'S').encode('latin-1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM