sandun.jaya Posted June 10, 2013 Share Posted June 10, 2013 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? Link to comment Share on other sites More sharing options...
Ankur Gupta Posted June 10, 2013 Share Posted June 10, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now