Book Image

JasperReports for Java Developers

Book Image

JasperReports for Java Developers

Overview of this book

JasperReports is the world's most popular embeddable Java open source reporting library, providing Java developers with the power to easily create rich print and web reports. This book shows you exactly how to get started, and develop the skills to get the most from JasperReports. The book steers you through each point of report setup, to creating, designing, formatting, and exporting reports with data from a wide range of datasources, and integrating JasperReports with other Java frameworks. Starting with the basics of adding reporting capabilities to your application, and creating report templates you will first see how to produce your reports through the use of JRXML files, custom ANT targets, and then see preview them in both the browser and the native browser of JasperReports. Getting data into your reports is the next step, and you will see how to get data from a range of datasources, not only databases, but XML files, and Java Objects, among others. You will create better looking reports with formatting and grouping, as well as adding graphical elements to the report. You will export your reports to a range of different formats, including PDF and XML. Creating reports will be made even easier with a walkthrough of the iReport Designer visual designing tool. To round things off, you will see how to integrate your reports with other Java frameworks, using Spring or Hibernate to get data for the report, and Java Server Faces or Struts for presenting the report.
Table of Contents (13 chapters)
12
Index

Exporting to PDF

We have already seen examples of exporting reports to PDF in previous chapters. However, all examples we have seen so far stream a PDF report straight to the browser window. In this example, we will export a report to PDF and save it to the file system.

package net.ensode.jasperbook;
import java.io.File;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class PdfExportDemo
{
public static final String REPORT_DIRECTORY = "reports";
public void pdfExport(String reportName)
{
File file = new File(REPORT_DIRECTORY + "/" + reportName + ".jrprint");
try
{
JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(file);
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter. JASPER_PRINT, jasperPrint...