salix/print/core/report.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

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');
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();
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);
let options = defaultOptions;
2019-10-31 11:43:04 +00:00
if (fs.existsSync(fullPath))
options = require(optionsPath);
2020-07-02 14:07:26 +00:00
2020-09-14 09:44:10 +00:00
const page = (await config.browser.pages())[0];
2020-08-25 08:00:21 +00:00
await page.emulateMedia('screen');
2020-07-02 14:07:26 +00:00
await page.setContent(template);
const element = await page.$('#pageFooter');
let footer = '\n';
2020-07-02 14:07:26 +00:00
if (element) {
footer = await page.evaluate(el => {
const html = el.innerHTML;
el.remove();
return html;
}, element);
}
options.headerTemplate = '\n';
options.footerTemplate = footer;
const buffer = await page.pdf(options);
return buffer;
2019-10-24 05:41:54 +00:00
}
}
module.exports = Report;