Java Reference
In-Depth Information
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);
pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
REPORT_DIRECTORY + "/" + reportName + ".pdf");
System.out.println("Exporting report...");
pdfExporter.exportReport();
System.out.println("Done!");
}
catch (JRException e)
{
e.printStackTrace();
}
}
 
Search WWH ::




Custom Search