Yesterday, ive found a very weird error when im trying to print my report to pdf using jasper report. Somehow it show a blank report when i’m printing my report and my log file shows no error at all despite there is nothing on my pdf file. I’m using a simple servlet and jasper report classes to generate my pdf files and show it on browser.
This is what my report looks like
And this is part of my sourcecode
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set header as pdf response.setContentType("application/pdf"); // set input and output stream ServletOutputStream servletOutputStream = response.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileInputStream fis; BufferedInputStream bufferedInputStream; String nama = ""; String alamat = ""; try { if(request.getParameter("nama")!=null) nama = request.getParameter("nama"); if(request.getParameter("alamat")!=null) alamat = request.getParameter("alamat"); // get report location ServletContext context = getServletContext(); String reportLocation = context.getRealPath("WEB-INF"); // get report fis = new FileInputStream(reportLocation + "/report/LaporanSatu.jasper"); bufferedInputStream = new BufferedInputStream(fis); // fill parameters Map<String, Object> map = new HashMap<String, Object>(); map.put("nama", "%"+nama+"%"); map.put("alamat", alamat); // export to pdf JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map); JasperExportManager.exportReportToPdfStream(jasperPrint, baos); response.setContentLength(baos.size()); baos.writeTo(servletOutputStream); // close it fis.close(); bufferedInputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally{ servletOutputStream.flush(); servletOutputStream.close(); baos.close(); } }
Well the workaround is actually very easy, i only need to add a new empty datasource (JREmptyDataSource) in JasperFillManager parameters
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set header as pdf response.setContentType("application/pdf"); // set input and output stream ServletOutputStream servletOutputStream = response.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileInputStream fis; BufferedInputStream bufferedInputStream; String nama = ""; String alamat = ""; try { if(request.getParameter("nama")!=null) nama = request.getParameter("nama"); if(request.getParameter("alamat")!=null) alamat = request.getParameter("alamat"); // get report location ServletContext context = getServletContext(); String reportLocation = context.getRealPath("WEB-INF"); // get report fis = new FileInputStream(reportLocation + "/report/LaporanSatu.jasper"); bufferedInputStream = new BufferedInputStream(fis); // fill parameters Map<String, Object> map = new HashMap<String, Object>(); map.put("nama", "%"+nama+"%"); map.put("alamat", alamat); // export to pdf JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JREmptyDataSource()); JasperExportManager.exportReportToPdfStream(jasperPrint, baos); response.setContentLength(baos.size()); baos.writeTo(servletOutputStream); // close it fis.close(); bufferedInputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally{ servletOutputStream.flush(); servletOutputStream.close(); baos.close(); } }
Thank you…!!! You save me a lot of time reading posts…!!!
glad it can help, cvaldex 🙂
Thanks A Lot, i got it before but was not clear. Thank you again.
Hi Ron,
glad it can help 🙂
Thanks for sharing your experience
Hi
Can u help me with providing an example for using the jrxml in this.
Bcoz i can’t able to print anything in the pdf with jrxml file, but will using servlets its working fine.
Thanks in advance
Bless you sir, was trying to figure out why my report wasn’t showing up but had no clue.
Thanks man….. you just saved me a lot of time 🙂
Thanks a lot !!!!
Thanks. this post helped me a lot and saved lot of time