Currently I'm using JasperReports to generate pdfs displaying a list of payments, one entity per page. Code for that (for one entity):
Map<String, Object> parameters = new HashMap<String, Object>();
JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(path + "WEB-INF/jasper/PaymentOrder.jasper");
report = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
What I need to do is to display two enitities in one file. I'm trying to use subreports to do that.
- How can I pass a Map with parameters to my subreport?
- And how to I change my subreport file (if needed) to get parameters from parent?
- How can I pass ResourceBundle from master report to subreport?
- How can I set the name of subreport?
Master report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<title>
<band height="531" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="555" height="20"/>
<textElement verticalAlignment="Middle">
<font fontName="Times New Roman" isUnderline="false" pdfEncoding="UTF-8" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA["* MASTER REPORT *"]]></textFieldExpression>
</textField>
<subreport runToBottom="false">
<reportElement positionType="Float" x="0" y="55" width="555" height="15" isPrintInFirstWholeBand="true" isPrintWhenDetailOverflows="true"/>
<dataSourceExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get( "subReport1Params" )]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get( "subReport1" )]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
Subreport:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<title>
<band height="531" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="555" height="20"/>
<textElement verticalAlignment="Middle">
<font fontName="Times New Roman" isUnderline="false" pdfEncoding="UTF-8" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}.getString( "user" ) + ": "+$P{REPORT_PARAMETERS_MAP}.get( "user" )]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="0" y="22" width="555" height="22"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Times New Roman" isUnderline="false" pdfEncoding="UTF-8" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}.getString( "payment" ) + " № "+$P{REPORT_PARAMETERS_MAP}.get( "docNumber" )+" "+$P{REPORT_RESOURCE_BUNDLE}.getString( "from" ) + " "+$P{REPORT_PARAMETERS_MAP}.get( "date" )]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
java code:
HashMap<String, Object> parameters = getParametersForPayment(doc1, user, locale);
String path = ((WebApplication) Application.get()).getServletContext().getRealPath("/");
File fileSubReport1 = new File(path + "WEB-INF/jasper/PaymentOrder.jasper");
parameters.put("subReport1", JRLoader.loadObject(fileSubReport1));
File f = new File(path + "WEB-INF/jasper/PaymentMaster.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(f);
report = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());