0

My first question on StackOverflow . . .

In a Google App Engine application using python, I'm trying to display a small pdf image in-line with html on a page.

I have a small class, written like this:

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

A small form is used to Upload the image and Submit the image to the following handler:

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()

I'm using the following code to display the image and the name of the Applicant:

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>
        ''')

When using the development server Datastore Viewer, I can see new 'modelReport' entities that list Key, Write Ops, ID, Key Name, Applicant, and Reportblob.

Issue is output lists the Applicant and then displays a small blue box with a "?" in the middle like it can't find the image file . . . And, the development server log console shows 404 errors:

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 -

I thought for awhile that I may be useing the wrong 'Content Type' header, but similar code using Apache web server displays the text and image just fine.

Seems like I may be making empty blobstore attributes "Reportblob", but I don't know how to verify or debug.

Any or all help fixing GAE code would be greatly appreciated.

Mark
  • 1
  • 2

1 Answers1

2

First a few mistake:

  1. Your HTML page is not a PDF document, so you can not declare it's Content-type as pdf:

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

    instead it should be something like

    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    
  2. PDF documents are not images, you can not just point a src to PDF document Url and expect it to be shown:

    <div><img src='img?img_id=%s'></img>
    
  3. Just checking: I assume you have /img?img_id=<..> handler to serve images from blobstore?

What you need to do:

  1. Make sure you serve blobs properly, as described in the link above in point 3.

  2. See all options on how to embed PDF document properly into your HTML page: Recommended way to embed PDF in HTML? (the most simple would be PDFObject)

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Regarding point #1, I have tried both ways; with "application/pdf" and with "text/html". Same result. Regarding point #2, If I put the following code in html page and load that page, it displays the pdf just fine: "" Regarding point #3, I'm not serving from the blobstore, I'm serving a blob from the datastore. Link to "server images from blobstore" I'm struggling to understand. Thanks for your comments and efforts, but I'm still trying to get this to work. How to retrieve & embed a graphic that is stored as a blob in datastore and show it? Thanks. – Mark Aug 13 '12 at 23:35
  • You can only use Datastore in your code. You can not create a direct link that points to your Datastore contents (your pdf blob). You have to make a handler, that gets the blob from database and serves it (as content type "application/pdf"). Then you can use link to this handler in Htmal. – Peter Knego Aug 14 '12 at 06:16
  • Hi Peter. So how do I make a handler that get the blob from the database? I thought that is what the RetrieveResults class was. The query retrieves the blob.key. And then I was under the impression that the graphic itself would be substituted for the blob.key during the return request to the client browser. I just don't know how to debug this, or break it down to figure things out. I'm very lost.... – Mark Aug 14 '12 at 23:13