salix/print/core/report.js

55 lines
1.4 KiB
JavaScript

const fs = require('fs');
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 page = await config.browser.newPage();
await page.emulateMedia('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;
const buffer = await page.pdf(options);
return buffer;
}
}
module.exports = Report;