Combining two Jasper reports

前端 未结 5 1640
迷失自我
迷失自我 2020-11-30 07:15

I have a web application with a dropdown from where user could select the type of report viz. report1, report2, report3, etc.

Based on the report selected, a Jasper

相关标签:
5条回答
  • 2020-11-30 07:33

    You can either merge reports before generating PDFs using JasperPrint or after generating PDFs using iText.

    For the JasperPrint solution: you will generate the 2 (or more) JasperPrints then get the content pages and concat them.

    JasperPrint jp1 = JasperFillManager.fillReport(url.openStream(), parameters,
                        new JRBeanCollectionDataSource(inspBean));
    JasperPrint jp2 = JasperFillManager.fillReport(url.openStream(), parameters,
                        new JRBeanCollectionDataSource(inspBean));
    
    List pages = jp2 .getPages();
    for (int j = 0; j < pages.size(); j++) {
        JRPrintPage object = (JRPrintPage)pages.get(j);
        jp1.addPage(object);
    }
    JasperViewer.viewReport(jp1,false);
    

    For the iText solution after generating the PDFs:

    void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) {
    
        Document document = new Document();
        try {
          List<InputStream> pdfs = streamOfPDFFiles;
          List<PdfReader> readers = new ArrayList<PdfReader>();
          int totalPages = 0;
          Iterator<InputStream> iteratorPDFs = pdfs.iterator();
    
          // Create Readers for the pdfs.
          while (iteratorPDFs.hasNext()) {
            InputStream pdf = iteratorPDFs.next();
            PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            totalPages += pdfReader.getNumberOfPages();
          }
          // Create a writer for the outputstream
          PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    
          document.open();
          BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
          PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
          // data
    
          PdfImportedPage page;
          int currentPageNumber = 0;
          int pageOfCurrentReaderPDF = 0;
          Iterator<PdfReader> iteratorPDFReader = readers.iterator();
    
          // Loop through the PDF files and add to the output.
          while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();
    
            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
              document.newPage();
              pageOfCurrentReaderPDF++;
              currentPageNumber++;
              page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
              cb.addTemplate(page, 0, 0);
    
              // Code for pagination.
              if (paginate) {
                cb.beginText();
                cb.setFontAndSize(bf, 9);
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0);
                cb.endText();
              }
            }
            pageOfCurrentReaderPDF = 0;
          }
          outputStream.flush();
          document.close();
          outputStream.close();
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          if (document.isOpen())
            document.close();
          try {
            if (outputStream != null)
              outputStream.close();
          } catch (IOException ioe) {
            ioe.printStackTrace();
          }
        }
      }
    
    0 讨论(0)
  • 2020-11-30 07:36

    You can try this one this is work for me

    JasperPrint jp1 = JasperFillManager.fillReport(reportFile1,reportParams,Connection);  
    JasperPrint jp2 = JasperFillManager.fillReport(reportFile2,reportParams,Connection);  
    for (int j = 0; j < jp1.getPages().size(); j++) {  
        //Add First report to second report
        jp2.addPage((JRPrintPage) jp1.getPages().get(j));  
    } 
    
    0 讨论(0)
  • 2020-11-30 07:42

    Here is sample code for combining multiple jasper prints

    List<JasperPrint> jasperPrints = new ArrayList<JasperPrint>();
    // Your code to get Jasperreport objects
    JasperReport jasperReportReport1 = JasperCompileManager.compileReport(jasperDesignReport1);
    jasperPrints.add(jasperReportReport1);
    JasperReport jasperReportReport2 = JasperCompileManager.compileReport(jasperDesignReport2);
    jasperPrints.add(jasperReportReport2);
    JasperReport jasperReportReport3 = JasperCompileManager.compileReport(jasperDesignReport3);
    jasperPrints.add(jasperReportReport3);
    
    JRPdfExporter exporter = new JRPdfExporter();
    //Create new FileOutputStream or you can use Http Servlet Response.getOutputStream() to get Servlet output stream
    // Or if you want bytes create ByteArrayOutputStream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
    exporter.exportReport();
    byte[] bytes = out.toByteArray();
    
    0 讨论(0)
  • 2020-11-30 07:44

    Here is my code which i use on grails code as well as java.Its gives me two different report in one pdf.

    String reportDir = Util.getReportDirectory() // my report directory
    Map reportParams = new LinkedHashMap()
    Map reportParams1 = new LinkedHashMap()
    
    String outputReportName="Test_Output_copy"
    
    reportParams.put('parameter name',"parameter")
    reportParams1.put('copy',"Customer's Copy")
    
    JasperReportDef reportDef1 = new JasperReportDef(name: 'testBillReport.jasper', fileFormat: JasperExportFormat.PDF_FORMAT,
                parameters: reportParams, folder: reportDir)
    JasperReportDef reportDef2 = new JasperReportDef(name: 'testBillReport.jasper', fileFormat: JasperExportFormat.PDF_FORMAT,
                parameters: reportParams1, folder: reportDir)
    
    List<JasperReportDef> jasperPrintList = new ArrayList<JasperReportDef>();
        jasperPrintList.add(reportDef1);
        jasperPrintList.add(reportDef2);
    
    ByteArrayOutputStream report1 = jasperService.generateReport(jasperPrintList);
        response.setHeader("Content-disposition", "inline;filename="+outputReportName+'.pdf')
        response.contentType = "application/pdf"
        response.outputStream << report1.toByteArray()
    
    0 讨论(0)
  • 2020-11-30 07:48

    This answer is to help user using latest version of Jasper-Report. In @Sangram Jadhav accept answer the JRExporterParameter.JASPER_PRINT_LIST is deprecated

    The current code would be:

    Map<String, Object> paramMap = new HashMap<String, Object>();
    List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
    JasperPrint jasperPrint1 = JasperFillManager.fillReport(report1, paramMap);
    jasperPrintList.add(jasperPrint1);
    JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, paramMap);
    jasperPrintList.add(jasperPrint2);
    
    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); //Set as export input my list with JasperPrint s
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("pdf/output.pdf")); //or any other out streaam
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    configuration.setCreatingBatchModeBookmarks(true); //add this so your bookmarks work, you may set other parameters
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    
    0 讨论(0)
提交回复
热议问题