svv/wsgi.py
changeset 2 e8b3f3884233
parent 1 06451697083a
child 3 44122295656a
--- a/svv/wsgi.py	Sun Dec 19 18:39:54 2010 +0200
+++ b/svv/wsgi.py	Mon Dec 20 01:02:56 2010 +0200
@@ -92,6 +92,36 @@
 
         raise NotImplementedError()
 
+class Document (AppHandler) :
+    """
+        PDF generation/export
+    """
+
+    def respond (self, url_values) :
+        from reportlab import platypus as rlpp
+        from reportlab.lib.units import inch
+        from reportlab.lib.styles import getSampleStyleSheet
+
+        from cStringIO import StringIO
+
+        buf = StringIO()
+        styles = getSampleStyleSheet()
+        style = styles['Normal']
+
+        title = url_values.get('name', "Hello World")
+
+        tpl = rlpp.PageTemplate('test', [ rlpp.Frame(inch, inch, 6 * inch, 9 * inch) ])
+        doc = rlpp.BaseDocTemplate(buf, pageTemplates=[tpl], title=title, author="Test Author", showBoundary=True)
+
+        elements = [
+                rlpp.Paragraph("Hello, World!\n" + title, style)
+        ]
+        
+        # render elements to buf as PDF code
+        doc.build(elements)
+
+        return Response(buf.getvalue(), mimetype='application/pdf')
+
 class Index (PageHandler) :
     DATA = (
         (100, "Top A", []),
@@ -147,6 +177,7 @@
     # map to AppHandler-endpoint
     URLS = Map((
         Rule('/', endpoint=Index),
+        Rule('/pdf/<string:name>.pdf', endpoint=Document),
     ))
 
     def __init__ (self) :