【问题标题】:User downloadable PDFs in Google App Engine returning corrupted PDFsGoogle App Engine 中的用户可下载 PDF 返回损坏的 PDF
【发布时间】:2015-02-05 01:44:42
【问题描述】:

我创建了一个 python 脚本,它使用 reportlab 从提供的用户数据生成 PDF。当我从命令行运行脚本时,PDF 下载正常,一切正常。但是,当我尝试像这样将文件传递给 Google App Engine 时:

...

outputstream = StringIO.StringIO()
PDF = output.write(outputstream)

class PDFHandler(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/pdf'
        self.response.headers['Content-Disposition'] = 'attachment; filename=Myfile.pdf'
        self.response.headers['Content-Transfer-Encoding'] = 'binary'
        self.response.out.write(PDF)

并运行开发服务器,PDF 下载,但是当我尝试打开它时,chrome 说它无法打开文件,Ubuntu 的文档查看器说“无法打开纯/文本文档”,即使当我检查文档的属性,它显示为“应用程序/pdf”并且它具有适当的 .pdf 后缀,当我尝试在 GIMP 图像查看器中打开它时,它显示“文档已损坏”。我将文件传递给 webapp2 处理程序的方式有问题吗?任何帮助将不胜感激!

【问题讨论】:

    标签: python google-app-engine pdf reportlab


    【解决方案1】:

    我通过查看一些旧教程解决了这个问题。在最后一行代码中,我忽略了将 .getvalue() 包含到输出中:

    outputstream = StringIO.StringIO()
    PDF = output.write(outputstream)
    
    class PDFHandler(webapp2.RequestHandler):
        def get(self):
            self.response.headers['Content-Type'] = 'application/pdf'
            self.response.headers['Content-Disposition'] = 'attachment; filename=Myfile.pdf'
            self.response.headers['Content-Transfer-Encoding'] = 'binary'
            self.response.out.write(PDF.getvalue())
    

    【讨论】:

    • 好的,请接受您自己的回答,以便此案正式“结案”,tx。
    • 哇,过去是 30 分钟左右,但后来我已经离开了这么多年,最近才回来,所以我的信息已经过时了!-)
    猜你喜欢
    • 1970-01-01
    • 2012-02-13
    • 2020-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多