0

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.

Alex K
  • 22,315
  • 19
  • 108
  • 236
Kiran
  • 1,119
  • 1
  • 10
  • 19
  • that is a hardcoded name, each time the same in this code, how are they unique/different to you? – Stultuske Nov 20 '18 at 08:46
  • I have different functions for different exports and the list,report path ,filenames are different in each functions , above i have just shown the code of one function . – Kiran Nov 20 '18 at 08:49
  • Java has methods, not functions. you shouldn't have several methods, just one, and pass the filename as parameter. you should show us the related code, not one part of it. have you debugged your code? have you tried to run the methods seperately? – Stultuske Nov 20 '18 at 08:50
  • ok let me edit my code – Kiran Nov 20 '18 at 08:53
  • You cannot (in http) download multiple files in parallel unless you ZIP them. So since it cannot work in http, it cannot work in JSF either. – Kukeltje Nov 20 '18 at 08:57
  • @Kukeltje this is not about downloading, but about generating/creating files. – Stultuske Nov 20 '18 at 09:17
  • @Kukeltje i am not talking about downloading a file only , i have to generate pdf files for each list and then download each files with different names. I will edit my question . – Kiran Nov 20 '18 at 09:26
  • Generating has definitly nothing to do with java-se or PrimeFaces. Will remove these tags then. – Kukeltje Nov 20 '18 at 09:56

0 Answers0