2

I am able to generate a jasper report in pdf format .I have a dialogue like shown below

    <p:dialog id="courseListDia" header="Student Profile" widgetVar="courseEdit" resizable="true"  
               showEffect="fade" hideEffect="fade" appendTo="@(body)">  


                     <h:form id="courseForm">
                     <h:panelGrid columns="2" id="profile"> 
                         <p:graphicImage id="photo1" alt="No Image" value="/images/#{studentDetailsBeans.clickedStudent.imageUrl}" width="100px" />     
                           <h:panelGrid columns="2" id="Details"> 
                           <p:outputLabel value="Student Name"/>
                           <p:outputLabel value="#{studentDetailsBeans.clickedStudent.studentFirstName} #{studentDetailsBeans.clickedStudent.studentLastName}"  />
                           <p:outputLabel value="Date of Birth"/> 
                           <h:outputText value="#{studentDetailsBeans.clickedStudent.studentDob}">                       
                           <f:convertDateTime pattern="dd.MM.yyyy"/>
                           </h:outputText>
                           <p:outputLabel value="Age: #{studentDetailsBeans.age}"/> 
                           <p:outputLabel value=""/> 
                           <p:outputLabel value="Emergency Number:   "/> 
                            <p:outputLabel value=" #{studentDetailsBeans.clickedStudent.studentEmno}"/> 

                           </h:panelGrid>
                     </h:panelGrid>
                      <h:panelGrid columns="2" id="cyclegroup"> 
                    <p:outputLabel for="cycle" value="Cycle:" />
                     <p:selectOneRadio id="cycle" value="#{studentDetailsBeans.selectedCycle}" converter="cycconv"  >
                       <f:selectItems value="#{studentDetailsBeans.cycleListForStudent}" var="cyc" itemLabel="#{cyc.cycleNo}" itemValue="#{cyc}"/>
                       <p:ajax listener="#{studentDetailsBeans.changeCycle}"    update="timinggroup" process="cycle" />
                     </p:selectOneRadio>
                     </h:panelGrid>
                     <h:panelGrid columns="2" id="timinggroup"> 
                            <p:outputLabel for="course_8" value="Course Starting At 8" />  
                    <p:selectOneMenu id="course_8" value="#{studentDetailsBeans.selectedCourse8}" effect="fade"  converter="courseconv"  >  
                     <f:selectItem itemLabel="Select One" itemValue="" />  
                     <f:selectItems value="#{studentDetailsBeans.courseAt8}" var="c" itemValue="#{c}" itemLabel="#{c.courseName}" />  
                     <p:ajax update="timinggroup" listener="#{studentDetailsBeans.courseChangeListener()}"/>
                    </p:selectOneMenu>  
                     <p:outputLabel for="course_9" value="Course Starting At 9" />                          
                     <p:selectOneMenu id="course_9" value="#{studentDetailsBeans.selectedCourse9}" effect="fade"  converter="courseconv"  >  
                     <f:selectItem itemLabel="Select One" itemValue="" />  
                     <f:selectItems value="#{studentDetailsBeans.courseAt9}" var="c" itemValue="#{c}" itemLabel="#{c.courseName}" />  
                      <p:ajax  process="course_9" update="course_10" listener="#{studentDetailsBeans.courseChangeListener()}"/>
                     </p:selectOneMenu>  
                     <p:outputLabel for="course_10" value="Course Starting At 10" />                          
                     <p:selectOneMenu id="course_10" value="#{studentDetailsBeans.selectedCourse10}" effect="fade"  converter="courseconv" disabled="#{admissionFormBean.pd1}" >  
                     <f:selectItem itemLabel="Select One" itemValue="" />  
                     <f:selectItems value="#{studentDetailsBeans.courseAt10}" var="c" itemValue="#{c}" itemLabel="#{c.courseName}" />  
                      <p:ajax update="timinggroup" listener="#{studentDetailsBeans.courseChangeListener()}"/>
                     </p:selectOneMenu>  
                        <p:outputLabel for="course_11" value="Course Starting At 11" />                          
                        <p:selectOneMenu id="course_11" value="#{studentDetailsBeans.selectedCourse11}" effect="fade"  converter="courseconv"  >  
                     <f:selectItem itemLabel="Select One" itemValue="" />  
                     <f:selectItems value="#{studentDetailsBeans.courseAt11}" var="c" itemValue="#{c}" itemLabel="#{c.courseName}" />  
                     <p:ajax process="course_11" update="course_12" listener="#{studentDetailsBeans.courseChangeListener()}"/>
                        </p:selectOneMenu>  
                         <p:outputLabel for="course_12" value="Course Starting At 12" />                          
                         <p:selectOneMenu id="course_12" value="#{studentDetailsBeans.selectedCourse12}" effect="fade"  converter="courseconv" disabled="#{admissionFormBean.pd2}" >  
                     <f:selectItem itemLabel="Select One" itemValue="" />  
                     <f:selectItems value="#{studentDetailsBeans.courseAt12}" var="c" itemValue="#{c}" itemLabel="#{c.courseName}" />  
                     <p:ajax  update="timinggroup" listener="#{studentDetailsBeans.courseChangeListener()}"/>
                         </p:selectOneMenu>  
                         <p:commandButton value="ID Card" process="@this" actionListener="#{studentDetailsBeans.getIdCardPDF()}"/>

                     </h:panelGrid>

                     </h:form>

                 </p:dialog>   




                         <p:commandButton value="ID Card" process="@this" actionListener="#{studentDetailsBeans.getIdCardPDF()}"/>

