如何使用 node-jasper 将数据发送到 table 组件?

How to send data to table component using node-jasper?

我的任务是使用 nodeJS 生成 PDF,所以我正在使用 node-jasper 库。 我尝试显示一个 table 组件,但是当我从后端发送数据集时,它一直显示为空 table。 这是我的 nodjs 代码:

const express = require('express')

const app = express()
const port = 5000

    
var jasper = require('node-jasper')({
    path: './lib/jasperreports-6.10.0',
    reports: {
        hw: {
            jasper: './jasperFiles/repportBI.jasper',
            jrxml: './reports/repportBI.jrxml',
            conn: 'in_memory_json'
        }
    },
});


app.get('/jasper1', (req, res) => {

    var report = {
            report: 'hw',
            data: {
                title: "My test report",
                Dataset1: jasper.toJsonDataSource(
                            {
                                dados: [
                                    {
                                        code: 'e1',
                                        name: 'raouf',
                                        function: 'Developer'
                                    },
                                    {
                                        code: 'e2',
                                        name: 'djamel',
                                        function: 'Developer'
                                    },
                                    {
                                        code: 'e3',
                                        name: 'omar',
                                        function: 'Medecine'
                                    }
                                ]
                            },
                            'dados' 
                        ),
            }   
            ,
                dataset: { }
           
        }
    var pdf = jasper.pdf(report);
    res.set({
        'Content-type': 'application/pdf',
        'Content-Length': pdf.length
    });
    res.send(pdf)
});

app.listen(port, () => {
    console.log('app is runing on port: ', port)
})

这是我的 jrxml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown  -->
<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="repportBI" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="95744304-ed70-4001-8031-6cec31c7acbf">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Dataset1" uuid="70370722-01b1-41a5-a681-3d6a28fa2f84">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="code" class="java.lang.String"/>
        <field name="name" class="java.lang.String"/>
        <field name="function" class="java.lang.String"/>
    </subDataset>
    <parameter name="title" class="java.lang.String"/>
    <parameter name="datasetTable1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="code" class="java.lang.String"/>
    <field name="name" class="java.lang.String"/>
    <field name="function" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="90" splitType="Stretch">
            <textField>
                <reportElement x="204" y="6" width="147" height="60" uuid="4fc37721-d03e-4f94-9df8-355d2c2ed4e3"/>
                <textElement textAlignment="Center">
                    <font size="26" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="354" splitType="Stretch">
            <componentElement>
                <reportElement x="72" y="154" width="200" height="200" uuid="99067366-d9e3-48e1-95b3-a4d8c1e8a1db">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" whenNoDataType="AllSectionsNoDetail">
                    <datasetRun subDataset="Dataset1" uuid="7f03ad97-fa43-4b82-94c5-19a674ac7d7e">
                        <dataSourceExpression><![CDATA[$P{datasetTable1}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="66" uuid="40fb6564-12c3-4174-955c-23d808832359">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Colonne1"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="a6e64620-475b-4cd5-a3f6-d7dd711b2ee2"/>
                                <text><![CDATA[code]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="a7d15e3a-2452-4f80-8c20-7fbcc8181fe0"/>
                                <textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="66" uuid="ba0efa7e-7795-4fd2-9b71-b39f9fdcb466">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Colonne2"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="a694b15b-401f-4e8d-a726-ac8e6ddcaf05"/>
                                <text><![CDATA[name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="b0fe47b1-8a5c-4df3-8851-6e89da8bfc3a"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="66" uuid="d0dc532f-c2e6-4ff4-8b3e-7a8c8ab0a748">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Colonne3"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="32797f1c-a862-4e5f-b624-e1f19af58277"/>
                                <text><![CDATA[function]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="ba092f73-109d-4160-b7dc-c664e83366c4"/>
                                <textFieldExpression><![CDATA[$F{function}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

生成的PDF: enter image description here

我找不到问题出在哪里,你能帮我吗?

这里有两个问题。

首先是report.data中的参数名称(即Dataset1)与报表中的参数名称(即datasetTable1)不匹配。所以你需要在 JavaScript 代码中使用它:

var report = {
        report: 'hw',
        data: {
            title: "My test report",
            datasetTable1: jasper.toJsonDataSource(
            ...

那么,报表中的datasetTable1参数的类型是net.sf.jasperreports.engine.data.JRBeanCollectionDataSource,而jasper.toJsonDataSource产生的是net.sf.jasperreports.engine.data.JsonDataSource对象。您需要将参数类型设置为 JsonDataSource 或像这样更通用的类型:

<parameter name="datasetTable1" class="net.sf.jasperreports.engine.JRDataSource"/>