Tuesday, July 15, 2008

Solution/ Workaround for Detail Band Not allowing Vertical Overflow

After a couple of hours of experimentation I discovered the easiest way to achieve detail band vertical overflow.

Technically speaking the detail band overflow is not achievable. But By Creating a Dummy Group and making the group repeat for all the records and placing it below all other groups you can get the desired result. Here’s the steps you need to take.

  1. Create a Dummy Group
  2. Place it after all other groups
  3. Put a unique key as the “Group Expression” (This way for each record will create a new group)
  4. Put your display fields in the group header and remove the details section

Sunday, July 13, 2008

Deatails Section Not allowing Vertical Overflow

Recently I noticed something very interesting about iReport 2.0.0

The “details” section does not allow vertical overflowing. Vertical overflowing is that adding extra lines below if ur field has more characters. But for some reason iReport does not allow it.

If you want to print two lines in ur Details section u have to leave enough space for those lines to be printed.

This is a major braw back in iReport as you would ultimately waste space for records that does not overflow. Hope iReport gives us this functionality in the near future.

Friday, May 23, 2008

Report Empty (Blank page is displayed) When No Data is present/retrieved: Solution


A Common Problem arising in Jasper Reports is when there’s no data retrieved A blank (white) page is displayed. This can be corrected in two ways

The Easiest way is of course to correct it through iReport. Find out how to do it by reading this.

There’s another not-so-subtle way to do it. That is setting the option through Java Code.

Whatever the method we use to display our report, we would have to construct the JasperReport objet. Simply set the option like this:

JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperTemplateURL);

jasperReport.setWhenNoDataType(jasperReport.WHEN_NO_DATA_TYPE_ALL_SECTIONS_NO_DETAIL);

The Method setWhenNoDataType can be set three values. All Three are Constants in the JasperReport Object.

  • jasperReport.WHEN_NO_DATA_TYPE_BLANK_PAGE
  • jasperReport.WHEN_NO_DATA_TYPE_NO_PAGES
  • jasperReport .WHEN_NO_DATA_TYPE_ALL_SECTIONS_NO_DETAIL

We can use them to set it to any of the options we want.

Friday, May 9, 2008

How to execute a Jasper Report through a web app as a PDF and HTML


The most reliable and efficient way to execute a report through a web app (for this example I’ll be using servlets) is to have the report pre-compiled and ready. You can place ur SQL Qurrey inside the report itself or pass a java ResultSet object when creating a report.

First you need to convert your compiled .jasper file to a java object. Jasper API provides the class called JasperReport for this purpose. We also need to know the templateURL (The physical location of the jasper file on the server)

import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperReport;

JasperReport jasperReport = (JasperReport) JRLoader.loadObject (templateURL);

Then Create the JasperPrint Object using the parameters and the resultset.

jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRResultSetDataSource(resultSet));

resultSet = the SQL result set containing your data.

Parameters = the HashMap containing your parameters.

Then Load a ByteStream and “Flush” The report

OutputStream outputStream = servletResponse.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport();
byteStream = baos.toByteArray();
outputStream.write(byteStream);
outputStream.flush();
outputStream.close();

You can call this code and flush the contents to a new window or put it in the same screen. For HTML Export you can use JRHtmlExporter instead of JRPdfExporter.

Friday, May 2, 2008

How to Execute A Report using iReport

Assuming that you have your Data source ready (ie. A Query/Hibernate) It’s a simple matter to execute the report.

You can execute a Report either with data or without data. If you are going to view without data you need to set your “When No Data Type” to “All section No detail” as demonstrated here.

You can execute the report by using the Buildà Execute (Empty Data source) OR Buildà Execute (With Active Connection) in the Main Menu under Build. OR you can simply click the execute button which in the toolbar. (Which will be place on the right hand side by default).