this will fire my action listener and it is generating reports and write it in my system drive.But I am not getting the file as a downloadable item.

public void PDF(String name) throws JRException, IOException{  
       init();  
       HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();  
       httpServletResponse.addHeader("Content-disposition", "attachment; filename="+name+".pdf");  
       ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();  
       JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  
       System.out.println("All done the report is done");
       servletOutputStream.flush();
       servletOutputStream.close(); 
       FacesContext.getCurrentInstance().responseComplete();  
   }  

I am getting the report in my drive.Nothing will happen in my view. I still stand in the same dialogue.I am expecting a pdf download or direct print on clicking this command button.Please give your advice.I have read that ajax calls from P:dialogue will not be render the pdfs. I have implemented the report download in normal links .But i am unable to do it from the dialogue or using the command button.

Gustavo Meira
  • 2,875
  • 3
  • 21
  • 33
SoulStreamer
  • 21
  • 1
  • 3

3 Answers3

5

You should call responseComplete on the FacesContext, to signal to the JSF runtime that it should short-circuit the response lifecycle, handing over control to you

  public void PDF(String name) throws JRException, IOException{  
   init();  
   HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();  
   httpServletResponse.addHeader("Content-disposition", "attachment; filename="+name+".pdf");  

   FacesContext.getCurrentInstance().responseComplete();


   ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();  
   JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  
   System.out.println("All done the report is done");
   servletOutputStream.flush();
   servletOutputStream.close(); 
   FacesContext.getCurrentInstance().responseComplete();  
  }  

Also bear in mind that you cannot use an ajax request to generate a file download. A full HTTP request is required. What this means is that you must use ajax="false" on your <p:commandButton/> or use a <h:commandButton/>

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • The command button isn't firing the function at all when i try ajax = false or when i use the the h:command button. This button is in p dialogue which is dynamically laoded . So I am afraid if the action lsiteneres are binded properly or not. Even then the methods sysouts are not shown when i use ajax is false or when using the command button. But the page gets refreshed. Kindly advise. – SoulStreamer Jul 12 '14 at 13:16
  • @SoulStreamer - look in your browser's javascript console for clues as to why the method is not being executed. Incidentally, I also noticed you're not setting the mimetype for your file download. You should have `FacesContext.getCurrentInstance().getExternalContext().setResponseContentType("application/pdf")` – kolossus Jul 13 '14 at 05:39
  • -Could you please tell me how the action listeners are bind to the P;dialogues. Or how can i rebind the action listeners .But it works for ajax .So binding is not an issue?Do you have some tips on that? – SoulStreamer Jul 15 '14 at 13:17
  • @SoulStreamer - If a full page submit failed to execute, it's most likely because of a conversion or validation error. The ajax controls default to `execute="@this"`, localizing the submit. See this answer for how to use jasper reports in JSF http://stackoverflow.com/a/13696243/1530938 – kolossus Jul 19 '14 at 16:50
  • I have succesfully done this making the call from h:commandLink value="IdCardList" actionListener="#{studentReportBean.generateReport()}" and made sure that this command link is not inside a form. – SoulStreamer Jul 20 '14 at 13:52
  • @SoulStreamer, if it's not inside an ``, how is the component able to submit anything to the server :/? – kolossus Jul 20 '14 at 14:50
  • had same problem with jasper and ajax = false saved my life,,,,thank u – ckinfos Aug 09 '15 at 18:01
-1
JREmptyDataSource dataSource = new JREmptyDataSource();


                        JasperDesign desing = JRXmlLoader.load(this.getClass().getResourceAsStream("/com/po_life_report.jrxml"));
                        JasperReport report = JasperCompileManager.compileReport(desing);

                        JasperPrint jasperPrint = JasperFillManager.fillReport(report, item, dataSource);

                        response.setHeader("Content-Disposition", "inline; filename=report.pdf");
                        OutputStream outputStream = response.getOutputStream();
                        response.setContentType("application/pdf");

                        File f = File.createTempFile("test123", ".pdf");
                        JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(f));

                        byte[] bytes = Base64.encodeBase64(FileUtils.readFileToByteArray(f));// FileUtils.readFileToByteArray(f);// ;

                        byte[] output = new String(bytes, StandardCharsets.US_ASCII).getBytes();;

                        outputStream.write(output);
                        outputStream.flush();
                        outputStream.close();
Tharaka
  • 13
  • 1
  • 6
-3

use ajax="false" at your command button otherwise jasper report will not work.

umairnaqvi
  • 57
  • 6