Thursday, November 27, 2014

Jasper Reports, 1kb PDFs and "I swear they did this on purpose"

I ran into this seemingly weird situation with Jasper Reports 5.0.1 where I was getting 1kb size PDFs which was weird. So after doing *lots* (lots) of debugging I couldn't find anything wrong. The lines where pretty straight forward:

  InputStream is1 = evalSummaryReportPage1.getResource().openStream(); //jrxml source
  Map reportParams1 = new HashMap(); // standard hashmap
  reportParams1.put(......)
  ..
  ..
  JasperDesign page1Design = JRXmlLoader.load(is1);
  JasperReport page1Compiled = JasperCompileManager.compileReport(page1Design);
  JasperPrint page1 = JasperFillManager.fillReport(page1Compiled, reportParams1); 

  JRPdfExporter exporter = new JRPdfExporter();
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, page1);
  exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, filename); 
  exporter.exportReport();

Until I found about JREmptyDataSource class. Apparently the fillReport() method of the JasperFillManager doesn't do shit when it doesn't has a third or the datasource parameter. It was bullshit because the same fillReport methods is overloaded and can be called with only two parameters. You can then compile with no problems or warnings. A warning would have been nice but no. I swear the Jasper devs did this on purpose just to fuck with me.

So to fix this shit:
  ..
  JasperDesign page1Design = JRXmlLoader.load(is1);
  JasperReport page1Compiled = JasperCompileManager.compileReport(page1Design);
  JasperPrint page1 = JasperFillManager.fillReport(page1Compiled, reportParams1, new JREmptyDataSource()); 
  ..
There, it now correctly generates the report.

zzzzzzzzz