1. Create folder named pdf into App Engine project folder.


  1. Copy xhtml2pdf, html5lib, reportlab folder to pdf folder.


  1. Copy six.py file to pdf folder.


  1. Create PDFHandler.py that will create the pdf file and make it to download.


Explanation of PDFHandler.py

1) Import webapp2


import webapp2
2) Import vendor to add installed lib to work


     from google.appengine.ext import vendor
     vendor.add(‘pdf’)
3) Import files that are required to create pdf file
     
     from xhtml2pdf import pisa
     from cStringIO import StringIO


4) Class that will handle the action
    
      class DownloadPDF:
         def post(self):
             try:
                content = StringIO(“<table><tr><th>Name</th></tr><tr><td>Sunil</td></tr></table>”)
                output = StringIO()
                pdf = pisa.CreatePDF(content, output, encoding = ’utf-8’)
    pdf_data = pdf.dest.getvalue()
 except ValueError:
    logging.info(‘Error’)

5) Set the response header to make this downloadable as pdf


        self.response.headers[‘Content-Type‘] = ‘application/pdf’
        self.response.headers[‘Content-Disposition’] = ‘attachment;filename=Any_Name’


6) Write pdf data as response


       self.response.write(pdf_data)


7) Define app variable to make URL working


       app = webapp2.WSGIApplication([(‘/downloadpdf‘, DownloadPDF)], debug=True)


8) Entry in app.yaml to run PDFHandler.py


     – url: /reports/downloadpdf
        script: PDFHandler.app


Clubbing it together :

import webapp2
     from google.appengine.ext import vendor
     vendor.add(‘pdf’)
     from xhtml2pdf import pisa
     from cStringIO import StringIO
      
      class DownloadPDF:
         def post(self):
             try:
                content = StringIO(“<table><tr><th>Name</th></tr><tr><td>Sunil</td></tr></table>”)
                output = StringIO()
                pdf = pisa.CreatePDF(content, output, encoding = ’utf-8’)
    pdf_data = pdf.dest.getvalue()
              
                self.response.headers[‘Content-Type‘] = ‘application/pdf’
                self.response.headers[‘Content-Disposition’] = ‘attachment;filename=Any_Name’
                self.response.write(pdf_data)
 except ValueError:
    logging.info(‘Error’)


        app = webapp2.WSGIApplication([(‘/downloadpdf‘, DownloadPDF)], debug=True)


Download from github :



Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Google App Engine

Channel API (Server to client communication)

Original web design allowed only one-way client-server communication. But there are some cases in which server need to communicate with browser. One way is to communicate from client to server after a fixed interval that Read more…

Google App Engine

Appcfg : Download source code and logs from GAE

Appcfg :- Google SDK Command (App Engine) Download Source Code :- 1) Open Notepad and copy the following code :appcfg.py download_app -A <your_app_id> -V <your_app_version> “<output-dir>” 2) Change <your_app_id>, <your_app_version> and <output_dir> fields accordingly. You ” (quotes) for Read more…

Google App Engine

Appcfg : One click deployment to Google App Engine

Google App Engine is IaaS (Infrastructure as a Service) provided by Google to create and deploy web applications. This service will let you select the environment in which you want to deploy your application like Read more…