BrightSide Workbench Full Report + Source Code
GeneratePDF.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.pdf;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Collection;
25 import java.util.Map;
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;
37 
42 public class GeneratePDF {
43 
44  public static void writeAsResponse(HttpServletResponse response, String jasperFile, Collection collection, Map parameters) {
45  ServletOutputStream output = null;
46  try {
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" );
52 
53  output = response.getOutputStream();
54  exportToStream(output, jasperFile, collection, parameters);
55  output.flush();
56  } catch (IOException ex) {
57  Logger.getLogger(GeneratePDF.class.getName()).log(Level.SEVERE, null, ex);
58  } finally {
59  try {
60  output.close();
61  } catch (IOException ex) {
62  Logger.getLogger(GeneratePDF.class.getName()).log(Level.SEVERE, null, ex);
63  }
64  }
65  }
66 
67  public static void exportToStream(OutputStream out, String jasperFile, Collection collection, Map parameters) {
68  try {
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);
74  }
75  }
76 
77 }
static void exportToStream(OutputStream out, String jasperFile, Collection collection, Map parameters)
static void writeAsResponse(HttpServletResponse response, String jasperFile, Collection collection, Map parameters)