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.

Google+

9 Comments

cvaldex

about 4 years ago

Thank you...!!! You save me a lot of time reading posts...!!!

Reply

edwin

about 4 years ago

glad it can help, cvaldex :)

ron

about 4 years ago

Thanks A Lot, i got it before but was not clear. Thank you again.

Reply

edwin

about 4 years ago

Hi Ron, glad it can help :)

Ayesh and Deya'

about 3 years ago

Thanks for sharing your experience

Reply

Ashok Kumar

about 2 years ago

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

Reply

Ed

about 2 years ago

Bless you sir, was trying to figure out why my report wasn't showing up but had no clue.

Reply

Isuru

about 2 years ago

Thanks man..... you just saved me a lot of time :)

Reply

Eduardo

about 1 year ago

Thanks a lot !!!!

Reply

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)