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 :