Sunday, October 5, 2008

Jasper Report Batch Printing for Multiple Page Reports

One of the things I've been trying to do for a few days now is to download multiple, dynamically generated, PDF reports as a single file. My problem was that each of the pages in this report were different and needed to use not only different data, but a different report template as well.

I looked at Jasper Reports, the defacto standard, but it seemed like I would only be able to create a multiple page PDF document that shared the same template. What's worse is that Jsaper Reports enforces a maximum height of 738 px in the detail section of the report. This meant I couldn't use page breaks to give each page its own unique look and feel.

After a little more reading, I discovered that Jasper Reports can generate PDF reports in "batch mode". Batch mode allows you to concatenate several reports together into a single File or OutputStream.

To use batch mode you must add your List of JasperPrint objects to the
JRExporterParameter.JASPER_PRINT_LIST parameter of the exporter.

Example:
ArrayList jasperPrintList = [Your logic for creating the list]

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

JRPdfExporter exporter = new JRPdfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);

exporter.exportReport();

inputStreamForStruts2 = new ByteArrayInputStream(outputStream.toByteArray());

4 comments:

Manoj said...

It works awesome thanks :) but one small help can i print next report from where the last printed report rather than printing in new page?

Unknown said...

Thanks!
It was very helpful.

Unknown said...

Monoj did u get solution for print next report from where the last printed report rather than printing in new page?

Madhavi said...

Hi,

I felt very happy after getting this code.
I too got a criteria where I need to print multiple barcodes in the same pdf file.
I have jasper file which will print one barcode with label.
I have applied a loop to print for multiple times and each time I am appending jasperprint to list.
I have used the above logic and executed successfully.
But nothing is coming on the pdf file. Its blank.
Am I doing anything wrong?

Code for printing one barcode label: (This code is working fine)
++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (templateStr != null) {
JasperPrint jasperPrint = JasperFillManager .fillReport(servletContext.getRealPath("/pages/reports/" + templateStr), parameters, new JREmptyDataSource());
response.setContentType("application/pdf"); JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code for printing multiple barcode labels:
=======================================================================================
ArrayList list = new ArrayList();

if (templateStr != null) {
for(int i=0;i<5;i++)
{
if(i>=1)
{ barcode1=barcode+"_"+i;
parameters.put("Barcode",barcode1);
}
JasperPrint jasperPrint = JasperFillManager
.fillReport(servletContext.getRealPath("/pages/reports/" + templateStr), parameters, new JREmptyDataSource());
System.out.println("Jasper print done");
list.add(jasperPrint);
System.out.println("Jasper print added to list");
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, list);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporter.exportReport();
}
======================================================================

Any help in this issue is appreciated.