Jasper « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ Jasper ’

MalformedByteSequenceException with grails jasper plugin 1.1.6

Posted by on December 3rd, 2010

Hi All,

I was facing a lot of problems with creating pdf reports using jasper plugin 1.1.6 in grails, Though I had earlier worked with 0.9.5 and 0.9.7 versions of the plugin with a lot of ease but some how 1.1.6 version of plugin was not generating reports  it always ended up throwing MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
 at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
 at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
 at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
 at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1619)
 at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1657)
 at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:193)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
 at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
 at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
 at org.apache.commons.digester.Digester.parse(Digester.java:1647)
 at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:241)
 at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:228)
 at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:216)
 at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:199)
 at net.sf.jasperreports.engine.JasperCompileManager$compileReport.call(Unknown Source)
 at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
 at java.lang.Thread.run(Thread.java:662)

I tried various things like changing the header of the jrxml file, adding new attributes in jasperReport tag and changing encoding type but to no Avail. Then I tried generating report without the .jasper files and deleted all jasper files except for sub-reports (sub-reports need to be in a pre-compiled state for the plugin to work)  and thing started working. For some strange reason the plugin was throwing exception when jasper report was available but if the report was compiled on the fly the plugin had no trouble with it. I am not sure what may be the reason may be I-report or something, but I got a move on. Hope You will too.

With Regards

Sachin Anand

sachin[at]intelligrape[dot].com

Posted in Java tools

Passing parameters to sub-reports in Jasper

Posted by on September 15th, 2010

In one of our recent project, we made quite a good use of Grails Jasper plugin (v-0.9.7) to generate reports in the form of PDFs and Excel sheets. The reports contain sub-reports, which in many cases, contain sub-reports again. The way to pass parameters to sub-reports at the deeper level is to use the following tag in the .jrxml file: <subreportParameter>

In the Grails Jasper plugin, the Jasper controller searches for the “SUBREPORT_DIR” in the params when the request is forwarded to it. If it does not find any key named “SUBREPORT_DIR” in the params map, it initializes the variable SUBREPORT_DIR with the value of “jasperFilePath” i.e. the location where the Jasper’s .jasper and .jrxml files can be found. Now suppose you want to give seperate file location for your sub-reports, you can easily do this using the following code in the .jrxml file inside the tag:


   ...
 
        
 
  ...

Note the value of “![CDATA[$P{SUBREPORT_DIR}]]” parameter can be passed as a parameter from the controller to the master report but afterwards you can make use of the tag to pass on the information to the subsequent sub-reports to any level.

For more information on the jasper .jrxml tags, please visit the following link here: http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/schema.reference.html

Hope this helps !!

- Abhishek Tejpaul
abhishek@intelligrape.com
[IntelliGrape Software Pvt. Ltd]

Posted in Java tools

Passing Optional Parameters in jasper reports

Posted by on September 13th, 2010

Hi Guys,

In one of my projects, While working with jasper reports, one of the requirement I faced was of passing optional parameter to the sql query in jasper  report. For eg. I had to write a query to find all new customers which were added between two given dates and if an optional status value of the customer is passed the report should should be further refined to include only those customer which were added and had the same status value as passed, If the status is not provided it should be ignored in SQL query in jasper report. So, in this case the status was an optional parameter.

The solution I found was pretty elegant. On searching over the net, I found that parameters with an ! (exclamation) mark are compiled before the query is executed. That is,

$P!{status}

will be compiled before the query is executed. So, I passed the status from my code in to the jasper report. Now, to take care of the condition that it could be optional, I added a new parameter inside the jasper report, say “status_value”. such that, status_value had a default value expression (DFE) :

 
($P{status}=="" ? " and customer.status=' "+ $P{status} + " ' " )

Now in the SQL query in jasper I wrote

SELECT * FROM customer WHERE customer.start_date=$P{startDate} AND customer.end_date=$P{endDate} 
$P!{status_value}

what it does is that status_value is compiled before the execution of query. So the condition in the DFE of status_value checks whether we a receive a status from outside or not, If we do the condition will be appended in the query. Other wise it won’t be appended and we are done. :) .

