Jasper Reports

Reporting on iSeries has come a long way. Traditionally, iSeries developers have to create reports using RLU, and then the RPG report program creates a spool file which the user then prints for his analysis. It’s a normal sight to see users printing bulk print-outs from the spool file. Then the need came for sending the reports via email, and there are lot of tools now to convert spool files to PDFs to send the reports to others. Though these converted reports to PDF will suffice, they do not have a professional look; they may lack a company logo and there is no way to show different colors, charts, internationalization, bar codes, different fonts, etc.

Though you can buy a vendor product to do all those things, why not try Jasper Reports for free? With Jasper, you can achieve all these things and create a professional looking report with any formats you wish (PDF, Excel, rich text format, XML, and CSV). However, to use Jasper, you will have to use java, and integrate Jasper in your java programs. And moreover, you will have to write SQLs to generate the data for your report. Thus, it might not be the best idea to write a complex SQL to generate a report (for example, a purchase order report), in order to replace or imitate a legacy reporting RPG program. In that case, the conversion tools (converting spool to pdf) will be a better option.

So, consider using Jasper for any new reports or for any small-to-complicated reports where not more than 2 or 3 files are needed, or for any files with less data.

Enough talk, soon I will show you how to get started with Jasper and create reports. But before that, a small overview.

Overview

First, you will have to download Jasper Reports from sourceforge.net and
add the jasperreports jar file under the ‘dist’ to your classpath. Then,
go through the docs and the samples. The samples are runnable with ant.

Initially, it’s hard to understand the docs. The idea here is that you need to write an XML based on the jasper syntax. (ends with .jrxml – that’s the hard part). Then you need to compile it with jasper classes resulting in a binary file called .jasper. Then you invoke the filler classes to fill appropriate format of reports like PDF, or Excel or other formats.

For a simple example, we will have to:

  1. load the .jrxml (xml file ) and compile it (use JasperCompileManager.compileReport() methods , it returns a JasperReport Object, use this in the next step
  2. use JasperFillManager.fillReport(JasperReport Object,a Map object, a Connection object ) for filling up (or produce page prints) it returs a JasperPrint Object which you use in the next step.
  3. use JasperExportManager.exportReportToXXX(JasperPrint Object ,output file)

A simple source code will be like this:

JasperPrint jasperPrint = new JasperPrint();
Connection conn = null;
Map inputParameters = new HashMap();
inputParameters.put("sometitle","first report");
conn = your database connection you get from your driver (using jt400)
JasperReport jasperReport = JasperCompileManager.compileReport("input.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport,
inputParameters, conn);
// for a pdf
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFile.pdf);

The above source is simplified to just give the main idea and you can easily build above on this referring the api docs, using iReport to create the xml (.jrxml).

Using iReport to create the xml (.jrxml)

As I said before the difficult part would be creating the xml (*.jrxml) by hand. A typical xml format would be something like this:


<jasperReport name="FirstJasper" columnCount="2" pageWidth="595" pageHeight="842" columnWidth="270" columnSpacing="15" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
<reportFont name="Arial_Normal" isDefault="true" fontName="Arial" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica"pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<reportFont name="Arial_Bold" isDefault="false" fontName="Arial" size="8" isBold="true" isItalic="false" isUnderline="false"isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<reportFont name="Arial_Italic" isDefault="false" fontName="Arial" size="8" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<reportFont name="Comic_Normal" isDefault="false" fontName="Comic Sans MS" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="COMIC.TTF" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
<reportFont name="Comic_Bold" isDefault="false" fontName="Comic Sans MS" size="10" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="COMICBD.TTF" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
<parameter name="ReportTitle" class="java.lang.String"></parameter>
<parameter name="MaxOrderID" class="java.lang.Integer"></parameter>
<parameter name="SummaryImage" class="java.awt.Image"></parameter>
<queryString><![CDATA[SELECT * FROM Orders WHERE OrderID <= $P{MaxOrderID} ORDER BY ShipCountry]]></queryString>

and so on………..

And so, iReport comes to the rescue. Since creating the .jrxml would be mostly a one time job, it would be easy to use iReport. It is a fantastic open source tool in Swing and it will create the stand-alone reports in any format you wish, for quick testing and then embedding into your java application.

Download iReport from here.

It is an executable jar, needs JRE, and loads right away. It is resource intensive, so please make sure that you have more than 512 MB RAM on your machine.

To explain using iReport screen by screen there is an excellent step-by-step tutorial on devx.

iSeries Specific steps

The first thing is to configure the data source. Simply put the jt400.jar inside the iReport Folder/lib directory. Then, while creating a data-source, enter the appropriate as400 JDBC URL and test the connection. If it is not successful, then possibly it is not seeing the
jt400.jar.

