Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JasperReports multi-page report with different content

I'm evaluating JasperReport and iReport, a requirement is the possibility to produce a multiple page report in which every page contains a different report.

Example:
Page 1 contains an actual invoice for a customer
Page 2 contains the invoices list for the customer
Page 3 contains a graph of amount of invoices by year
Page 4 contains only fixed text (say operator instructions ...)

Is it possible to create such a unique report instead of creating four standalone report and then merge the PDFs.

like image 908
Han Fastolfe Avatar asked Apr 08 '10 20:04

Han Fastolfe


People also ask

How do I make a two page jasper report?

To add a new page go to Ireport and "Add Report Group" ,by adding report group you can add as many pages you want. Save this answer.

How do I group data in jasper report?

right click on the report in the report inspector and choose Add Report Group. Follow the wizard, set as group name PledgeType and choose Group by the following report object where you select the field PledgeType. Click next. Check Add the group header and click Finish.


2 Answers

I tried something different:

I used Ireport 4.1.3 and if you right click on detail1 section you can add another detail section.

Add a break page and it's done.

like image 121
Ali Avatar answered Nov 15 '22 09:11

Ali


Yes you can by creating a dataSource and Parameters map for each sub report in your main report,

Data source contains the list that will be displayed as a table in the report

Parameters map contains keys and values of textfields in the report

The good news is that you can include all parameters of all your pages in each Parameters map then in report processing each page will extract its parameters and forgets others :)

Example :

List<Map<String, Object>> ParamList = new ArrayList<Map<String, Object>>();
List<JRDataSource> SourceList = new ArrayList<JRDataSource>();

Map<String, Object> params = new HashMap<String, Object>();
params.put("Page1_param1", "value1_1");
params.put("Page1_param2", "value1_2");
params.put("Page1_param3", "value1_3");
..
params.put("Page2_param1", "value2_1");
params.put("Page2_param2", "value2_2");
params.put("Page2_param3", "value2_3");
..
params.put("Page3_param1", "value3_1");
params.put("Page3_param2", "value3_2");
params.put("Page3_param3", "value3_3");
..
..
List listResult_1 = //select table sql for example
List listResult_2 = //select table sql for example
List listResult_3 = //select table sql for example

JRDataSource dataSource_1 = new ListOfArrayDataSource(
listResult_1, new String[] {"LastName", "FirstName", "address"});

JRDataSource dataSource_2 = new ListOfArrayDataSource(
listResult_2, new String[] {"LastName", "FirstName", "address"});

JRDataSource dataSource_3 = new ListOfArrayDataSource(
listResult_3, new String[] {"LastName", "FirstName", "address"});

// Yes i know ! :D , we put the same params list then as i said befor every sub report will take its own parameters values, so don't worry about this task ;)
ParamList.add(params);
ParamList.add(params);
ParamList.add(params);

SourceList.add(dataSource_1);
SourceList.add(dataSource_2);
SourceList.add(dataSource_3);

File reportFile = // the jrxml file template of the report

// We can use also a list of reportFile, so that every page uses his own template :D


CreateReport(jasperReport, ParamList, SourceList);

}

Now we create each sub report and append it to the main report :

Public void CreateReport(File reportFile, List<Map<String, Object>> ParamList,  List<JRDataSource> SourceList){

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
    Map<String, Object> parameters = paramList.get(0);
    JRDataSource datasource = datasourceList.get(0);
    jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource);

    if(paramList.size()>1){
        for(int i=1; i < paramList.size(); i++)
        {
            JasperPrint jasperPrint_next = JasperFillManager.fillReport(jasperReport, paramList.get(i), datasourceList.get(i));
             List pages = jasperPrint_next.getPages();
             for (int j = 0; j < pages.size(); j++) {
               JRPrintPage object = (JRPrintPage) pages.get(j);
               jasperPrint.addPage(object);
             }
        }
    }

}
like image 44
Shessuky Avatar answered Nov 15 '22 09:11

Shessuky