const Vue = require('vue'); const VueI18n = require('vue-i18n'); const renderer = require('vue-server-renderer').createRenderer(); const fs = require('fs'); const pdf = require('html-pdf'); const juice = require('juice'); const path = require('path'); const config = require('./config'); const reportsPath = '../templates/reports'; if (!process.env.OPENSSL_CONF) process.env.OPENSSL_CONF = '/etc/ssl/'; Vue.use(VueI18n); class Report { constructor(name, args) { this.name = name; this.args = args; } get path() { return `${reportsPath}/${this.name}`; } get template() { const templatePath = `${this.path}/${this.name}.html`; const fullPath = path.resolve(__dirname, templatePath); return fs.readFileSync(fullPath, 'utf8'); } get locale() { } get style() { } async render() { const localePath = `${this.path}/locale`; const stylePath = `${this.path}/assets/css/index`; const stylesheet = require(stylePath); const component = require(this.path); component.i18n = require(localePath); component.template = juice.inlineContent(this.template, stylesheet, { inlinePseudoElements: true }); const i18n = new VueI18n(config.i18n); const app = new Vue({ i18n: i18n, render: h => h(component, { props: this.args }) }); return renderer.renderToString(app); } async toPdfStream() { const template = await this.render(); let options = config.pdf; const optionsPath = `${this.path}/options.json`; if (fs.existsSync(optionsPath)) options = Object.assign(options, require(optionsPath)); return new Promise(resolve => { pdf.create(template, options).toStream((err, stream) => { resolve(stream); }); }); } } module.exports = Report; /* module.exports = { path: `${appPath}/report`, async render(name, ctx) { const component = require(`${this.path}/${name}`); const i18n = new VueI18n(config.i18n); const app = new Vue({ i18n: i18n, render: h => h(component, { props: { myProp: 'asd1' } }) }); return renderer.renderToString(app); }, async preFetch(orgComponent, ctx) { let component = Object.create(orgComponent); let mergedData = {}; let asyncData = {}; let data = {}; let params = {}; if (Object.keys(ctx.body).length > 0) params = ctx.body; if (Object.keys(ctx.query).length > 0) params = ctx.query; await this.attachAssets(component); if (orgComponent.hasOwnProperty('data')) data = orgComponent.data(); if (orgComponent.hasOwnProperty('asyncData')) asyncData = await orgComponent.asyncData(ctx, params); mergedData = Object.assign(mergedData, data, asyncData); component.data = function data() { return mergedData; }; const components = orgComponent.components; if (components) { const promises = []; const childNames = []; component.components = {}; Object.keys(components).forEach(childName => { childNames.push(childName); promises.push(this.preFetch(components[childName], ctx)); }); await Promise.all(promises).then(results => { results.forEach((result, i) => { component.components[childNames[i]] = result.component; }); }); } return {component}; }, async attachAssets(component) { const localePath = `${this.path}/${component.name}/locale`; const templatePath = `${this.path}/${component.name}/index.html`; const stylePath = `${this.path}/${component.name}/assets/css/index`; const template = await fs.readFile(templatePath, 'utf8'); const css = require(stylePath); const cssOptions = {inlinePseudoElements: true}; component.i18n = require(localePath); component.template = juice.inlineContent(template, css, cssOptions); }, async toPdf(name, ctx) { const html = await this.render(name, ctx); let options = config.pdf; const optionsPath = `${this.path}/${name}/options.json`; if (fs.existsSync(optionsPath)) options = Object.assign(options, require(optionsPath)); return new Promise(resolve => { pdf.create(html, options).toStream((err, stream) => { resolve(stream); }); }); }, }; */