Skip to content

Commit

Permalink
Loop add resources .jar
Browse files Browse the repository at this point in the history
  • Loading branch information
jadsonbr committed Jun 12, 2020
1 parent ca4f067 commit 7ec6699
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
60 changes: 60 additions & 0 deletions jasper.jrxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="jasper" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="812bd71a-1818-447c-8f44-ce4ad6b96e43">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="relatorio"/>
<queryString language="SQL">
<![CDATA[SELECT codigo, nome FROM cidade LIMIT 10]]>
</queryString>
<field name="codigo" class="java.lang.Integer">
<property name="com.jaspersoft.studio.field.label" value="codigo"/>
<property name="com.jaspersoft.studio.field.tree.path" value="cidade"/>
</field>
<field name="nome" class="java.lang.String">
<property name="com.jaspersoft.studio.field.label" value="nome"/>
<property name="com.jaspersoft.studio.field.tree.path" value="cidade"/>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="205" y="20" width="153" height="30" uuid="8fe107cd-604b-4225-b1c4-4f464adc801b"/>
<textElement textAlignment="Center">
<font size="20"/>
</textElement>
<text><![CDATA[TEST]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="1b7c4994-ded6-464d-a553-2c02d69ed417">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="49846b9b-de68-4ab1-b9b1-b6b27b672343"/>
</reportElement>
<text><![CDATA[nome]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="550" height="30" uuid="7330f3f0-72d3-47b3-ac65-3880f5f79cd9">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="49846b9b-de68-4ab1-b9b1-b6b27b672343"/>
</reportElement>
<textFieldExpression><![CDATA[$F{nome}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
10 changes: 8 additions & 2 deletions pyreportjasper/jasperpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from requests import Request, Session
import tempfile
import json
import glob


class JasperPy:
Expand Down Expand Up @@ -39,6 +40,7 @@ def __init__(self, jvm_maxmem='512M', jvm_classpath=None):
self.SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
self.LIBS = os.path.join(self.SCRIPT_DIR, 'jasperstarter', 'lib')
self.JDBC_DIR = os.path.join(self.SCRIPT_DIR, 'jasperstarter', 'jdbc')
self.JDBC_FILE = os.path.join(self.SCRIPT_DIR, 'jasperstarter', 'jdbc', 'postgresql.jar')
if not os.path.isdir(self.LIBS):
raise NameError('Unable to find lib in {0}'.format(self.LIBS))
self.CLASSPATH = os.path.join(self.LIBS, 'jasperstarter.jar')
Expand Down Expand Up @@ -198,11 +200,15 @@ def process(self, input_file, output_file=False, format_list=['pdf'],
if 'csv_charset' in db_connection:
config.setCsvCharset(db_connection['csv_charset'])

self.jvApplicationClasspath.add(self.JDBC_DIR)
self.jvApplicationClasspath.add(self.jvFile(self.JDBC_FILE))

if os.path.isfile(resource):
self.jvApplicationClasspath.add(os.path.dirname(resource))
self.jvApplicationClasspath.add(self.jvFile(resource))
# self.jvApplicationClasspath.add(os.path.dirname(resource))
elif os.path.isdir(resource):
self.jvApplicationClasspath.add(resource)
for res_file in glob.glob(os.path.join(resource, '*.jar')):
self.jvApplicationClasspath.add(self.jvFile(res_file))

#
# Run the report. See Report.java for details.
Expand Down
27 changes: 27 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
import os
from pyreportjasper import JasperPy

def advanced_example_using_database():
file_name = 'jasper'
input_file_name = '{}.jrxml'.format(file_name)
input_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), input_file_name)
con = {
'driver': 'postgres',
'username': 'postgres',
'password': 'root',
'host': '127.0.0.1',
'database': 'relatorio',
'port': '5432'
}
jasper = JasperPy()
jasper.process(
input_file_path,
format_list=["pdf"],
parameters={},
db_connection=con,
locale='pt_BR'
)

if __name__ == '__main__':
advanced_example_using_database()

0 comments on commit 7ec6699

Please sign in to comment.