1

In my current project, I am working on some Jasper reports. I have a report which has 2 sub reports in it. I am exporting it to pdf file. If I export a normal jasper report which doesn't have any subreports, the pdf file is working perfectly, when I have subreports, the pdf file is blank. Following is my code:

static String reportPath = "D:/Netbeans Projects/Abc/mail_reports/";
public static void getReport() {
    try {
        String reportName = reportPath + "AuctionSale/AuctionSeller/AuctionSeller.jasper";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sale_date", "2012-01-10");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportName, params, DB.getConn());
        OutputStream output = new FileOutputStream(new File("C:/JasperReport.pdf")); 
        JasperExportManager.exportReportToPdfStream(jasperPrint, output); 
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Can anybody tell me where did I miss or what do I have to make the report work.

Thanks

2 Answers 2

1

I could get the print from the following code:

String s = Dashboard.mail_seller.getSelectedItem().toString();
            String selected[] = s.split(" -- ");
            String seller_id = selected[0].trim();
            String filename = sale_date + "_" + selected[1].replaceAll(" ", "_").trim() + ".pdf";
            String reportName = reportPath + "AuctionSale/AuctionSellerMail/AuctionSeller.jasper";
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("sale_date", sale_date);
            params.put("seller_id", seller_id);
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportName, params, DB.getConn());
            OutputStream output = new FileOutputStream(new File("D:/Netbeans Projects/JDSons/mail_reports/"+filename+"")); 
            JasperExportManager.exportReportToPdfStream(jasperPrint, output); 
            output.flush();
            output.close();

Thanks guys

0

As you can see from this sample or from the full sample located in %jasperreports%\demo\samples\subreport folder (from JasperReports distribution package) you can pass the compiled subreport as parameter.

The sample java code:

JasperReport subreport = (JasperReport) JRLoader.loadObjectFromFile("build/reports/ProductReport.jasper");

Map parameters = new HashMap();
parameters.put("ProductsSubreport", subreport);

JasperFillManager.fillReportToFile("build/reports/MasterReport.jasper", parameters, getDemoHsqldbConnection());

The snippet from MasterReport jrxml file:

<parameter name="ProductsSubreport" class="net.sf.jasperreports.engine.JasperReport"/>
...
<detail>
    ...
        <subreport>
            <reportElement isPrintRepeatedValues="false" x="5" y="25" width="325" height="20" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
            <subreportParameter name="City">
                    <subreportParameterExpression><![CDATA[$F{City}]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <returnValue subreportVariable="PriceSum" toVariable="ProductTotalPrice" calculation="Sum"/>
            <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{ProductsSubreport}]]></subreportExpression>
        </subreport>
        <subreport>
            <reportElement positionType="Float" x="335" y="25" width="175" height="20" isRemoveLineWhenBlank="true" backcolor="#99ccff"/>
            <subreportParameter name="City">
                    <subreportParameterExpression><![CDATA[$F{City}]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <returnValue subreportVariable="REPORT_COUNT" toVariable="CityAddressCount"/>
            <subreportExpression class="java.lang.String"><![CDATA["AddressReport.jasper"]]></subreportExpression>
        </subreport>
</detail>
1
  • Hi Alex, I could get the output from the following code. Thanks a lot for your help: Please see it in my answer
    – Mujahid
    Commented Jan 17, 2012 at 5:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.