2019-10-24 05:41:54 +00:00
|
|
|
const fs = require('fs');
|
2019-01-24 14:03:01 +00:00
|
|
|
const pdf = require('html-pdf');
|
2019-10-31 11:43:04 +00:00
|
|
|
const path = require('path');
|
2019-10-24 05:41:54 +00:00
|
|
|
const config = require('./config');
|
2019-10-29 06:46:44 +00:00
|
|
|
const Component = require('./component');
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
if (!process.env.OPENSSL_CONF)
|
|
|
|
process.env.OPENSSL_CONF = '/etc/ssl/';
|
|
|
|
|
2019-10-29 06:46:44 +00:00
|
|
|
class Report extends Component {
|
2019-10-24 05:41:54 +00:00
|
|
|
constructor(name, args) {
|
2019-10-29 06:46:44 +00:00
|
|
|
super(name);
|
|
|
|
|
2019-10-24 05:41:54 +00:00
|
|
|
this.args = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
get path() {
|
2019-10-29 06:46:44 +00:00
|
|
|
return `../templates/reports/${this.name}`;
|
2019-10-24 05:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async toPdfStream() {
|
|
|
|
const template = await this.render();
|
|
|
|
let options = config.pdf;
|
|
|
|
|
|
|
|
const optionsPath = `${this.path}/options.json`;
|
2019-10-31 11:43:04 +00:00
|
|
|
const fullPath = path.resolve(__dirname, optionsPath);
|
|
|
|
if (fs.existsSync(fullPath))
|
2019-10-24 05:41:54 +00:00
|
|
|
options = Object.assign(options, require(optionsPath));
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
pdf.create(template, options).toStream((err, stream) => {
|
|
|
|
resolve(stream);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Report;
|