Tuesday, 29 April 2008

Outputting pdfs with google app engine

It's child easy, but not googleable yet. I used reportlab - a handy, pure-python pdf library. Here are the steps:

  1. Checkout reportlab into your app directory
    svn co http://www.reportlab.co.uk/svn/public/reportlab/trunk \
  2. Write some pdf-generation code, like
    import wsgiref.handlers

    from google.appengine.ext import webapp

    from reportlab.pdfgen import canvas

    class MainPage(webapp.RequestHandler):

    def get(self):
    self.response.headers['Content-Type'] = 'application/pdf'
    p = canvas.Canvas(self.response.out)
    p.drawString(100, 750, "Hey, it's easy!.")


    def main():
    application = webapp.WSGIApplication([('/', MainPage)], debug=True)

    if __name__ == "__main__":
    The key is the canvas instantiation. The constructor takes a file-like object and writes into it. You might have noticed that the code is heavily inspired by this tutorial. The only difference is the creation of canvas.
  3. That's it. See? I said it was easy. You can run the dev_appserver or upload it to Google in order to see the results.


Tartley said...

Ha! Brilliant! :-)

Jesse said...

Thank you. This was exactly what I was looking for.

I was not able to download ReportLab from their svn site. I could download the ReportLab Toolkit in zip form for Windows. Initially, I download the .tgz version. But it includes compiled (pyc) files that didn't seem to work on Windows. I switched to the zip distribution and it worked; it generated the sample PDF.

Francisco said...
This comment has been removed by the author.
Francisco said...


svn co https://www.reportlab.co.uk/svn/public/reportlab/trunk

Note in Aug 2008: due to recurring problems with our ISP which we have been able to resolve, port 80 into this server is no not working; please use https as indicated above.

RJ said...

Its not working for me...i am getting the error no such module found...i am using django, in addition i created a symbolic link of reportlab in my application directory...help greatly appreciated...

Kumaresan said...

Thanks Just what I was looking for.

Fraser Rackham said...

Thanks, this helped a lot!