Generate multiple page Report for multiple entities

Hi!

I am using
reportParams.put("entity", this.getItem());
reportGuiManager.printReport(report, reportParams, null, "my invoice", null);
to print a one sheet invoice

Is it possible to provide reportGuiManager with a list of entityes and print for example a 10page pdf, with 1 page for each invoice?

Thank you

I actually generated multiple reports and merged them with iText, I used the example from this website

If anyone else is interested, I can provide the adapted code

Kind regards,
George

Interesting, would appreciate if you share.

Hi

At the time I wrote this I was in a hurry, I’m sure it can be improved:

In entity browser controller

if(comandaLucrusTable.getSelected() != null && comandaLucrusTable.getSelected().size()>1){
          FileDescriptor myFiledescriptor = spicaService.printMultiple(comandaLucrusTable.getSelected());
          if(myFiledescriptor !=null){
              exportDisplay.show(myFiledescriptor, null);
          }

        }

print multiple Service

@Override
    public FileDescriptor printMultiple(Set<ComandaLucru> comenzi) {

//        http://java-x.blogspot.ro/2006/11/merge-pdf-files-with-itext.html

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        List<InputStream> streamOfPDFFiles = new ArrayList<>();
        for (ComandaLucru myComandaLucru : comenzi) {
            ReportOutputDocument myReport = generateReport(myComandaLucru);

            streamOfPDFFiles.add(new ByteArrayInputStream(myReport.getContent()));
        }

        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();


            com.haulmont.cuba.core.entity.FileDescriptor fileDescriptor;
            fileDescriptor = metadata.create(com.haulmont.cuba.core.entity.FileDescriptor.class);
            byte[] bytes = outputStream.toByteArray();

            fileDescriptor.setName("cocobau.pdf");
            fileDescriptor.setExtension(".pdf");
            fileDescriptor.setSize((long) bytes.length);
            fileDescriptor.setCreateDate(new Date());
            fileLoader.saveStream(fileDescriptor, () -> new ByteArrayInputStream(bytes));

            dataManager.commit(fileDescriptor);
            outputStream.close();
            return (fileDescriptor);




        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen())
                document.close();
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return null;
    }


    ReportOutputDocument generateReport(ComandaLucru comandaLucru) {
        Map<String, Object> reportParams = new HashMap<>();

        LoadContext<Report> lContext = new LoadContext<>(Report.class);
        lContext.setQueryString("select r from report$Report r where r.code like '%fisa_service%'").setMaxResults(2);
        List<Report> reports = dataService.loadList(lContext);

        Report targetreport = reports.get(0);

        reportParams.put("entity", comandaLucru);
        Long counter = getValLong("contorPrintFisaService");
        setValLong("contorPrintFisaService", counter + 1);
        reportParams.put("contor", "FS" + comandaLucru.getNrRegistru() + "-" + counter);
        reportParams.put("qrCode", "lol");


        String fileName = "Comanda Nr. " + comandaLucru.getNrRegistru() + ".pdf";

        String templateCode = "Service";

        Collection<OfertaLinie> linii = new ArrayList<>();
        if (comandaLucru.getOferta() != null) {
            Oferta myOferta = comandaLucru.getOferta();
            int i = 0;
            for (OfertaLinie myLinie : myOferta.getLinii()) {

                if ((myLinie.getProdus() != null) && (myLinie.getProdus().getTip() != ProdusTip.Serviciu) && i < 15) {
                    linii.add(myLinie);
                    i++;
                }
            }
        }
        reportParams.put("linii", linii);

        switch (comandaLucru.getTip()) {
            case Instalare:
                templateCode = "Instalare";
                break;
            case Service:
                templateCode = "Service";
                break;
            case Mentenanta:
                switch (comandaLucru.getProiect().getTipSistem()) {
                    case SupraveghereVideo:
                        templateCode = "MentenantaSupraveghere";
                        break;
                    case ControlAcces:
                        templateCode = "MentenantaControlAcces";
                        break;
                    case DetectieEfractie:
                        templateCode = "MentenantaDetectieEfractie";
                        break;
                    case DetectieIncendiu:
                        templateCode = "MentenantaDetectieIncendiu";
                        break;
                    case Hidranti:
                        templateCode = "MentenantaHidranti";
                        break;
                    case Sprinklere:
                        templateCode = "MentenantaSprinklere";
                        break;
                    case Automatizare:
                        templateCode = "MentenantaAutomatizare";
                        break;
                }
                break;

        }

        ReportOutputDocument outputDocument = reportService.createReport(targetreport, templateCode, reportParams);
        return outputDocument;
    }

Hi George,

Reporting subsystem has no ability to merge several PDF reports into one PDF.
On the other side, if you print a report for several entities, report subsystem generates zip with several files. See TablePrintFormAction (https://doc.cuba-platform.com/reporting-6.7/run_actions.html)

Thanks,
Andrey Subbotin