60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
const fs = require('fs');
|
|
const puppeteer = require('puppeteer');
|
|
const path = require('path');
|
|
const config = require('./config');
|
|
const Component = require('./component');
|
|
|
|
if (!process.env.OPENSSL_CONF)
|
|
process.env.OPENSSL_CONF = '/etc/ssl/';
|
|
|
|
class Report extends Component {
|
|
constructor(name, args) {
|
|
super(name);
|
|
|
|
this.args = args;
|
|
}
|
|
|
|
get path() {
|
|
return `../templates/reports/${this.name}`;
|
|
}
|
|
|
|
async toPdfStream() {
|
|
const template = await this.render();
|
|
const defaultOptions = Object.assign({}, config.pdf);
|
|
|
|
const optionsPath = `${this.path}/options.json`;
|
|
const fullPath = path.resolve(__dirname, optionsPath);
|
|
let options = defaultOptions;
|
|
if (fs.existsSync(fullPath))
|
|
options = require(optionsPath);
|
|
|
|
const browser = await puppeteer.launch({
|
|
headless: true,
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
|
});
|
|
const page = await browser.newPage();
|
|
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;
|
|
|
|
const buffer = await page.pdf(options);
|
|
await browser.close();
|
|
|
|
return buffer;
|
|
}
|
|
}
|
|
|
|
module.exports = Report;
|