With Regards

Sachin Anand
sachin@intelligrape.com

Posted in Java tools

Compiling sub-reports in Jasper reports

Posted by on March 12th, 2010

In my project we needed to generate pdf reports. The tool we used to design reports is IReport 2.0.5 & the plugin used is grails jasper plugin.The plugin works fine with simple reports but fails when it come to complex report which uses subreport.The problem is that the plugin compiles .jrxml file to .jasper file upto 1 level.It do not compiles subreport.

When we compile.jrxml files using iReport, it takes the hardcoded path of the local machine & hence throws an exception in other environment.
So to overcome this problem i used the following script by putting it in CompileReports.groovy file under scripts folder.

  import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
grailsHome = Ant.project.properties."environment.GRAILS_HOME"
includeTargets &lt;&lt; new File("${grailsHome}/scripts/Init.groovy")
target('default': "Compile all Jasper Reports") {
    compileAll()
}
target(compileAll: "Compile all *.jrxml files as *.jasper") {
    depends(classpath)
    def sourceDir = (config.'jasper.dir.reports'?:'reports')
    Ant.echo "sourceDir = ${sourceDir}"
    Ant.taskdef(classpathref:"grails.classpath",name: "jrc", classname: "net.sf.jasperreports.ant.JRAntCompileTask")
    mkdir(dir:"~/.grails/.jasper")
    jrc(srcdir:"${basedir}/web-app/${sourceDir}", tempdir: "~/.grails/.jasper") {include(name:"**/*.jrxml")
    }
}

To compile reports use the command:

grails compile-reports

Helpful links:-

http://n4.nabble.com/Jasper-Grails-Plugin-Best-place-to-store-Files-td1373864.html

Hope it helps.

Vishal Sahu
vishal@intelligrape.com

http://www.intelligrape.com

Posted in Grails, Java tools

Error Compiling .jrxml file with iReport 2.0.5

Posted by on February 25th, 2010

Recently I encountered an error , while working  on iReports ,

iReport it was not able to compile .jrxml file to generate .jasper file , whilst  it used to show  compile time.

Error :

Compiling to file... ./<filename>.jasper -> /usr/share/iReport/bin/<filename>.java
Errors compiling ./<filename>.jasper.
net.sf.jasperreports.engine.JRException: Error saving expressions class file : /usr/share/iReport/bin/<filename>_1267071292035_83340.java
Compilation running time: 80

Basically I found two way out this situation

1) Changing permission of usr/share/iReport/bin/ directory .

This can be done with CHMOD command
sudo chmod 777 usr/share/iReport/bin

or

2)  change the directory where reports are compiled.

check "option -> setting -> compiler --> Use Report Directory to Compile"

________________________
Regards
Hitesh Bhatia

http://www.IntelliGrape.com

________________________

Tags: ,
Posted in Java tools

My experience of using jasper reports with grails application

Posted by on February 25th, 2010

In my project we needed to generate certain pdf reports. Then while exploring the options available, I encountered a PDF plugin, which renders gsp page and returns the PDF report. It looked good for the simple reports but may not be a good idea if the report are complex. Then with the help of one of my collegue, we decided to rather generate jasper reports. And as expected, there exists a plugin called ‘jasper’ to integrate jasper reports with a grails application, which made my job really easy.

Things looked good, now we were only left with an IDE to rapidly generate jasper reports and then we finally found a tool called IReport. We could make version 2.0.5 working flawlessly with the jasper plugin.

As our reports became more complex and our understanding about jasper reports grew, even things which looked big like required page breaks on certain conditions etc. became really easy to handle with the IReport tool. We could generate a complex jasper report in 3 days time which includes the time taken to get accustomed to iReport and jasper report and its integration. I will soon write more about jasper reports and few changes we made to the jasper plugin to suit our requirements like directly sending emails with jasper report as an attachment…

Cheers!
~~Amit Jain~~
amit@intelligrape.com
IntelliGrape Softwares

http://intelligrape.com

Posted in Grails, Java tools