Inserting charts into PDF


JasperReport is report generation tool used to create reports in Java. And also used to include Charts, Images, and Tables into the report. It can produce the reports in multiple formats like PDF, CSV, HTML, XLS etc. In this article, first we discussed about creation of PDF file using jasper report.

Tools required to generate Jasper reports are JRE 1.5 or above, jasperreport-3.0.0.jar, itext-1.2.3.jar.

Jasper uses JRXML files to generate report. For easy to generate JRXML, we can use iReport-2.0.4 or later. IReports is a tool which is used to generate JRXML. It provides GUI to design the report elements such as titles, fields, variables, parameters, charts etc.

The generated JRXML file contains report design information like title, page footers, page headers, page size, margins etc.

The JRXML file needs to be compiled to generate the JASPER file, which will be used by JasperReport classes to generate the actual report. JRXML can be compiled using method compileReport() of net.sf.jasperreports.engine.JasperCompileManager class. The compiled binary file will have extension “.jasper”. Also we can use iReports to compile the JRXML file.

For example to create report in form of PDF the code is given below.

JasperReport jasperReport = null;

JasperPrint jasperPrint = null;

InputStream jasperStream = null;

try {

jasperStream = this.getClass().getClassLoader().getResourceAsStream(

“fileName.jasper”);———————————————————(1)

jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);————–(2)

jasperPrint = JasperFillManager.fillReport(jasperReport, reportParams, dataSource);——————————————————————————–(3)

//exporting the PDF file

JasperExportManager.exportReportToPdfFile(jasperPrint, fileName);———–(4)

} catch (Exception e) {

e.printStackTrace();

}

}

In above code JasperReport is instance of net.sf.jasperreports.engine.JasperReport and holds the jasper file stream. We are loading the jasper stream at step (1, 2). Here we are using the JRLoader.loadObject() method to load the jasper stream into jasperReport. Now jasperReport instance contains the compiled “.jasper”. The “.jasper” is nothing but the compiled JRXML which holds the design and information about where the different data should fit.

JasperFillManager.fillReport(…..) method will returns a instance of JasperPrint class and that can be used to export the data into PDF, CSV, XLS etc. In above example at step(3) we are creating JasperPrint instance. It takes the parameters

· jasperReport having the compiled .jasper file,

· reportParams is actually a java.util.Map. We can set any parameters and their values as key value pairs in map. And then passed as parameter for method fillReport(…..). This contains the certain static data to be displayed in report.

· dataSource is a instance of JRDataSource. Whatever the data we want to show in report can be send as dataSource. This contains the DataSource which will be iterated by JASPER report and shown the data in a table.

Now the jasperPrint instance is created, which actually holds the data viewed in PDF page. Finally the exportreportToPdfFile() method of JasperExportManger is used to export the report object into the PDF file and stored with the specified file name. We already know that jasperPrint instance has the report object to export on the page. This object will be sent as a parameter to JasperExportManager.exportReportToPdfFile() method. Finally it returns binary content as bite[] array. This array is nothing but the PDF file.

Up to now creation of PDF is completed. Now we discussed about creating charts and insert them into PDF file.

ChartFx for Java 6.5 Development is a tool to develop charts and maps in the form of JPEG, PNG etc. This provides APIs which are required to develop charts. These APIs are used to set charts title, labels, size, background color, axis labels, chart type etc. Charts which are developed using chartFx are stored in temporary directory. So we can capture them from temp location and display in PDF.

For example see the given code.

String path= null;

ChartServer chart1 = new ChartServer(context, request, response);

……………………….

……………………….

path = chart1.getHtmlTag(“1560″, “2000”, “PNG”);

new ChartServer(context, request, response) will create chartServer object. This object is the actual chart object which will be used to create a chart image. Here the parameter context is of type javax.servlet.ServletContext, request is of type javax.servlet.http.HttpServletRequest and response is of type javax.servlet.http.HttpServletResponse. One more default constructor is also available to create chart server is ChartServer(). There are many more methods are available in this class to set the title of chart, x-axis title, y-axis title, background color, font color, font size, font style etc. In our example we skipped the code of designing part of chart. Any one can design as per their requirement. At the end method getHtmlTag(“1560″, “2000”, “PNG”), will return html image tag(<img …. />) which will have “src” attribute and this attribute has path of chart image file. Also the <img /> element will have the width and height attributes which will hold 1560 and 2000 respectively.

For our example, think that charts are already created and placed in location “C:images flower.jpeg”. Now we display generated charts into PDF report. For that, first create parameter in the JRXML file using the iReport.

In iReports goto File–>view–>parameters and then select “parameters” tab, and then click “new” button. Give a name for that parameter and type as String. Later we assign value for this parameter from our java code using java.util.Map.

clip_image0021

And then add image element in our JRXML. Right click on image element and open properties and click on “Image Tab” then add “Image Expression” for that element.

clip_image0022

And then design report as per your requirement and compile to generate “.Jasper”. Now we have to send path for this image to display in PDF from our java code. The java code given below uses the image available at “C:images flower.jpeg” location.

Map reportParameters = new Hashmap();

reportParameters.put(“chartPath”,” C:\images \flower.jpeg”);

jasperPrint = JasperFillManager.fillReport(jasperReport, reportParameters,datasource);

Then pass this map as parameter at the time of creating jasperPrint object. Now the path for the image is known for jasperFillManager. So it creates jasperPrint object including this image.

Problem when inserting chart in PDF

Some times the inserting chart will have the size that may not be fit into actual size set in JRXML file for that image. In this situation while creating the chart, we can divide original chart into number of charts.

For example, in JRXML we designed the report like it can show chart of 500 pixels height. The chart having 20 bars only perfectly fit in this image element. Now what we have to do is, while creating chart, create two or more charts depending on the result and display them one by one in report.

For example, we want to display bar chart for students’ percentages. There are 55 students in class room. As per our design only chart contains bars for 20 students will fit perfectly in image element. Now what we have to do is, while creating chart split it into three charts contains students’ percentage bars of 20, 20, and 15 each. Now display these images into the PDF and they will be displayed perfectly.

Wow! Finally we get the charts into PDF with the combination of ChartFX and Jasper Reports. I hope this will help anyone to develop reports in the form of PDF.

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

thanku very much. it is very useful info to me.
byeeee

nice article. can you please provide details about inserting HTML table date export into PDF? thanks in advance.

nice article. can you please provide details about HTML table data export into PDF? thanks in advance.

Leave a comment

(required)

(required)