Weird Blank Report When Using JasperReport
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(); } }
10 Comments
cvaldex
about 8 years agoThank you...!!! You save me a lot of time reading posts...!!!
Replyedwin
about 8 years agoglad it can help, cvaldex :)
ron
about 8 years agoThanks A Lot, i got it before but was not clear. Thank you again.
Replyedwin
about 8 years agoHi Ron, glad it can help :)
Ayesh and Deya'
about 7 years agoThanks for sharing your experience
ReplyAshok Kumar
about 6 years agoHi 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
ReplyEd
about 6 years agoBless you sir, was trying to figure out why my report wasn't showing up but had no clue.
ReplyIsuru
about 5 years agoThanks man..... you just saved me a lot of time :)
ReplyEduardo
about 5 years agoThanks a lot !!!!
ReplyAliasgar Arif
about 3 years agoThanks. this post helped me a lot and saved lot of time
Reply