I've recently been researching reporting tools for a project I will be soon be working on. One of the tools I've been looking at is JasperReports. JasperReports is a very popular open source (LGPL) reporting library written in Java. Unfortunately it is not very well documented and I had a hard time coming up with a simple report. After some research, I was able to generate a simple report, this article summarizes what needs to be done to get started with JasperReports. See Resources for information on how to find additional information and documentation about JasperReports.
Getting Started
JasperReports' reports are defined in XML files, which by convention have an extension of jrxml. A typical jrxml file contains the following elements:
- the root element.
- its contents are printed only once at the beginning of the report
- its contents are printed at the beginning of every page in the report.
- contains the body of the report.
- its contents are printed at the bottom of every page in the report.
- defines a report section, all of the above elements contain aband
element as its only child element.
All of the elements are optional, except for the root jasperReport
element. Here is an example jrxml file that will generate a simple report displaying the string "Hello World!"
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
For this simple example, I omitted the optional
,
and
elements. The
element, unsurprisingly, displays static text on the report, as can be seen, it contains a single
element defining the text that will be displayed.
jrxml files need to be "compiled" into a binary format that is specific to JasperReports, this can be achieved by calling the compileReport()
method on the net.sf.jasperreports.engine.JasperCompileManager
class. There are several overloaded versions of this method, in our example, we will use the one that takes a single String
parameter, consult the JasperReport documentation for details on the other versions of the method.
public class JasperReportsIntro
{
public static void main(String[] args)
{
JasperReport jasperReport;
JasperPrint jasperPrint;
try
{
jasperReport = JasperCompileManager.compileReport(
"reports/jasperreports_demo.jrxml");
jasperPrint = JasperFillManager.fillReport(
jasperReport, new HashMap(), new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/simple_report.pdf");
}
catch (JRException e)
{
e.printStackTrace();
}
}
}
A jrxml file needs to be compiled only once, but for this simple example it is compiled every time the application is executed. Before a report can be generated, it needs to be "filled" with data, this is achieved by calling the fillReport()
method on the net.sf.jasperreports.engine.JasperFillManager
class. Again, there are several overloaded versions of the fillReport()
method, here we will use one that takes three parameters, an instance of net.sf.jasperreports.engine.JasperReport
, a java.util.HashMap
containing any parameters passed to the report, and an instance of a class implementing the net.sf.jasperreports.engine.JRDataSource
interface. The line that accomplishes this in our example above is
jasperPrint = JasperFillManager.fillReport(
jasperReport, new HashMap(), new JREmptyDataSource());
Since our simple report takes no parameters, we pass an empty HashMap
as the second parameter, and an instance of net.sf.jasperreports.engine.JREmptyDataSource
as the third parameter. JREmptyDataSource
is a convenience class included with JasperReports, it is basically a DataSource with no data.
Finally, in our example, we export the report to a PDF file, this way it can be read by Adobe Acrobat, XPDF, Evince, or any other PDF reader, the line that accomplishes this in our example is
JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/simple_report.pdf");
The parameters are self-explanatory.
Conclusion
This article provides a brief howto on how to display PDF reports generated with JasperReports from a web application to the user's browser. Basic knowledge of JasperReports and Java Servlet programming is assumed. See the Resources section for links to JasperReports and Servlet programming tutorials and documentation.
The trick to sending a PDF report generated by JasperReports to the user's browser is to call the net.sf.jasperreports.engine.JasperRunManager.runReportToPdf()
method. That method has several overloaded versions, the one we will use here has three parameters, a String
representing the absolute path of the compiled report (jasper file), an instance of a class implementing the java.util.Map
interface, and an instance of a class implementing the net.sf.jasperreports.engine.JRDataSource
interface. The JasperRunManager.runReportToPdf()
method returns an array of bytes that can be passed as a parameter to the write()
method of the javax.servlet.ServletOutputStream
class. An instance of ServletOutputStream
can be obtained from the getOutputStream()
method of the javax.servlet.http.HttpServletResponse
class. The best way to explain and visualize all of this is by example, the following code segment demonstrates this technique:
package tutorial.jasperreportsbrowserdemo;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
public class JasperReportsBrowserDemoServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ServletOutputStream servletOutputStream = response.getOutputStream();
File reportFile = new File(getServletConfig().getServletContext()
.getRealPath("/reports/Simple_Report.jasper"));
byte[] bytes = null;
try
{
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),
new HashMap(), new JREmptyDataSource());
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
}
catch (JRException e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
No comments:
Post a Comment