Mark
Mark

Reputation: 1

How to display pdf blob from Google App Engine using Python

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.

Upvotes: 0

Views: 1344

Answers (1)

Peter Knego
Peter Knego

Reputation: 80330

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)

Upvotes: 2

Related Questions