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


amol 16

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):    
    def index(self):
      return dict() 

    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.
      # 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']

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

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

Your session has timed out.

Dismiss (the page may not function properly).