pyJasper
pyJasper is a way for generating PDF output in Python Programms using the JasperReports Library. Some background on the machinery involved can be found in this blog posting.
Usage
Servlet Interface
The servlet keeps no state at all. You have to supply it with an XML datasource, an XPath expression for that datasource and the JRXML report design. You get back the generated PDF or an plain text error message. The respective data has to be submitted via the form variables 'xpath', 'design' and 'xmldata'.
To try it out you can use curl. E.g. do to pyjasper/backend and start the jetty servlet container (sh pyJasper-httpd.sh). Then use curl in another window to request rendering of a PDF.
curl -X POST --form xpath=//lieferscheine/lieferschein
--form design=@reports/Lieferschein.jrxml
--form xmldata=@sample-xml/Lieferschein.xml
http://localhost:8080/pyJasper/jasper.py > test.pdf
test.pdf should now contain a rendered PDF document.
Python interface
You are expected to subclass pyjasper.JasperGenerator and call it's generate_pdf() function. Usually you only have to overwrite the __init__() and generate_xml(self, ...) functions and use the the Python ElementTree API to generate an xml-tree. E.g.
class MyPDFGenerator(JasperGenerator): """Jasper-Generator for Greetingcards""" def __init__(self): super(MovementGenerator, self).__init__() self.reportname = 'reports/Greeting.jrxml' self.xpath = '/greetings/greeting' self.root = ET.Element('gretings') def generate_xml(self, tobegreeted): """Generates the XML File used by Jasperreports""" ET.SubElement(self.root, 'generator').text = __revision__ for name in tobegreeted: xml_greeting = ET.SubElement(self.root, 'greeting') ET.SubElement(xml_greeting, "greeting_to").text = unicode(name) ET.SubElement(xml_greeting, "greeting_from").text = u"Max" return xmlroot
Now you can use MyPDFGenerator like this:
generator = MyPDFGenerator() pdf = generator.generate(['nik', 'tobias', 'chris', 'daniel']) open('/tmp/greetingcard.pdf', 'w').write(pdf)
The Python client finds the URL of the Jasper Servlet by checking the PYJASPER_SERVLET_URL environment variable. It this Variable is not set, a default value of http://localhost:8080/pyJasper/jasper.py is used.
Installation
To install the Python client interface just execute python setup.py install as administrator. This should install the requred dependency (httplib2) automatically. For the Server part there exist no automatic setup script. Just copy pyjasper/backend/ to a suitable location and start pyJasper-httpd.sh I use Dan Bernsteins supervise tool for running the Jetty server.
Downloads
You can dowload pyJasper at http://cybernetics.hudora.biz/dist/pyJasper/.
History
- 0.2.1 public release (Summer 2008)
- 0.2 release based on Jetty/Servlets (late 2007)
- 0.1.1 public release on hodted-projects.com
- 0.1 release based on long running Java Process (late 2006)
- 0.0 one Java process per document (2006)
Links
- How to Integrate JasperReports with Ruby on Rails
- jasapp a VMWare Virtuall Appliance providing a JasperReports Interface.
