简体   繁体   English

使用Java在运行时编译jasperreport子报表

[英]Compile jasperreport subreports at runtime in java

I have a java rest service that returns a generated PDF out of a jasper file. 我有一个Java Rest服务,它从jasper文件中返回生成的PDF。 For some bussiness rules, the compiled .jasper files will not be on the service, only the .jxrml files. 对于某些业务规则,编译的.jasper文件将不在服务上,而仅在.jxrml文件上。 I compile the report at runtime using this line 我在运行时使用此行编译报告

JasperReport report = JasperCompileManager.compileReport(fileName);

This is the original section of the subreport with the reference to the compiled .jasper file 这是子报表的原始部分,其中引用了已编译的.jasper文件

        <subreport isUsingCache="false">
            <reportElement x="57" y="30" width="498" height="15" isRemoveLineWhenBlank="true" uuid="5b26c250-7d4d-4ddb-9fdc-834a98fc6eac"/>
            <subreportParameter name="fecha">
                <subreportParameterExpression><![CDATA[$F{recorridodato_Fecha}]]></subreportParameterExpression>
            </subreportParameter>
            <subreportParameter name="idRuta">
                <subreportParameterExpression><![CDATA[$F{ruta_RutaId}.longValue()]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]]></subreportExpression>
        </subreport>

This is what I read somewhere (I can't remember where) that I should do 这就是我应该读的地方(我不记得在哪里)

        <subreport isUsingCache="false">
            <reportElement x="57" y="30" width="498" height="15" isRemoveLineWhenBlank="true" uuid="5b26c250-7d4d-4ddb-9fdc-834a98fc6eac"/>
            <subreportParameter name="fecha">
                <subreportParameterExpression><![CDATA[$F{recorridodato_Fecha}]]></subreportParameterExpression>
            </subreportParameter>
            <subreportParameter name="idRuta">
                <subreportParameterExpression><![CDATA[$F{ruta_RutaId}.longValue()]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA[JasperCompileManager.compileReport(getClass().getResource($P{SUBREPORT_DIR} + "subreport.jrxml").openStream())]]></subreportExpression>
        </subreport>

The second options works when the report is called from within iReport. 从iReport中调用报告时,第二个选项有效。 Howhever, it fails when called from the service. 但是,从服务调用时失败。 The generated PDF does not print the subreport data. 生成的PDF不打印子报表数据。 The first option works OK both on iReport and on the REST service call, but it references the compiled .jasper file, which will not be accesible. 第一个选项在iReport和REST服务调用上都可以正常运行,但是它引用了已编译的.jasper文件,该文件将不可访问。 So I need to generate the global report in Java by referencing the .jrxml subreport path only (not the .jasper file) I am using these maven references 因此,我需要仅通过引用.jrxml子报表路径(而不是.jasper文件)来生成Java中的全局报告,而我正在使用这些Maven引用

                <!-- Jasper-->
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>5.6.0</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.12</version>
    </dependency>

I found out a way thanks to this post 感谢这篇文章,我找到了一种方法

Jasper Reports: How to compile subreports Jasper报告:如何编译子报告

Basically it involves reading the jrxml file to detect the subreports and compile them to the physical path the .jasper should be. 基本上,它涉及读取jrxml文件以检测子报表并将其编译为.jasper应该是的物理路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM