~ubuntu-branches/ubuntu/trusty/pisa/trusty

« back to all changes in this revision

Viewing changes to demo/cherrypy/demo-cherrypy.py

  • Committer: Bazaar Package Importer
  • Author(s): W. Martin Borgert
  • Date: 2010-02-08 22:02:40 UTC
  • Revision ID: james.westby@ubuntu.com-20100208220240-cwsifrpnqeaug5x5
Tags: upstream-3.0.32
ImportĀ upstreamĀ versionĀ 3.0.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/local/bin/python
 
2
# -*- coding: ISO-8859-1 -*-
 
3
#############################################
 
4
## (C)opyright by Dirk Holtwick, 2008      ##
 
5
## All rights reserved                     ##
 
6
#############################################
 
7
 
 
8
import cherrypy as cp
 
9
import sx.pisa3 as pisa
 
10
import cStringIO as StringIO
 
11
 
 
12
try:
 
13
    import kid
 
14
except:
 
15
    kid = None
 
16
 
 
17
class PDFDemo(object):
 
18
 
 
19
    """
 
20
    Simple demo showing a form where you can enter some HTML code.
 
21
    After sending PISA is used to convert HTML to PDF and publish
 
22
    it directly.
 
23
    """
 
24
 
 
25
    @cp.expose
 
26
    def index(self):
 
27
        if kid:
 
28
            return file("demo-cherrypy.html","r").read()
 
29
 
 
30
        return """
 
31
        <html><body>
 
32
            Please enter some HTML code:
 
33
            <form action="download" method="post" enctype="multipart/form-data">
 
34
            <textarea name="data">Hello <strong>World</strong></textarea>
 
35
            <br />
 
36
            <input type="submit" value="Convert HTML to PDF" />
 
37
            </form>
 
38
        </body></html>
 
39
        """
 
40
 
 
41
    @cp.expose
 
42
    def download(self, data):
 
43
 
 
44
        if kid:
 
45
            data = """<?xml version="1.0" encoding="utf-8"?>
 
46
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 
47
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
48
                <html xmlns="http://www.w3.org/1999/xhtml"
 
49
                      xmlns:py="http://purl.org/kid/ns#">
 
50
                  <head>
 
51
                    <title>PDF Demo</title>
 
52
                  </head>
 
53
                  <body>%s</body>
 
54
                </html>""" % data
 
55
            test = kid.Template(source=data)
 
56
            data = test.serialize(output='xhtml')
 
57
 
 
58
        result = StringIO.StringIO()
 
59
        pdf = pisa.CreatePDF(
 
60
            StringIO.StringIO(data),
 
61
            result
 
62
            )
 
63
        if pdf.err:
 
64
            return "We had some errors in HTML"
 
65
        else:
 
66
            cp.response.headers["content-type"] = "application/pdf"
 
67
            return result.getvalue()
 
68
 
 
69
cp.tree.mount(PDFDemo())
 
70
 
 
71
if __name__ == '__main__':
 
72
    import os.path
 
73
    cp.config.update(os.path.join(__file__.replace(".py", ".conf")))
 
74
    cp.server.quickstart()
 
75
    cp.engine.start()