0

I have a servlet that receive a request with a parameter: it contains the ID of a sql table that i use for execute a query wich results in a 1row resultset (i can't change this servlet), then i create a JAVA Object with the result of that query and use it to create my "invoice" pdf report.

ServletOutputStream out = response.getOutputStream();
ElencoVerifiche elencoverifiche = new ElencoVerifiche();
VerificheDao vdao = new VerificheDao();
elencoverifiche = vdao.getVerifiche(idFromRequest);
DynamicReportDesign drd = new DynamicReportDesign();            
JasperReportBuilder report = drd.build(elencoverifiche);
report.toPdf(out);
out.close();

I'm quite good in generating this single pdf file and send it back to the browser with the code above

My question is: If i receive an array of "idFromRequest", how can i create multiple pdf using a loop that generate single pdf and one by one add them to a final pdf with something like the code below:

ArrayList<idFromRequest>arrayOfID= request.getParameter("arrayofid")
ServletOutputStream out = response.getOutputStream();
UNKNOWN_CLASS reportfinal = new UNKNOWN_CLASS()   ???
for(int i=0;i<arrayOfID.size();i++){            
    ElencoVerifiche elencoverifiche = new ElencoVerifiche();
    VerificheDao vdao = new VerificheDao();
    elencoverifiche = vdao.getVerifiche(idFromRequest);
    DynamicReportDesign drd = new DynamicReportDesign();            
    JasperReportBuilder report = drd.build(elencoverifiche);
    reportfinal.add(report); ??? 
}
reportfinal.toPdf(out);
out.close();

After Petter Comment i was able to solve the problem like follow:

List<JasperPrint> jasperPrints = new ArrayList<JasperPrint>();
for(int i=0;i<arrayOfID.size();i++){
    ElencoVerifiche elencoverifiche = new ElencoVerifiche();
    VerificheDao vdao = new VerificheDao();
    elencoverifiche = vdao.getVerifiche(arrayOfID.get(i).getID);
    DynamicReportDesign drd = new DynamicReportDesign();            
    JasperReportBuilder report = drd.build(elencoverifiche);
    JasperPrint jreport = report.toJasperPrint();
    jasperPrints.add(jreport);
}

JRPdfExporter exporter = new JRPdfExporter();

ByteArrayOutputStream out2 = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out2);
exporter.exportReport();
byte[] bytes = out2.toByteArray();
out.write(bytes);
Community
  • 1
  • 1
Gio Venice
  • 55
  • 8
  • any one knows how to do this??? – Gio Venice May 15 '17 at 15:28
  • Use a master report and a subreport in the detail band for this. Otherwise, you'll have to use iText to concatenate the PDFs, which is a bit of architectural duplication (i.e., JasperReports already provides ways to produce multi-page reports). – Dave Jarvis May 15 '17 at 16:16
  • if you prefer generating the pdf and then combine check out this http://stackoverflow.com/questions/23062345/function-that-can-use-itext-to-concatenate-merge-pdfs-together-causing-some – Petter Friberg May 16 '17 at 08:32
  • Petter Friberg u are my angel!!!! I will edit now the thread adding ur note and how i solved my problem!!! – Gio Venice May 17 '17 at 09:08

0 Answers0