简体   繁体   English

python3.x:使用cgi在浏览器中显示pdf

[英]python3.x: display pdf in browser with cgi

I am upgrading some code from python2 to python3. 我正在将一些代码从python2升级到python3。 I have the following cgi script to display a pdf in the browser. 我有以下cgi脚本在浏览器中显示pdf。 It works fine in python2, but doesn't work in python3. 它在python2中工作正常,但在python3中不工作。 In python 3, there is no server error, but rather I get an error from the browser which says "Failed to load PDF document." 在python 3中,没有服务器错误,但是从浏览器中显示了一个错误,提示“无法加载PDF文档”。

I believe the problem has to do with the encoding, but I am at a loss after searching for a few hours. 我相信问题与编码有关,但是经过几个小时的搜索后,我感到茫然。 The current CGI script is here: 当前的CGI脚本在这里:

#!/usr/local/bin/python3.7

filename='file.pdf'
file = open(filename,'rb')
fileData=file.read()
file.close() 

print('Content-Disposition: inline; filename="out.pdf"')
print('Content-type: application/pdf')
print('')
print(fileData)

The following code works for me: 以下代码对我有用:

#!/usr/local/bin/python3.7

filename='file.pdf'
file = open(filename,'rb')
fileData=file.read()
file.close() 

print('Content-Disposition: inline; filename="out.pdf"')
print('Content-type: application/pdf')
print('')
sys.stdout.flush()
sys.stdout.buffer.write(fileData)

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

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