如何使用subataset的json数组生成表jasper

时间:2018-06-14 09:15:36

标签: python json jasper-reports

我遇到了生成包含json数组所有元素的表的问题。

JSON就是:

{
    "clasificacion_resultados":{
        "otras_empresas":[
            {
                "URL":"http://aa.com",
                "nombre_documento":"aa.pdf"
            },
            {
                "URL":"https://bb.pdf",
                "nombre_documento":"bb.pdf"
            },
            {
                "URL":"http://www.ccc.pdf",
                "nombre_documento":"ccc.pdf"
            },
            {
                "URL":"http://www.ddd.pdf",
                "nombre_documento":"www.ddd.pdf"
            },
            {
                "URL":"http://www.eee.pdf",
                "nombre_documento":"www.eee.pdf"
            },
            {
                "URL":"https://www.fff.pdf",
                "nombre_documento":"www.fff.pdf"
            },
            {
                "URL":"http://www.ggg.pdf",
                "nombre_documento":"www.ggg.pdf"
            }
          ]
    }
 }

以下jrxml定义:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="Blank_Letter" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0682e178-ad1a-4d9c-8c89-daa66551172a">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="json_data_results"/>
    <subDataset name="Dataset1" uuid="4cf6215f-dfdc-4280-8fe1-89e581f2783b">
        <field name="details_other_organizations" class="java.lang.String">
            <fieldDescription><![CDATA[URL]]></fieldDescription>
        </field>
        <field name="details_other_organizations_2" class="java.lang.String">
            <fieldDescription><![CDATA[nombre_documento]]></fieldDescription>
        </field>
    </subDataset>
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="URL" class="java.lang.String">
        <fieldDescription><![CDATA[URL]]></fieldDescription>
    </field>
    <field name="nombre_documento" class="java.lang.String">
        <fieldDescription><![CDATA[nombre_documento]]></fieldDescription>
    </field>
    <detail>
        <band height="410" splitType="Stretch">
            <textField>
                <reportElement x="0" y="30" width="280" height="30" uuid="ad93247d-d7e3-45ff-841e-d9baf7682047"/>
                <box topPadding="0" leftPadding="0" bottomPadding="0" rightPadding="0">
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textFieldExpression><![CDATA[$F{URL}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement stretchType="ContainerBottom" x="280" y="30" width="280" height="30" uuid="07f04337-c4cb-4303-b348-1b035abf0add"/>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textFieldExpression><![CDATA[$F{nombre_documento}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="0" width="280" height="30" uuid="7ddb2dc9-394d-4cac-a91a-ce734012cf9a"/>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <text><![CDATA[URL]]></text>
            </staticText>
            <staticText>
                <reportElement x="280" y="0" width="280" height="30" uuid="acc3ddf5-4f00-4703-9e8a-be61bdaf8fa4"/>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <text><![CDATA[Nombre documento]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

我正在使用python pyreportJaspert库从json生成pdf,使用此函数:

def json_to_pdf():
    input_file = os.path.dirname(os.path.abspath(__file__)) + \
             '/examples/json_results.jrxml'

    output = os.path.dirname(os.path.abspath(__file__)) + '/output/_Result'
    json_query = 'clasificacion_resultados.otras_empresas'

    data_file = os.path.dirname(os.path.abspath(__file__)) + \
    '/examples/data.json'

    jasper = JasperPy()
        jasper.process(
        input_file,
        output_file=output,
        format_list=["pdf"],
        parameters={},
        db_connection={
            'data_file': data_file,
            'driver': 'json',
            'json_query': json_query,
        },
        locale='pt_BR'
    )

    print('Result is the file below.')
    print(output + '.pdf')

这个python函数的执行现在为每个json数组元素生成一个页面,而不是一个页面中的所有元素。

我想创建一个包含所有元素的表(“URL”和“nombre_document”)。我在stackoverflow中看起来看起来很相似,但如果我在列表中使用类似表达式的表达式

<datasetRun subDataset="listDataSet" uuid="71276e30-7777-44ae-b6d9-2087a4c51ca3">
   <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("Dataset1")]]></dataSourceExpression>
</datasetRun>`

python中函数的结果是input file: "/home/xoan/Documentos/pyreportjasper/examples/Blank_Letter.jrxml" is not a valid jrxml file

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

  • 如果您真的必须使用表格组件,则应为表格dataSourceExpression设置以下datasetRun

    ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("clasificacion_resultados.otras_empresas")
    

    并删除主数据集中的字段和关联的textFields,因为它们不会映射到任何内容。

  • 但毕竟你可能不需要表组件。使用您当前的JRXML:

    • 删除数据集 subDataset以获得更清晰的文件
    • 设置正确的queryString

      <queryString language="json">
          <![CDATA[clasificacion_resultados.otras_empresas]]>
      </queryString>
      
    • 创建列标题乐队
    • 将静态文本元素从detail范围移动到您刚刚创建的columnHeader,以便它们不会为每条记录重复
    • 缩小细节带的高度以包裹textField元素

非表解决方案的最终JRXML可能如下所示:

<?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="Blank_Letter" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0682e178-ad1a-4d9c-8c89-daa66551172a">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="json_data_results"/>
    <queryString language="json">
        <![CDATA[clasificacion_resultados.otras_empresas]]>
    </queryString>
    <field name="URL" class="java.lang.String">
        <fieldDescription><![CDATA[URL]]></fieldDescription>
    </field>
    <field name="nombre_documento" class="java.lang.String">
        <fieldDescription><![CDATA[nombre_documento]]></fieldDescription>
    </field>
    <columnHeader>
        <band height="30">
            <staticText>
                <reportElement x="280" y="0" width="280" height="30" uuid="acc3ddf5-4f00-4703-9e8a-be61bdaf8fa4"/>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <text><![CDATA[Nombre documento]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="0" width="280" height="30" uuid="7ddb2dc9-394d-4cac-a91a-ce734012cf9a"/>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <text><![CDATA[URL]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="280" height="30" uuid="ad93247d-d7e3-45ff-841e-d9baf7682047"/>
                <box topPadding="0" leftPadding="0" bottomPadding="0" rightPadding="0">
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textFieldExpression><![CDATA[$F{URL}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement stretchType="ContainerBottom" x="280" y="0" width="280" height="30" uuid="07f04337-c4cb-4303-b348-1b035abf0add"/>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textFieldExpression><![CDATA[$F{nombre_documento}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>