I have to export multiple list data to multiple pdf files. Code is shown below:
public void prinReport(String reportType){
String reportPath ="",fileName="";
List<LoanDisplayModel> loanDisplayModelList=new ArrayList<>();
Map<String, Object> params = new HashMap();
if(reportType.equals("Sanchipta")){
reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath
("/reports/np/report1.jasper");
loanDisplayModelList=loanSanchipitaModelList;
fileName="reportOne.pdf";
}
else if(reportType.equals("Bistrit")){
reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath
("/reports/np/report2.jasper");
loanDisplayModelList=loanBistritModelList;
fileName="reportTwo.pdf";
}
else{
reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath
("/reports/np/report3.jasper");
loanDisplayModelList=loanSahaBistritModelList;
fileName="reportThree.pdf";
}
JasperPrint jasperPrint;
try {
jasperPrint = JasperFillManager.fillReport(reportPath, params,new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(
loanDisplayModelList));
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.setContentType("application/x-download");
httpServletResponse.addHeader("Content-disposition", "inline; filename="+fileName+"");
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The above code is used to export list to pdf file.Using httpServletResponse.setContentType("application/x-download");
downloads the pdf file.
For a single list the code works perfectly. But I have three different lists and i have to export the list to different pdf files on a single button click.
The button click code is as follows:
<h:commandButton value="Export" actionListener="#{closingMB.setReports}" id="cmdExport" ajax="false"/>
The actionListener calls setReports() function where three different lists are set then printReport() function is called and with reportType as a parameter. The filename,reportPath and loanDisplayModelList is set according to reportType . Now i am facing a problem i.e only the last list is exported to pdf file and downloaded and the initial pdf files are not exported and the fileName is set as reportOne.pdf i.e the first fileName.
Note: I need three different pdf files with different filenames.
Can someone help me with this problem.