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();
		}        
	}

This is the screenshot for my successful report.

10 thoughts on “Weird Blank Report When Using JasperReport”

  1. 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

Leave a Reply to Ayesh and Deya' Cancel Reply

Your email address will not be published.