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