1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

How to send data to Jasper Report using Node js?

Discussão em 'StackOverflow' iniciado por fdantas, Setembro 2, 2020.

  1. fdantas

    fdantas Administrator Moderador

    I'm new to Jasper Reports and trying to connect jasper reports with my node js (express) application. For that, I have tried node-jasper node module & unfortunately, I'm unable to send data to jasper report because I couldn't understand how that mechanism works.

    I'll tell you what I have done in brief.

    1. First of all, I installed node-jasper module to my express project.
    2. Then I did everything as described in node-jasper Github page ('https://github.com/agmoyano/node-jasper'). I'm using the Progress OpenEdge database with JDBC. (I have no issue getting data from database)
    3. For creating the Jasper report I used Jasper Studio. By using that I have created employees.jrxml file and added that to the reports directory.
    4. But after starting the server, I got two errors calling that lib and dist folders are missing inside of the jasperreports-6.10.0 library that I have downloaded and put in the express project directory.
    5. Then I have created those two folders. Then I got an error called some jar files missing.
    6. For that, I downloaded all jar files for jasper reports version 6.10.0 from 'https://jar-download.com/artifacts/net.sf.jasperreports/jasperreports/6.10.0' website and put all those jar files in both lib and dist folders that I have created.
    7. Then that error was gone.
    8. But after I'm going to call '/pdf' rest endpoint, the report is created as employee.pdf but without any data. It's only an empty pdf without any content.

    Can any of you help me on how to send data to the jasper report and how to get a pdf output with all contents?

    Another question that I have to ask is, can we do SQL queries inside of the report object as described below instead of calling a dataset?

    expressApp.get('/pdf', function(req, res, next) {

    var report = {
    report: 'employee',
    data: {
    // id: parseInt(req.query.id, 10),
    -------- do a sql query to get data instead of passing json object ----------------------
    secundaryDataset: jasper.toJsonDataSource({
    data: [{ emp_name: "ABCD", nic: "V123456789" }]
    }, 'data')
    },

    --------- do a sql query to get dataset instead of passing json object ------------------
    dataset: [
    {
    emp_name: "ABCD",
    nic: "V123456789"
    },
    {
    emp_name: "EFGH",
    nic: "V987654321"
    }
    ]
    }

    var pdf = jasper.pdf(report);

    res.set({
    'Content-type': 'application/pdf',
    'Content-Length': pdf.length
    });
    res.send(pdf);
    });


    Also, what type of outputs can we generate by using node-jasper node module? (Ex: - pdf, csv, etc.)

    I have attached my index.js file content, employee.jrxml file content and some screenshots of the project directory structure.

    Thank You!

    injex.js file content

    var express = require('express');
    var bodyparser = require('body-parser');
    var cors = require('cors');
    var portNumber = require('./dependencies/dependencies');
    const oeConnection = require('./databaseHelpers/oeWrapper');
    var path = require('path');
    var fs = require('fs');

    var expressApp = express();

    var jasper = require('node-jasper')({
    path: path.join(__dirname + '/lib/jasperreports-6.10.0'),
    reports: {
    employee: {
    jrxml: path.join(__dirname + '/reports/employees.jrxml')
    }
    },
    drivers: {
    openedge: {
    path: path.join(__dirname + 'drivers/openedge.jar'),
    class: 'com.ddtek.jdbc.openedge.OpenEdgeDriver',
    type: 'openedge'
    }
    },
    conns: {
    dbserver1: {
    jdbc: 'jdbc:datadirect:eek:penedge://localhost:6400;databaseName=jasperdb',
    user: 'root',
    pass: 'root',
    }
    },
    defaultConn: 'dbserver1'
    });

    expressApp.use(cors());

    expressApp.use(bodyparser.json());

    expressApp.get('/pdf', function(req, res, next) {

    var report = {
    report: 'employee',
    data: {
    // id: parseInt(req.query.id, 10),
    secundaryDataset: jasper.toJsonDataSource({
    data: [{ emp_name: "ABCD", nic: "V123456789" }]
    }, 'data')
    },
    dataset: [
    {
    emp_name: "ABCD",
    nic: "V123456789"
    },
    {
    emp_name: "EFGH",
    nic: "V987654321"
    }
    ]
    }

    var pdf = jasper.pdf(report);

    res.set({
    'Content-type': 'application/pdf',
    'Content-Length': pdf.length
    });
    res.send(pdf);
    });

    expressApp.listen(portNumber.PORT, function(){

    });


    employee.jrxml content

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-
    cb8f9004be492ccc537180b49c026951f4220bf3 -->
    <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="employees" pageWidth="595"
    pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
    uuid="e477f8f4-274a-42e0-9da9-febb03865039">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="ProgressOE"/>
    <subDataset name="secundaryDataset" uuid="03225756-cc2d-4b65-a73e-6f232480ffa6">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="ProgressOE"/>
    <queryString language="JSON">
    <![CDATA[]]>
    </queryString>
    <field name="emp_name" class="java.lang.String">
    <property name="com.jaspersoft.studio.field.label" value="emp_name"/>
    </field>
    <field name="nic" class="java.lang.String">
    <property name="com.jaspersoft.studio.field.label" value="nic"/>
    </field>
    </subDataset>
    <queryString language="JSON">
    <![CDATA[]]>
    </queryString>
    <field name="emp_name" class="java.lang.String">
    <property name="com.jaspersoft.studio.field.label" value="emp_name"/>
    </field>
    <field name="nic" class="java.lang.String">
    <property name="com.jaspersoft.studio.field.label" value="nic"/>
    </field>
    <background>
    <band/>
    </background>
    <title>
    <band height="72">
    <frame>
    <reportElement mode="Opaque" x="-20" y="-20" width="595" height="92" backcolor="#006699"
    uuid="7e7561f7-a817-4d3d-bfc6-29865506aa24"/>
    <staticText>
    <reportElement x="20" y="20" width="234" height="43" forecolor="#FFFFFF"
    uuid="b48f6848-3160-4d1d-9a24-b6ccb7b7cb66"/>
    <textElement>
    <font size="34" isBold="true"/>
    </textElement>
    <text><![CDATA[Employee List]]></text>
    </staticText>
    <staticText>
    <reportElement x="395" y="43" width="180" height="20" forecolor="#FFFFFF"
    uuid="c2398724-6d3a-446e-8ad2-80f9fd52e52d"/>
    <textElement textAlignment="Right">
    <font size="14" isBold="false"/>
    </textElement>
    <text><![CDATA[Sample Report]]></text>
    </staticText>
    </frame>
    </band>
    </title>
    <pageHeader>
    <band height="13"/>
    </pageHeader>
    <columnHeader>
    <band height="21">
    <line>
    <reportElement x="-20" y="20" width="595" height="1" forecolor="#666666" uuid="bc1eb0ea-
    e8e7-4ebb-8779-4a2b3b7f00b8"/>
    </line>
    <staticText>
    <reportElement mode="Opaque" x="0" y="0" width="277" height="20" forecolor="#006699"
    backcolor="#E6E6E6" uuid="103315ad-2289-42e5-ae69-fbcaf92f7f20">
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="e665037b-a8ba-
    4c3e-b060-61296b94c1a6"/>
    </reportElement>
    <textElement textAlignment="Center">
    <font size="14" isBold="true"/>
    </textElement>
    <text><![CDATA[emp_name]]></text>
    </staticText>
    <staticText>
    <reportElement mode="Opaque" x="277" y="0" width="277" height="20" forecolor="#006699"
    backcolor="#E6E6E6" uuid="a9e3611e-49a4-4c36-ad6c-3db17853cb96">
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="15ed3288-7c9d-
    4795-9704-55b9fdccff62"/>
    </reportElement>
    <textElement textAlignment="Center">
    <font size="14" isBold="true"/>
    </textElement>
    <text><![CDATA[nic]]></text>
    </staticText>
    </band>
    </columnHeader>
    <detail>
    <band height="20">
    <line>
    <reportElement positionType="FixRelativeToBottom" x="0" y="19" width="555" height="1"
    uuid="46205314-fb81-4792-a256-7dcfbd0959f7"/>
    </line>
    <textField isStretchWithOverflow="true">
    <reportElement x="0" y="0" width="277" height="20" uuid="1dc6cba3-d034-4760-b08e-
    66c2b52e91a8">
    <!-- <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="e665037b-
    a8ba-4c3e-b060-61296b94c1a6"/> -->
    <!-- </reportElement>
    <textElement>
    <font size="14"/>
    </textElement> -->
    <textFieldExpression><![CDATA[$F{emp_name}]]></textFieldExpression>
    </textField>
    <textField isStretchWithOverflow="true">
    <reportElement x="277" y="0" width="277" height="20" uuid="a5a4adb0-350a-45b0-b646-
    20bc0f1dcd3c">
    <!-- <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="15ed3288-
    7c9d-4795-9704-55b9fdccff62"/> -->
    <!-- </reportElement>
    <textElement>
    <font size="14"/>
    </textElement> -->
    <textFieldExpression><![CDATA[$F{nic}]]></textFieldExpression>
    </textField>
    </band>
    </detail>
    <columnFooter>
    <band/>
    </columnFooter>
    <pageFooter>
    <band height="17">
    <textField>
    <reportElement mode="Opaque" x="0" y="4" width="515" height="13" backcolor="#E6E6E6"
    uuid="cdec6bec-da10-4969-b227-5cdf63e411f5"/>
    <textElement textAlignment="Right"/>
    <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
    </textField>
    <textField evaluationTime="Report">
    <reportElement mode="Opaque" x="515" y="4" width="40" height="13" backcolor="#E6E6E6"
    uuid="df97dff6-5734-49e5-a903-243cf69079d8"/>
    <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
    </textField>
    <textField pattern="EEEEE dd MMMMM yyyy">
    <reportElement x="0" y="4" width="100" height="13" uuid="b42d37dd-9ffe-4d28-9be5-
    2ce2e8b5d2e4"/>
    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
    </textField>
    </band>
    </pageFooter>
    <summary>
    <band/>
    </summary>
    </jasperReport>


    project structure: [​IMG]

    lib and dist folders in jasperreport-6.10.0 library: [​IMG]

    downloaded and added jar files in lib folder: [​IMG]

    downloaded and added jar files in dist folder: [​IMG]

    Continue reading...

Compartilhe esta Página