A JasperReports Liferay Portlet (1)

Lately I´ve been working with JasperReports. I´ve included it in a liferay portal so that the user will be able to include reports about all the groups he has on his webserver. When you´re working with enterprise open source software like liferay, you will see that a lot of the software is used together: Liferay with JSF with Activiti Workflows, maybe a connection to Talend, JasperReports as the reporting server and Alfresco as a document server. That´s why I´ve decided to add my JasperReports experience to this blog and not create a new one.

So this will be the first post in a series on how to use JasperReports. But beware: All posts will still have a strong liferay focus. I´m not using the JasperReports Server and I am not building a stand-alone reporting solution.

What is Jasper Reports ?

JasperReports is a reporting solution that connects to almost any dataSource out there and delivers good-looking reports in almost any format. My goal will be to get data out of liferay services and present them as a PDF document for the users to download.

To create reports in your portal you will need the following:

1) The JasperReports Library (Link)

This is a set of JARs that contain JasperReports. You can include them in your portlet by copying them into your WEB-INF/lib folder for now. Basically those JARs are all you need if you´re keen to write your reports in XML.

2) The JasperReports iReport Designer (Link)

That´s a "drag and drop" GUI that creates your diagrams. It´s free and you should use it if you want to save some time. Please download and install it.

Out first goal will be to create a Report with the iReport Designer, put it into our portlet, equip the portlet with the JARs we need and then download the report in liferay. Open iReport designer and create a new blank report. This will be very simple "hello world" report that we will just use for demonstration purposes. Add a "Static Text" Element to the header section and save the document. Click on "Compile report" to produce a compile jasper file.

Using a jasper file instead of a jrxml file has the advantage of not having to compile it when we run our code. This saves time and ressources. 

If we are talking about integrating jasper reports into liferay we will start with a simple example: A portlet displaying reports. The simplest approach is to create a portlet with an iFrame that points to a servlet delivering the report. 

//get input stream to jasper file
InputStream input = Thread.currentThread().getContextClassLoader() .getResourceAsStream("report1.jasper");
final FileBufferedOutputStream fbos = new FileBufferedOutputStream();
//load report
JasperReport report = (JasperReport) JRLoader.loadObject(input);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, null);
//export report as PDF
JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);

//write pdf to servlet output stream
ServletOutputStream ouputStream = response.getOutputStream();

Now all you have to do, is to decide whether you want to show your report inline in an iFrame or downloadable via a link. Have fun :)

If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.