Concat reports using Jasper library
authorCarlos Aristu <carlos.aristu@openbravo.com>
Fri, 15 May 2015 15:17:50 +0200
changeset 26749 5ea02a80a35c
parent 26748 47b88136f675
child 26750 80c2246bd89f
Concat reports using Jasper library
modules/org.openbravo.client.application/src/org/openbravo/client/application/report/ReportingUtils.java
src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/report/ReportingUtils.java	Thu May 14 12:43:04 2015 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/report/ReportingUtils.java	Fri May 15 15:17:50 2015 +0200
@@ -30,6 +30,7 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -48,6 +49,7 @@
 import net.sf.jasperreports.engine.design.JasperDesign;
 import net.sf.jasperreports.engine.export.HtmlExporter;
 import net.sf.jasperreports.engine.export.JRCsvExporter;
+import net.sf.jasperreports.engine.export.JRPdfExporter;
 import net.sf.jasperreports.engine.export.JRXlsExporter;
 import net.sf.jasperreports.engine.fill.JRSwapFileVirtualizer;
 import net.sf.jasperreports.engine.util.JRSwapFile;
@@ -57,6 +59,7 @@
 import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
 import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;
 import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
+import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
 import net.sf.jasperreports.export.SimpleWriterExporterOutput;
 import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
 import net.sf.jasperreports.export.type.HtmlSizeUnitEnum;
@@ -1035,6 +1038,77 @@
   }
 
   /**
+   * Returns a PDF file into an output stream as result of the concatenation of the JasperPrint
+   * objects list passed as parameter.
+   * 
+   * @param jasperPrintList
+   *          A list of JasperPrint objects.
+   * @param createBoomkmarks
+   *          A flag to indicate if the document should contain bookmarks, to mark the beginning of
+   *          each individual document that was part of the initial document list.
+   * @param outputStream
+   *          The output stream used for returning the report.
+   * @throws JRException
+   *           In case there is any error compiling the report an exception is thrown with the error
+   *           message.
+   */
+  public static void concatPDFReport(List<JasperPrint> jasperPrintList, boolean createBookmarks,
+      OutputStream outputStream) throws JRException {
+
+    JRPdfExporter exporter = new JRPdfExporter();
+    SimpleOutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
+        outputStream);
+    SimplePdfExporterConfiguration reportConfiguration = new SimplePdfExporterConfiguration();
+
+    reportConfiguration.setCreatingBatchModeBookmarks(createBookmarks);
+    exporter.setConfiguration(reportConfiguration);
+    exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
+    exporter.setExporterOutput(exporterOutput);
+
+    exporter.exportReport();
+  }
+
+  /**
+   * Returns an encrypted PDF file into an output stream as result of the concatenation of the
+   * JasperPrint objects list passed as parameter.
+   * 
+   * @param jasperPrintList
+   *          A list of JasperPrint objects.
+   * @param createBoomkmarks
+   *          A flag to indicate if the document should contain bookmarks, to mark the beginning of
+   *          each individual document that was part of the initial document list.
+   * @param userPassword
+   *          A String that contains the user password of the resulting document.
+   * @param ownerPassword
+   *          A String that contains the owner password of the resulting document.
+   * @param outputStream
+   *          The output stream used for returning the report.
+   * @throws JRException
+   *           In case there is any error compiling the report an exception is thrown with the error
+   *           message.
+   */
+  public static void concatPDFReportEncrypted(List<JasperPrint> jasperPrintList,
+      boolean createBookmarks, String userPassword, String ownerPassword, OutputStream outputStream)
+      throws JRException {
+
+    JRPdfExporter exporter = new JRPdfExporter();
+    SimpleOutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
+        outputStream);
+    SimplePdfExporterConfiguration reportConfiguration = new SimplePdfExporterConfiguration();
+
+    reportConfiguration.setEncrypted(true);
+    reportConfiguration.set128BitKey(true);
+    reportConfiguration.setUserPassword(userPassword);
+    reportConfiguration.setOwnerPassword(ownerPassword);
+    reportConfiguration.setCreatingBatchModeBookmarks(createBookmarks);
+    exporter.setConfiguration(reportConfiguration);
+    exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
+    exporter.setExporterOutput(exporterOutput);
+
+    exporter.exportReport();
+  }
+
+  /**
    * Adds the parameters needed to print a report from a standard process definition into the
    * parameter map.
    * 
--- a/src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java	Thu May 14 12:43:04 2015 +0200
+++ b/src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java	Fri May 15 15:17:50 2015 +0200
@@ -77,11 +77,6 @@
 import org.openbravo.utils.FormatUtilities;
 import org.openbravo.xmlEngine.XmlDocument;
 
-import com.itextpdf.text.Document;
-import com.itextpdf.text.pdf.PdfCopy;
-import com.itextpdf.text.pdf.PdfImportedPage;
-import com.itextpdf.text.pdf.PdfReader;
-
 @SuppressWarnings("serial")
 public class PrintController extends HttpSecureAppServlet {
   private final Map<String, TemplateData[]> differentDocTypes = new HashMap<String, TemplateData[]>();
@@ -241,6 +236,8 @@
          */
         archivedReports = true;
         Report report = null;
