How to show Report preview in Web Application?

0

I'm using Jasper Report with JSF web Application.Everything is ok to my application.But I couldn't show report preview before exoprt as pdf,xls....

Cannot use Jasper Viewer because it is a Swing component.So how can I do it?

sandun.jaya's picture
Joined: Jun 9 2013 - 9:42pm
Last seen: 6 years 5 months ago

1 Answer:

0

Hi sandun.jaya,

 

Actually in a case which I came across is what I am doing is that in a web application , I am running and compiling the report and then providing the user , download link for the same. 

In you case what you can do is instead of the download link , you could show the same file in the window or as you desire 

 

the code snippets are as follows:

 

   // 1- export to PDF to the specified path
   String filePath1 = getServletContext().getRealPath("") + File.separator +"sample_report.pdf";
   JasperExportManager.exportReportToPdfFile(jasperPrint, filePath1);
 
   // 2- export to HTML to the specified path
   String filePath2 = getServletContext().getRealPath("") + File.separator +"sample_report.html";
   JasperExportManager.exportReportToHtmlFile(jasperPrint, filePath2 );
 
  // 3- export to XLS to the specified path
  String filePath3 = getServletContext().getRealPath("") + File.separator +"sample_report.xls";
  JRXlsExporter exporter = new JRXlsExporter();
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, filePath3 );
  exporter.exportReport();
 
Here I am generating the files in the build/web  folder . Then these files I have given choice to user to download.
 
The download link is generated as .
 
out.print("1.<a href='GetPDF'>Get PDF</a><br/>");
 
Here I have refered to GetPDF Servlet.The content of GetPDF servlet is :
 
                    // Gets the output Stream
                    stream = response.getOutputStream();
                    
                    /*sets response headers tell browser program going to return an application file instead of html page*/
                    response.setContentType("application/pdf"); // The mime type of the file should be taken care of 
                    response.setDateHeader("Expires", 0);
                    response.addHeader("Content-Disposition",
                     "attachment; filename=ReportExport.pdf");// File name should also be taken care of .
                    response.setContentLength((int) file.length());
                    
                    // Converts the file in input buffered Stream
                    buf = new BufferedInputStream(new FileInputStream(file));
                    int readBytes = 0;
                    
                    //copies binary contect to output stream
                    while ((readBytes = buf.read()) != -1)
                     stream.write(readBytes);
                } finally {
                     if (stream != null){
                                stream.flush();
                 }        
                stream.close();
                if (buf != null){
                                buf.close();
                        }        
              }
Hope This helps
 
Thanks,
Ankur

 

Ankur Gupta's picture
Joined: Jan 21 2013 - 10:36pm
Last seen: 2 months 1 week ago
Feedback
randomness