Home » Java » Jasper report exports empty data in PDF format when there is more data

Jasper report exports empty data in PDF format when there is more data

Posted by: admin December 28, 2021 Leave a comment

Questions:

I have a report to be exported in excel, pdf and word using jasper reports. I use xml file as the DataSource for the report, but when the data increases jasper report exports empty file in only for PDF format, when i reduce the data content it export the data available correctly. is there any limitation to pdf size? , how can we manage the size in jasper reports from java?

My jrxml is really big, so i cannot add it here, i have added my java code which i use to export the content:

JRAbstractExporter exporter = null;
if (format.equals("pdf")) {
    exporter = new JRPdfExporter();
    jasperPrint.setPageWidth(Integer.parseInt(pWidth));
} else if (format.equals("xls")) {
    exporter = new JRXlsExporter();

} else if (format.equals("doc")) {
    jasperPrint.setPageWidth(Integer.parseInt(pWidth));
}
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
        jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
        outputStream_);
exporter.exportReport();
contents = outputStream_.toByteArray();
response.setContentType("application/" + format);
response.addHeader("Content-disposition",
        "attachment;filename=" + name.toString() + "." + format);
Answers:

When there is no date source you can try this code

       jasperReport = JasperCompileManager.compileReport(sourceFileName);
       jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter,new JREmptyDataSource()); 
       JasperExportManager.exportReportToPdfFile(jasperPrint, "D://.pdf")

Even if you do not have any data source and its a static data report giving JREmptyDataSource is important, otherwise you may get a blank report.

Check this link for more info https://stackoverflow.com/a/5334415/649451


Cheers!

###

Try setting the content length:

response.setContentLength(outputStream_.toByteArray().length)

See if this resolves your problem.

###

maybe you don´t have data defined in report.

try this code when generating report to avoid hiding sections when no data:

JasperReport jasperReport = (JasperReport)JRLoader.loadObject ("report1.jasper");
**jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);**

cheers!.

###

I had similar issue. For large number of records pdf was getting blank report.

This was solved by setting JRParameter.IS_IGNORE_PAGINATION to false.

###

A method that worked for me when dealing with big data and jasper reports is to first insert the data you need into a database table and then create a SQL statement on the jasper report that will join the table you created with the data you need. This puts more work on the database and less on the jasper library. Here is an example of a table that can be created to store report data.

CREATE TABLE RE_EMPLOYEE(
EMPLOYEE_ID INT UNIQUE NOT NULL
COMPANY_ID INT UNIQUE NOT NULL
REPORT_NAME VARCHAR(20)
UNIQUE_USER_ID INT UNIQUE NOT NULL
PRIMARY KEY (EMPLOYEE_ID, COMPANY_ID)
);