salix/print/core/report.js

64 lines
1.7 KiB
JavaScript

const fs = require('fs');
// const pdf = require('html-pdf');
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();
let options = Object.assign({}, config.pdf);
const optionsPath = `${this.path}/options.json`;
const fullPath = path.resolve(__dirname, optionsPath);
if (fs.existsSync(fullPath))
options = Object.assign(options, require(optionsPath));
/* return new Promise(resolve => {
pdf.create(template, options).toStream((err, stream) => {
resolve(stream);
});
}); */
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.setContent(template);
const element = await page.$('#pageFooter');
let footer = '';
if (element) {
footer = await page.evaluate(el => {
const html = el.innerHTML;
el.remove();
return html;
}, element);
}
options.headerTemplate = '\n';
options.footerTemplate = footer;
console.log(options);
const buffer = await page.pdf(options);
await browser.close();
return buffer;
}
}
module.exports = Report;