0

我关于 StackOverflow 的第一个问题。. .

在使用 python 的 Google App Engine 应用程序中,我试图在页面上显示一个与 html 内联的小型 pdf 图像。

我有一个小班,这样写:

class modelReport(db.Model):
    Applicant = db.StringProperty()
    Reportblob = db.BlobProperty()

一个小表单用于上传图像并将图像提交给以下处理程序:

class UploadResults(webapp.RequestHandler):
    def post(self):
        m = modelReport()
        m.Applicant = self.request.get("txtApplicantName")
        Reportblob = self.request.get("file")
        m.Reportblob = db.Blob(Reportblob)
        m.put()

我正在使用以下代码来显示申请人的图像和姓名:

class RetrieveResults(webapp.RequestHandler):
    def get(self):
        self.response.out.write('''
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="em">
        <head>
        <meta http-equiv="Content-Type" content="application/pdf" />
        <title>Results Page</title>
        </head>
        <body>
        ''')
        reports = db.GqlQuery('SELECT * FROM modelReport')
        for report in reports:
            self.response.out.write('</p><b>%s</b> report is:' % report.Applicant)
            self.response.out.write("<div><img src='img?img_id=%s'></img>" % report.key())
        self.response.out.write('''
        </body></html>
        ''')

使用开发服务器 Datastore Viewer 时,我可以看到新的“modelReport”实体,其中列出了 Key、Write Ops、ID、Key Name、Applicant 和 Reportblob。

问题是输出列出了申请人,然后显示一个带有“?”的小蓝色框。中间好像找不到图片文件。. . 并且,开发服务器日志控制台显示 404 错误:

INFO..."GET /retrieve_results/all_results HTTP/1.1" 200 -
INFO..."GET /retrieve_results/img?img_id=ag...Aww HTTP/1.1" 404 -
INFO..."GET /retrieve_results/img?img_id=ag...BQw HTTP/1.1" 404 -
INFO..."GET /retrieve_results/img?img_id=ag...Bgw HTTP/1.1" 404 -

我想了一会儿,我可能使用了错误的“内容类型”标头,但是使用 Apache Web 服务器的类似代码可以很好地显示文本和图像。

似乎我可能正在制作空的 blobstore 属性“Reportblob”,但我不知道如何验证或调试。

任何或所有帮助修复 GAE 代码将不胜感激。

4

1 回答 1

2

先说几个错误:

  1. 您的 HTML 页面不是 PDF 文档,因此您不能将其 Content-type 声明为 pdf:

    <meta http-equiv="Content-Type" content="application/pdf" />
    

    相反,它应该是

    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    
  2. PDF 文档不是图像,您不能只指向srcPDF 文档 URL 并期望它显示:

    <div><img src='img?img_id=%s'></img>
    
  3. 只是检查一下:我假设您有/img?img_id=<..>处理程序来提供来自 blobstore 的图像

你需要做什么:

  1. 确保正确地提供 blob,如上面第 3 点中的链接所述。

  2. 查看有关如何将 PDF 文档正确嵌入 HTML 页面的所有选项:在 HTML 中嵌入 PDF 的推荐方法?(最简单的是PDFObject

于 2012-08-08T10:03:25.337 回答