Jump to content
We've recently updated our Privacy Statement, available here ×

How to show Report preview in Web Application?


sandun.jaya

Recommended Posts

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...