Always use the Report Design wizard to create the reports. Note, iReport sometimes creates a line in the wrong place if you are using the Report Wizard; simply adjust the line or delete it.

Other tools apart from iReport

There are two other tools which can create the .jrxml file for you:

  • Jasperassistant
    If you use Eclipse then you can use Jasperassistant. It is a plugin to Eclipse and works the same way as iReport though not so easy as iReport.
  • Jasperedit
    A simple tool with not so much features as iReport and JasperAssistant.

Resources

Read the API docs, the devx tutorail and some good tutorials are available here . Buying the manual for jasper soft and iReport will explain some complex topics like scriptlets, embedding in server side and many more.

Republished, with permission, from http://www.rizwan.in/node/299.

[tags]iSeries, jasperreports, Java[/tags]

16 Replies to “Jasper Reports”

  1. Great work.. Keep going!!!.

    But as you earlier mentioned, we still convert the spool files into to required format and send it to the vendors and customers. And seems our company is satisfied with it..lol

  2. Also, there should be some one to do the R&D work and come out with an efficient solution as you have mentioned. But to do that, we need a programmer analyst which is costlier thing to have.

  3. If you are a business analyst and want to do things yourself (if no other go they turn up to programmers :-)) then you can eclipse birt. It is a eclipse tool and has lots of documentation to install and get going. Here is the link
    http://www.eclipse.org/birt/
    But it still needs a container like tomcat to deploy your generated document formats but works great. Give a look.

  4. It is probably 7 years since I did any coding and I was always surprised that no one ever got round to writing the facilities to generate nice looking reports from AS/iSeries.

    In the 80’s I generated reports from RPGIII using embedding PCL controls in the O specs. They were hellish as it was impossible to print off a listing for debugging so it was all on screen. The worst part was you had to keep count of the pixels in both horizontal and vertical directions but the results were superb.

    By the 90’s usng RPG400, I output in RTF, much easier to do and delivered the print file as .doc file to a PC directory on the network. The only really tricky bit was the eol in the spool file which messed up justified text but I did find a work round for that. The whole thing was writtens as subroutines to output each parameter and that meant that the report itself could be written as mainline with no complicated bits. I am sure that software to create any report as a RTF file should be easily done now with all the options now available on iSeries, most business users like to be able to modify their reports in Word. Adding password protection to the report can help prevent unwanted changes.

    Another trick I did a few years ago was to have Monotype create a Unicode Type1 font with EBCDIC character ID’s instead of Adobe. This gave a beautiful result as it allowed matching fonts to be used in AFP on both iSeries and Mvs with a matching TTF font for Windows. TTF has a higher resoution(?) than Type 1 so by building in Type 1 and creating the TTF from that gave identical glyph spacing. The font was readily embedded in PDF’s too.

    Hope this helps someone.

  5. Creating a jasper report is not difficult but writing a jrxml for Report is really very difficult.

  6. Hello,

    I need to create Crosstabs using JRDataSource in Jasper Report.Can you please tell me any tutorial for this.

  7. Hi,
    can any one please tell me how to generate the format of jasper report dynamically using java?? Is that feasible in real time application to generate the Jrxml file dynamically depending on the requirement??

  8. I have to change dynamically the number of columns in report.The problem is like the following.
    There is a list box in jsp page,that contains the column names.User selects columns in list box that are printed in report.Therefore,i must create report dynamically.How can i organize this report using jasperreports?

    i need to use dynamics column on a report with a java datasource.
    is it possible for jasper to use a list of elements to create dynamics columns ?

  9. Hi,
    You can use ‘ Print When Expression ‘ for this.
    Apply the changes to text based on condition.
    You will get the correct result

  10. I am using jrxml files to produce a splf on the as400.
    I am using the same jrxml to produce a pdf for display.

    I am wondering if anyone knows if you can conditionally choose the formatting depending on whether it is outputted as a spool or pdf within the jrxml itself?

    the splf treats x position 124 as the same as 120.
    the pdf treats x position 124 as the real position 124… leaving the pdf a bit off from the splf

    If I can use conditional presentation, I would use printWhenExpression and place both in the jrxml… allowing it to print one location for spool and the other for pdf… I am not sure if the value of splf is naturally sent to the jrxml in a global variable of sorts… if this is the wrong place to ask, please forgive me.

    1. Edward,
      First off, the styles are evaluated at report fill time, which is just prior to exporting the report. To get around that, you would have to add smarts to pass in what you intend to export the report as. The easiest way would be to add a parameter and pass that in to the jasper engine.
      For a lot of the dynamic formatting, you could use conditional styles rather than printWhens:
      http://community.jaspersoft.com/wiki/how-apply-conditional-formatting-your-text-fields

Leave a Reply

Your email address will not be published. Required fields are marked *