diff --git a/print/boot.js b/print/boot.js index 02bb27817..5e84bb5dd 100644 --- a/print/boot.js +++ b/print/boot.js @@ -1,11 +1,13 @@ const express = require('express'); const path = require('path'); const fs = require('fs'); +const puppeteer = require('puppeteer'); const templatesPath = path.resolve(__dirname, './templates'); const componentsPath = path.resolve(__dirname, './core/components'); +const config = require('./core/config'); -module.exports = app => { +module.exports = async app => { global.appPath = __dirname; process.env.OPENSSL_CONF = '/etc/ssl/'; @@ -51,4 +53,16 @@ module.exports = app => { app.use(`/api/${templateName}/assets`, express.static(assetsDir)); }); }); + + // Instantiate Puppeteer browser + async function launchBrowser() { + config.browser = await puppeteer.launch({ + headless: true, + args: ['--no-sandbox', '--disable-setuid-sandbox'] + }); + + config.browser.on('disconnected', launchBrowser); + } + + launchBrowser(); }; diff --git a/print/core/report.js b/print/core/report.js index c5847fda1..46404ff96 100644 --- a/print/core/report.js +++ b/print/core/report.js @@ -1,5 +1,4 @@ const fs = require('fs'); -const puppeteer = require('puppeteer'); const path = require('path'); const config = require('./config'); const Component = require('./component'); @@ -28,11 +27,7 @@ class Report extends Component { 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(); + const page = await config.browser.newPage(); await page.emulateMedia('screen'); await page.setContent(template); @@ -51,7 +46,6 @@ class Report extends Component { options.footerTemplate = footer; const buffer = await page.pdf(options); - await browser.close(); return buffer; }