Speed up development with full-stack environments for every branch.

Learn More

Create PDF from TurboGears [Python]

Forked from Parse JSON Data from Request in TurboGears.

70 Runs 167 Views 54 Copies
Saved

Saved

amol 16

amol
published 3 years ago

from wsgiref.simple_server import make_server
from tg import expose, TGController, AppConfig, response
from reportlab.pdfgen import canvas
from cStringIO import StringIO

class RootController(TGController):    
    @expose('index.html')
    def index(self):
      return dict() 

    @expose(content_type='application/pdf')
    def download_pdf(self, **params):
      response.headers['Content-Disposition'] = 'attachment; filename=hello.pdf'

      temp = StringIO()

      # Create the PDF object, using the StringIO object as its "file."
      p = canvas.Canvas(temp)
  
      # Draw things on the PDF. Here's where the PDF generation happens.
      # See the ReportLab documentation for the full list of functionality.
      p.drawString(100, 300, "Hello world.")
  
      # Close the PDF object cleanly.
      p.showPage()
      p.save()
  
      # Get the value of the StringIO buffer and return it as response
      return temp.getvalue()


config = AppConfig(minimal=True, root_controller=RootController())
config.renderers = ['genshi']


DEPLOY_OPTIONS = {}
httpd = make_server('', 80, config.make_wsgi_app(**DEPLOY_OPTIONS))

print "Serving on port 80..."
httpd.serve_forever()
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).