19 package org.turro.pdf;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Collection;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.servlet.ServletOutputStream;
29 import javax.servlet.http.HttpServletResponse;
30 import net.sf.jasperreports.engine.JRDataSource;
31 import net.sf.jasperreports.engine.JRException;
32 import net.sf.jasperreports.engine.JasperExportManager;
33 import net.sf.jasperreports.engine.JasperFillManager;
34 import net.sf.jasperreports.engine.JasperPrint;
35 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
36 import org.amic.util.file.FileUtil;
44 public static void writeAsResponse(HttpServletResponse response, String jasperFile, Collection collection, Map parameters) {
45 ServletOutputStream output =
null;
47 response.setContentType(
"application/pdf");
48 response.setHeader(
"pragma",
"no-cache");
49 response.setHeader(
"Cache-control",
"no-cache, no-store, must-revalidate");
50 response.setHeader(
"Expires",
"01 Apr 1995 01:10:10 GMT");
51 response.setHeader(
"Content-Disposition",
"filename=" + FileUtil.getBaseName(
new File(jasperFile)) +
".pdf" );
53 output = response.getOutputStream();
56 }
catch (IOException ex) {
57 Logger.getLogger(
GeneratePDF.class.getName()).log(Level.SEVERE,
null, ex);
61 }
catch (IOException ex) {
62 Logger.getLogger(
GeneratePDF.class.getName()).log(Level.SEVERE,
null, ex);
67 public static void exportToStream(OutputStream out, String jasperFile, Collection collection, Map parameters) {
69 JRDataSource dataSource =
new JRBeanCollectionDataSource(collection);
70 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile, parameters, dataSource);
71 JasperExportManager.exportReportToPdfStream(jasperPrint, out);
72 }
catch (JRException ex) {
73 Logger.getLogger(
GeneratePDF.class.getName()).log(Level.SEVERE,
null, ex);
static void exportToStream(OutputStream out, String jasperFile, Collection collection, Map parameters)
static void writeAsResponse(HttpServletResponse response, String jasperFile, Collection collection, Map parameters)