Welcome, guest | Sign In | My Account | Store | Cart

short python jasperreport integration example

Python, 88 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
JasperReports wrapper:
"""

import jpype

JVMPath= jpype.getDefaultJVMPath()
pathToJRLibs= "c:\\Program Files\\jasperserver-3.0\\apache-tomcat\\webapps\\jasperserver\\WEB-INF\\lib\\"

JasperCompileManager= JasperFillManager= JasperExportManager= None
JRPdfExporter= JRRtfExporter= JRHtmlExporter= JRXlsExporter= None
JRExporterParameter= JRPdfExporterParameter= JRXlsExporterParameter= None
JRLoader= JRParameter= JRXmlDataSource= JRXPathQueryExecuterFactory= JRXmlUtils= None
def startJVM():
  global JasperCompileManager, JasperFillManager, JasperExportManager, \
    JRPdfExporter, JRRtfExporter, JRHtmlExporter, JRXlsExporter, \
    JRExporterParameter, JRPdfExporterParameter, JRrtfExporterParameter, JRXlsExporterParameter, \
    JRLoader, JRParameter,JRXmlDataSource, JRXPathQueryExecuterFactory, JRXmlUtils

  _jvmArgs = ["-ea"] # enable assertions
  _jvmArgs.append("-Djava.ext.dirs=%s" % pathToJRLibs)
  jpype.startJVM(JVMPath, *_jvmArgs)
  #
  JasperCompileManager= jpype.JClass("net.sf.jasperreports.engine.JasperCompileManager")
  JasperFillManager= jpype.JClass("net.sf.jasperreports.engine.JasperFillManager")
  JasperExportManager= jpype.JClass("net.sf.jasperreports.engine.JasperExportManager")
  
  JRPdfExporter= jpype.JClass("net.sf.jasperreports.engine.export.JRPdfExporter")
  JRRtfExporter= jpype.JClass("net.sf.jasperreports.engine.export.JRRtfExporter")
  JRHtmlExporter= jpype.JClass("net.sf.jasperreports.engine.export.JRHtmlExporter")
  JRXlsExporter= jpype.JClass("net.sf.jasperreports.engine.export.JRXlsExporter")
  #
  JRParameter= jpype.JClass("net.sf.jasperreports.engine.JRParameter")
  JRExporterParameter= jpype.JClass("net.sf.jasperreports.engine.JRExporterParameter")
  JRPdfExporterParameter= jpype.JClass("net.sf.jasperreports.engine.export.JRPdfExporterParameter")
  JRXlsExporterParameter= jpype.JClass("net.sf.jasperreports.engine.export.JRXlsExporterParameter")
  
  JRLoader= jpype.JClass("net.sf.jasperreports.engine.util.JRLoader")
  JRXmlDataSource=jpype.JClass("net.sf.jasperreports.engine.data.JRXmlDataSource")
  JRXPathQueryExecuterFactory=jpype.JClass("net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory")
  JRXmlUtils=jpype.JClass("net.sf.jasperreports.engine.util.JRXmlUtils")
#:
def shutdownJVM():
  jpype.shutdownJVM()
#
def JasperReports(jrxml,connection,parameters, report):
  def JDBCconnection(conpars):
    jdbcDriver= jpype.JClass(conpars[0])
    dm= jpype.JClass("java.sql.DriverManager")
    conn= dm.getConnection(conpars[1], conpars[2], conpars[3])
    conn.setAutoCommit(jpype.JBoolean(False))
    return conn
  #}def JDBCconnection
  s1= '.'.join(jrxml.split('.')[:-1])
  output= parameters["output"]
  jasper= "%s.jasper" % s1
  jrprint= "%s.jrprint" % s1
  
  #
  JasperCompileManager.compileReportToFile(jrxml,jasper)
  #
  jdbc= JDBCconnection(connection)
  jasperParameters= jpype.JClass("java.util.HashMap")()
  if output in ['xls']:#,'html']:
    jasperParameters.put(JRParameter.IS_IGNORE_PAGINATION, True)
  for item in parameters.keys():
    jasperParameters.put(item, parameters[item].decode("utf-8"))  
  JasperFillManager.fillReportToFile(jasper, jrprint, jasperParameters, jdbc)
  #
  if output=="pdf":
    exporter = JRPdfExporter()
    exporter.setParameter(JRPdfExporterParameter.IS_CREATING_BATCH_MODE_BOOKMARKS, True)
  elif output=="rtf":
    exporter= JRRtfExporter()
    #
  elif output=="xls":
    exporter= JRXlsExporter()
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, True);
  elif output=="html":
    exporter= JRHtmlExporter()
  else:
    return
  exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, jrprint)
  exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, report)
  exporter.exportReport()
# ------------------------------------------------------------------------------------------
Created by Martchenko on Fri, 27 Nov 2009 (MIT)
Python recipes (4591)
Martchenko's recipes (3)

Required Modules

Other Information and Tasks