PDF格式的回复在Python 3中被破坏,但在Python 2中运行 [英] PDF response is corrupted in Python 3 but works in Python 2

查看:152
本文介绍了PDF格式的回复在Python 3中被破坏,但在Python 2中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python2.7和Flask中写了一个工作的应用程序。它所做的一件事是下载PDF发票。现在我正在做一个新的应用程序,也允许下载一个PDF发票,但我这次使用的是Python3。我可以将文件保存到服务器,并获得一个完美的工作PDF,但如果我尝试将其发送到浏览器,结果文件已损坏。



这里是下载函数:

$ $ $ $ $ $ $ $ c $ @ mod.route '/ get_invoice /< invoice_id>')
def get_invoice(invoice_id):
invoice = Invoices.query.filter_by(id = invoice_id).all()

pdf_generator = PDFInvoice(invoice)
pdf = pdf_generator.new()

response = make_response(pdf.output(dest ='S'))
response.headers ['Content-Disposition '] ='附件; filename =invoice.pdf'
response.headers ['Content-Type'] ='application / pdf'

return response

这与旧版Python2.7应用程序中的工作函数几乎相同。



(PDF):

 x 3R  2 35W( r
Q w3T30P ^ HISP ^ M ^ A ^ X ^ [^ YZ * ^ [^ Z ^ Y *(^他VG(^ V + $( e 奖h * d ^ @ ^ @ v^T

(损坏的PDF):

pre $ code> x ^3Rð235W(
Q \\ w3T°\\ 30P ^ HISp ^ M ^ A ^ É^ X ^ [è^ YZ * ^ O ^ [^泽^ Y 1 U * ^å¤(H ^ AE ^ Vg的2 O(^ O ^ V + $ 2 O(^ O ^×^×ëe§æ^ è¥^ ^ ^ * * * @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The The数据是文本字符串,它们看起来没有改变,所以它似乎是一个编码问题。

解决方案

FPDF输出一个 str 其中i Python 2基本上等同于 bytes ,但是在Python 3中是unicode,而不是字节。直接从文档:


如果您正在使用Python 3.x你必须使用 pdf.output(dest ='S')。encode('latin-1')为了得到输出,如果你不这样做生成的PDF将是无效的



I wrote a working application in python2.7 and Flask. One of the things it does is download a PDF invoice. It's working fine.

Now I am doing a new app that also allows downloading a PDF invoice, but I am using Python3 this time. I can save the file to the server and get a perfectly working PDF, but if I try to send it to the browser, the resulting file is corrupted.

Here is the download function:

@mod.route('/get_invoice/<invoice_id>')
def get_invoice(invoice_id):
    invoice = Invoices.query.filter_by(id=invoice_id).all()

    pdf_generator = PDFInvoice(invoice)
    pdf = pdf_generator.new()

    response = make_response(pdf.output(dest='S'))
    response.headers['Content-Disposition'] = 'attachment; filename="invoice.pdf"'
    response.headers['Content-Type'] = 'application/pdf'

    return response

This is nearly identical to the working function in the older Python2.7 application.

To expand, it is turning this (working PDF):

x�3R��2�35W(�r
Q�w3T��30P^HISp^M^A�^X^[�^YZ*�^[^Z�^Y�*��(h�e^Vg�(�^V+$�(����e����奖h*�d��^@^@�v^T�

into this (corrupted PDF):

x^Ü3Rðâ2Ð35W(çr
QÐw3T°Ô30P^HISp^M^A^É^X^[è^YZ*^Ø^[^Zê^Y^Û*^Ĥ(h^Äe^Vg^Ö(^Ô^V+$^Ö(^Ô^×^×ëe§æ^Õèå¥^Öh*^Äd^Áô^@^@øv^TÂ

The rest of the pdf's data is text strings, and they appear to be unchanged. So it seems to be an encoding issue someplace.

解决方案

FPDF outputs a str which in Python 2 is basically equivalent to bytes, but in Python 3 is unicode, not bytes. Straight from the docs:

If you are using Python 3.x you have to use pdf.output(dest='S').encode('latin-1') in order to get the output, if you don't do so the generated PDF will be invalid

这篇关于PDF格式的回复在Python 3中被破坏,但在Python 2中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