使用Node-Jasper包在Node.js中生成报告
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Node-Jasper包在Node.js中生成报告相关的知识,希望对你有一定的参考价值。
我正在尝试使用node.js中的node-jasper包生成报告。当我编写没有类实现的代码时,代码工作正常以下是可行的代码。
jasper = require('node-jasper')({
path: '../lib/jasperreports-5.6.0',
reports: {
hw: {
jasper: '../jasperFiles/NodeIreportTest.jasper'
}
},
drivers: {
mysql: {
path: '../lib/com.mysql.jdbc_5.1.5.jar',
class: 'com.mysql.jdbc.Driver', //odbc driver//sun.jdbc.odbc.JdbcOdbcDriver //mysqlDriver// com.mysql.jdbc.Driver
type: 'mysql'
}
},
conns: {
dbserver1: {
host: '192.168.1.164',
port: 3306,
dbname: 'dbname',
user: 'user',
pass: '1234',
driver: 'mysql'
}
},
defaultConn: 'dbserver1'
});
以下是我调试代码时找到的完整对象。具有(hm)财产。
但是,当我试图介绍类并使代码标准我得到和错误TypeError: self.hm is not a constructor
以下是类实现相同的代码
class ReportModel {
constructor(strJasperPath, objDataObject, intClientIP) {
this.strJasperPath = strJasperPath;
this.objDataObject = objDataObject;
this.intClientIP = intClientIP;
}
makeReport() {
return new Promise ((resolve, reject) => {
jasper = require('node-jasper')({
path: '../lib/jasperreports-5.6.0',
reports: {
hw: {
//jasper: '../jasperFiles/NodeIreportTest.jasper'
jasper: '../jasperFiles/' + this.strJasperPath + '.jasper' //path to report
}
},
drivers: {
mysql: {
path: '../lib/com.mysql.jdbc_5.1.5.jar',
class: 'com.mysql.jdbc.Driver', //odbc driver//sun.jdbc.odbc.JdbcOdbcDriver //mysqlDriver// com.mysql.jdbc.Driver
type: 'mysql'
}
},
conns: {
dbserver1: {
host: '192.168.1.164',
port: 3306,
dbname: 'dbname',
user: 'user',
pass: '1234',
driver: 'mysql'
}
},
defaultConn: 'dbserver1'
});
var report = {
report: 'hw',
data: this.objDataObject
}
var pdf = jasper.pdf(report);
var strReportName = this.strJasperPath + intClientIP + '.pdf'
var filepath = "./reports/" + strReportName
var filename = {
"filepath": serverConfig.reporthost + path.basename(filepath)
};
//check if file is present or not
fs.access(filepath, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
//file is not present
fs.writeFile("./reports/" + strReportName, new Buffer(pdf, 'binary'), (err) => {
if (err) {
reject(err);
}
resolve(filename);
});
} else {
//file is present then delete it and then create it
fs.unlink(filepath, (err) => {
if (err) {
reject(err);
} else {
fs.writeFile("./reports/" + strReportName, new Buffer(pdf, 'binary'), (err) => {
if (err) {
console.log('WriteFile', err)
}
resolve(filename);
});
}
});
}
});
});
}
}
以下是没有具有类实现的hm属性的对象。
答案
步骤1)从类体中删除您的Jasper声明,并从下面的代码中删除hw对象
jasper = require('node-jasper')({
path: '../lib/jasperreports-5.6.0',
reports: {
// hw removed from this place
},
drivers: {
mysql: {
path: '../lib/com.mysql.jdbc_5.1.5.jar',
class: 'com.mysql.jdbc.Driver', //odbc driver//sun.jdbc.odbc.JdbcOdbcDriver //mysqlDriver// com.mysql.jdbc.Driver
type: 'mysql'
}
},
conns: {
dbserver1: {
host: '192.168.1.164',
port: 3306,
dbname: 'node_incrency_minpcs',
user: 'RAHUL',
pass: '1234',
driver: 'mysql'
}
},
defaultConn: 'dbserver1'
});
步骤2)在构造函数中添加hw对象的声明,如下所示
constructor(strReportName) {
jasper.reports = {
hw: {
jasper: '../jasperFiles/' + strReportName+ '.jasper'
}
}
}
步骤3)将其他参数添加到makeReport
函数作为makeReport(strJasperPath, objDataObject, intClientIP)
的函数体中
希望这对你有用。
以上是关于使用Node-Jasper包在Node.js中生成报告的主要内容,如果未能解决你的问题,请参考以下文章
在 Windows 中生成 python ENOENT node.js