2019-10-24 05:41:54 +00:00
|
|
|
const fs = require('fs');
|
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');
|
2022-10-19 12:30:36 +00:00
|
|
|
const Cluster = require('./cluster');
|
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
|
|
|
}
|
|
|
|
|
2022-01-05 13:48:21 +00:00
|
|
|
async getName() {
|
|
|
|
return (await this.getUserLocale())['reportName'];
|
|
|
|
}
|
|
|
|
|
2019-10-24 05:41:54 +00:00
|
|
|
async toPdfStream() {
|
|
|
|
const template = await this.render();
|
2020-07-06 06:27:05 +00:00
|
|
|
const defaultOptions = Object.assign({}, config.pdf);
|
2019-10-24 05:41:54 +00:00
|
|
|
|
|
|
|
const optionsPath = `${this.path}/options.json`;
|
2019-10-31 11:43:04 +00:00
|
|
|
const fullPath = path.resolve(__dirname, optionsPath);
|
2020-07-06 06:27:05 +00:00
|
|
|
let options = defaultOptions;
|
2019-10-31 11:43:04 +00:00
|
|
|
if (fs.existsSync(fullPath))
|
2020-07-06 06:27:05 +00:00
|
|
|
options = require(optionsPath);
|
2020-07-02 14:07:26 +00:00
|
|
|
|
2023-04-04 13:00:28 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-10-19 12:30:36 +00:00
|
|
|
Cluster.pool.queue({}, async({page}) => {
|
2023-04-04 13:00:28 +00:00
|
|
|
try {
|
|
|
|
await page.emulateMediaType('screen');
|
|
|
|
await page.setContent(template);
|
|
|
|
|
|
|
|
const element = await page.$('#pageFooter');
|
|
|
|
|
|
|
|
let footer = '\n';
|
|
|
|
if (element) {
|
|
|
|
footer = await page.evaluate(el => {
|
|
|
|
const html = el.innerHTML;
|
|
|
|
el.remove();
|
|
|
|
return html;
|
|
|
|
}, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.headerTemplate = '\n';
|
|
|
|
options.footerTemplate = footer;
|
|
|
|
|
2024-05-21 21:58:25 +00:00
|
|
|
const buffer = await page.content();
|
2023-04-04 13:00:28 +00:00
|
|
|
resolve(buffer);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
2022-10-19 12:30:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-10-24 05:41:54 +00:00
|
|
|
}
|
2022-01-05 13:48:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the params that ends with id
|
|
|
|
*
|
|
|
|
* @return {array} List of identifiers
|
|
|
|
*/
|
|
|
|
getIdentifiers() {
|
|
|
|
const identifiers = [];
|
|
|
|
const args = this.args;
|
|
|
|
const keys = Object.keys(args);
|
|
|
|
|
|
|
|
for (let arg of keys) {
|
|
|
|
if (arg.endsWith('Id'))
|
|
|
|
identifiers.push(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return identifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFileName() {
|
|
|
|
const args = this.args;
|
|
|
|
const identifiers = this.getIdentifiers(args);
|
|
|
|
const name = await this.getName();
|
|
|
|
const params = [];
|
|
|
|
params.push(name);
|
|
|
|
|
|
|
|
for (let id of identifiers)
|
|
|
|
params.push(args[id]);
|
|
|
|
|
|
|
|
const fileName = params.join('_');
|
|
|
|
|
|
|
|
return `${fileName}.pdf`;
|
|
|
|
}
|
2019-10-24 05:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Report;
|