2

I have a problem with a JasperReports's report because I'm using a Spring-boot framework. Instead of the absolute path in a subreport expression tag, I need to pass a InputStream.

Hence, I add a parameter with this InputStream:

Map<String, Object> parameters = new HashedMap();
parameters.put("STREAM_SUBREPORT",classLoader.getResource(subreport_filename).openStream());

and in the jrxml a have the following subreport definition

<subreportParameter name="ItemDataSource">
    <subreportParameterExpression><![CDATA[$P{ItemDataSource}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{STREAM_SUBREPORT}]]></subreportExpression>

My Java Code is:

JasperDesign jd = null;
try {
    jd = JRXmlLoader.load(classLoader.getResource(REPORT_PATH).openStream());

} catch (IOException e) {
   ...
}
JasperReport report = JasperCompileManager.compileReport(jd);
JasperPrint print = JasperFillManager.fillReport(report, parameters, ds);

The definition of the subreport that I try to pass is

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-04-02T23:37:54 -->
<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="submissions_subreport_summary" pageWidth="595" pageHeight="842" whenNoDataType="BlankPage" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="408f425e-96dc-41a9-991e-2208c3c8a120">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w1" value="233"/>
    <property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w2" value="756"/>
    <parameter name="ItemDataSourceSummary" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="title" class="java.lang.String"/>
    <field name="details" class="java.lang.String"/>
    <title>
        <band height="15">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <staticText>
                <reportElement x="2" y="0" width="123" height="12" uuid="782e96a2-5db0-458a-9078-63d470b2e155">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
                <box>
                    <bottomPen lineWidth="1.0"/>
                </box>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Subreport Caption]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="35">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <textField>
                <reportElement x="0" y="0" width="280" height="30" uuid="6f92584d-1738-4d79-b169-2f544152341b"/>
                <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="280" y="0" width="270" height="30" uuid="579b2443-f865-4422-888d-d995ffdfbdc5"/>
                <textFieldExpression><![CDATA[$F{details}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

But, I got the exception by the JasperFillManager method,

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: $P{STREAM_SUBREPORT}

I try with the follow cast in the jrxml

<subreportExpression><![CDATA[(java.io.InputStream($P{DIR_SUBREPORT_SUBMISSION}))]]></subreportExpression>

but also got an exception

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. java.io cannot be resolved to a type
                value = (java.io.InputStream(((java.lang.String)parameter_STREAM_SUBREPORT.getValue()))); //$JR_EXPR_ID=11$

Any suggestions would be welcome!

henryr
  • 169
  • 1
  • 15
  • You should post the definition of *STREAM_SUBREPORT* parameter. – Alex K Apr 05 '17 at 14:45
  • Hi Alex, at the beginning of my post is defined like a parameter that recive an InputStream, i.e, parameters.put("STREAM_SUBREPORT",classLoader.getResource(subreport_filename).openStream()); – henryr Apr 05 '17 at 14:49
  • I mean definition at *jrxml* file – Alex K Apr 05 '17 at 14:49
  • Ah.. okk, I think that is it irrelevant, but, I will add it right now – henryr Apr 05 '17 at 14:51
  • 2
    BTW, Did you try the way from this post: [Generate Jasper report with subreport from java](http://stackoverflow.com/q/9785451/876298)? – Alex K Apr 05 '17 at 14:53
  • 2
    Your problem solved here: [How to load subreport resources with Jasper?](http://stackoverflow.com/a/35534680/876298) – Alex K Apr 05 '17 at 14:56
  • Alex, thank you very much for your help, this is my solution, before asking I did a search but maybe my keywords were not adequate. – henryr Apr 05 '17 at 15:39
  • Great to hear. You are welcome :). BTW, you can accept duplicate flag – Alex K Apr 05 '17 at 15:46

0 Answers0