+        JasperPrint jasperPrint = null;
+        Collection<JasperPrint> jrPrintReports = new ArrayList<JasperPrint>();
         final Collection<Report> savedReports = new ArrayList<Report>();
         for (int index = 0; index < documentIds.length; index++) {
           String documentId = documentIds[index];
@@ -248,14 +245,15 @@
               OutputTypeEnum.ARCHIVE);
           buildReport(response, vars, documentId, reports, reportManager);
           try {
-            reportManager.processReport(report, vars);
+            jasperPrint = reportManager.processReport(report, vars);
+            jrPrintReports.add(jasperPrint);
           } catch (final ReportingException e) {
             log4j.error(e);
           }
           reportManager.saveTempReport(report, vars);
           savedReports.add(report);
         }
-        printReports(response, null, savedReports);
+        printReports(response, jrPrintReports, savedReports);
       } else {
         if (vars.commandIn("DEFAULT")) {
 
@@ -489,8 +487,7 @@
           ReportingUtils.saveReport(jasperPrint, ExportType.PDF, null, os);
         }
       } else {
-        response.setContentType("application/pdf");
-        concatReport(reports.toArray(new Report[] {}), response);
+        concatReport(reports.toArray(new Report[] {}), jrPrintReports, response);
       }
       for (Iterator<Report> iterator = reports.iterator(); iterator.hasNext();) {
         Report report = iterator.next();
@@ -525,77 +522,25 @@
     }
   }
 
-  /*
-   * This method is base on code originally created by Mark Thompson (Concatenate.java) and
-   * distributed under the following conditions.
-   * 
-   * $Id: Concatenate.java 3373 2008-05-12 16:21:24Z xlv $
-   * 
-   * This code is free software. It may only be copied or modified if you include the following
-   * copyright notice:
-   * 
-   * This class by Mark Thompson. Copyright (c) 2002 Mark Thompson.
-   * 
-   * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
-   * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-   */
-  private void concatReport(Report[] reports, HttpServletResponse response) {
+  private void concatReport(Report[] reports, Collection<JasperPrint> jrPrintReports,
+      HttpServletResponse response) {
     try {
-      int pageOffset = 0;
-      // ArrayList master = new ArrayList();
-      int f = 0;
       String filename = "";
-      Report outFile = null;
-      if (reports.length == 1)
+      boolean createBookmarks = true;
+      if (reports.length == 1) {
         filename = reports[0].getFilename();
-      Document document = null;
-      PdfCopy writer = null;
-      while (f < reports.length) {
-        if (filename == null || filename.equals("")) {
-          outFile = reports[f];
-          if (multiReports) {
-            filename = outFile.getTemplateInfo().getReportFilename();
-            filename = filename.replaceAll("@our_ref@", "");
-            filename = filename.replaceAll("@cus_ref@", "");
-            filename = filename.replaceAll(" ", "_");
-            filename = filename.replaceAll("-", "");
-            filename = filename + ".pdf";
-          } else {
-            filename = outFile.getFilename();
-          }
-        }
-        response.setHeader("Content-disposition", "attachment" + "; filename=" + filename);
-        // we create a reader for a certain document
-        PdfReader reader = new PdfReader(reports[f].getTargetLocation());
-        reader.consolidateNamedDestinations();
-        // we retrieve the total number of pages
-        int n = reader.getNumberOfPages();
-        pageOffset += n;
-
-        if (f == 0) {
-          // step 1: creation of a document-object
-          document = new Document(reader.getPageSizeWithRotation(1));
-          // step 2: we create a writer that listens to the document
-          writer = new PdfCopy(document, response.getOutputStream());
-          // step 3: we open the document
-          document.open();
-        }
-        // step 4: we add content
-        PdfImportedPage page;
-        for (int i = 0; i < n;) {
-          ++i;
-          page = writer.getImportedPage(reader, i);
-          writer.addPage(page);
-        }
-        if (reports[f].isDeleteable()) {
-          File file = new File(reports[f].getTargetLocation());
-          if (file.exists() && !file.isDirectory()) {
-            file.delete();
-          }
-        }
-        f++;
+        createBookmarks = false;
+      } else if (reports.length > 1) {
+        filename = reports[0].getTemplateInfo().getReportFilename();
+        filename = filename.replaceAll("@our_ref@", "");
+        filename = filename.replaceAll("@cus_ref@", "");
+        filename = filename.replaceAll(" ", "_");
+        filename = filename.replaceAll("-", "");
+        filename = filename + ".pdf";
       }
-      document.close();
+      response.setHeader("Content-disposition", "attachment" + "; filename=" + filename);
+      ReportingUtils.concatPDFReport(new ArrayList<JasperPrint>(jrPrintReports), createBookmarks,
+          response.getOutputStream());
     } catch (Exception e) {
       log4j.error(e);
     }