![]() |
|
![]() |
|||
![]() |
|||||
|
Deploying reports by Giulio Toffoli When you design and preview a report in iReport, iReport produces a Jasper file. This file
is all you need in your Java application to generate and display the report (in addition to JasperReports, of course).
Here is a simple application to create a PDF from a Jasper file using an empty data source. An empty data source is a data source that by default has a single record and for which you can define any field name to use in the report, but its value will always be null. It is a perfect data source to test a very simple report displaying just the label "Hello World!" For real applications, you will probably use a JDBC connection, a collection of JavaBeans, or a Hibernate session to provide data for the report. But for the aim of this tutorial, the empty data source is good enough.
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;
public class JasperTest {
public static void main(String[] args) {
String fileName = "test.jasper";
String outFileName = "test.pdf";
HashMap hm = new HashMap();
try {
// Fill the report using an empty data source
JasperPrint print = JasperFillManager.fillReport(fileName, hm, new JREmptyDataSource());
// Create a PDF exporter
JRExporter exporter = new JRPdfExporter();
// Configure the exporter (set output file name and print object)
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
// Export the PDF file
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
The most important line of code in this example is line 13. The class
JasperFillManager
provides several static methods to fill a Jasper file, using a data source, a connection, or even nothing.
The result of the call to the method fillReport() is the special JasperPrint object which contains an in-memory
representation of the report. This object can be previewed by using the JasperReports Viewer, a convenient Swing
component to display the report on screen (it's the same component used by the iReport preview). As in this example,
the JasperPrint object can also be passed to an exporter to generate a final document in a specific format. |
|||||
![]() |
|
![]() |