iReport Designer can create PDF files for publishing as static web content. Alternatively, the products of iReport Designer -- .jrxml and .jasper files -- can be called by JSP for dynamic configuration, layout, or avoiding the file system.
|PDF Generated by JSP|
Simple PDF JSP
The following JSP is a two-line API invocation that configures a JasperPrint object and then streams it using a static method "exportReportToPDFStream".
<%@ page contentType="application/pdf" %>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="java.util.*" %>
JasperPrint print = JasperFillManager.fillReport(
The JasperPrint object is a ready-to-export internal copy of a report. In this case, a file called EmptyReport.jasper was created in iReport Designer. The .jasper file is the compiled product from iReport Designer. This is different than the .jrxml file that contains the actual contents of a Jasper Report. The compilation comes from either previewing or explicitly compiling a file. The JasperPrint object is filled with substituted parameter values (in contrast to a JasperReport object which is not).
|iReport Designer with Compile Command|
In the example above, an empty parameter map ("new HashMap()") is passed to the JasperPrint object as well as an empty JREmptyDataSource. The map and the datasource could be provided by the container (Spring, etc.) or through user input (form, URL). The location and name of the .jasper file should also be parameterized.
JasperExportManager is a facade for several classes including JRPdfExporter. JasperExportManager contains a convenient static method exportReportToPdfStream() that takes the JasperPrint object set up in the first line of the scriptlet and the OutputStream from the HTTP response object.
This example JSP is deployed in Tomcat 7.0.14. The webappp contains the JSP 'streamEmptyReport.jsp' and a /WEB-INF/lib folder. web.xml files are no longer required. The contents of the lib folder are a set o JAR files taken from the modules/ext folder in iReport Designer. For my installation, this is C:\Program Files\Jaspersoft\iReport-4.0.2\ireport\modules\ext.
Be careful not to mix-and-match versions of .jasper files and JAR files. If you get a strange error, something other than "File not found", it's probably an incompatibility. You can't run a 4.0.2-generated .jasper file in a webapp containing 4.0.1 JAR files. A later post will demonstrate compiling on-the-fly which will work with the more portable .jrxml files.
A Java Server Page (JSP) is a dynamic web page. Usually, JSPs are used to render HTML. In this example, the JSP is used to render a PDF. There is a setting at the top of the example which provides a contentType of application/pdf. This is an important cue to the browser to bring up the Adobe plug-in. Keep the contentType in sync with what's being rendered by the JasperReports API calls.
Publishing static contents generated from iReport Designer is an easy way to get reports online. Having a web server like Apache retrieve static content is very fast and can scale by replicating the static files across multiple instances. However, many cases will require a dynamic component whether a datasource, input parameters, or layout. In these cases use the JasperReports API integrated into a Java EE app or simple JSP.