PDF response is corrupted in Python 3 but works in Python 2

岁酱吖の 提交于 2019-12-10 23:39:51

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/35368352/pdf-response-is-corrupted-in-python-3-but-works-in-python-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!