diff --git a/package-lock.json b/package-lock.json index 279ef15..83a520b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,6 +56,7 @@ "notistack": "^2.0.5", "paypal-checkout": "^4.0.336", "pdf-lib": "^1.17.1", + "ps-node-promise-es6": "^0.0.1", "puppeteer": "^19.8.2", "qrcode": "^1.5.1", "raw-body": "^2.5.1", @@ -5792,6 +5793,11 @@ "node": ">=0.8" } }, + "node_modules/connected-domain": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/connected-domain/-/connected-domain-1.0.0.tgz", + "integrity": "sha512-lHlohUiJxlpunvDag2Y0pO20bnvarMjnrdciZeuJUqRwrf/5JHNhdpiPIr5GQ8IkqrFj5TDMQwcCjblGo1oeuA==" + }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -12467,6 +12473,22 @@ "integrity": "sha512-F2JHgJQ1iqwnHDcQjVBsq3n/uoaFL+iPW/eAeL7kVxy/2RrWaN4WroKjjvbsoRtv0ftelNyC01bjRhn/bhcf4A==", "dev": true }, + "node_modules/ps-node": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/ps-node/-/ps-node-0.1.6.tgz", + "integrity": "sha512-w7QJhUTbu70hpDso0YXDRNKCPNuchV8UTUZsAv0m7Qj5g85oHOJfr9drA1EjvK4nQK/bG8P97W4L6PJ3IQLoOA==", + "dependencies": { + "table-parser": "^0.1.3" + } + }, + "node_modules/ps-node-promise-es6": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/ps-node-promise-es6/-/ps-node-promise-es6-0.0.1.tgz", + "integrity": "sha512-w8TTISlSp6eduK0dAcePjzO49FPGfqqopOoZI5p4I0FZpqGaDzKFQmHp4XvPOPfq8K9vwF/gDV+qWNvcd/d8HA==", + "dependencies": { + "ps-node": "^0.1.6" + } + }, "node_modules/pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", @@ -14244,6 +14266,14 @@ "node": ">= 0.8.0" } }, + "node_modules/table-parser": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/table-parser/-/table-parser-0.1.3.tgz", + "integrity": "sha512-LCYeuvqqoPII3lzzYaXKbC3Forb+d2u4bNwhk/9FlivuGRxPE28YEWAYcujeSlLLDlMfvy29+WPybFJZFiKMYg==", + "dependencies": { + "connected-domain": "^1.0.0" + } + }, "node_modules/tapable": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.1.tgz", @@ -19748,6 +19778,11 @@ "integrity": "sha512-U73+6lQFmfiNPrYbXqr6kZ1i1wiRqXnp2nhMsINseWXO8lDau0LGEffJ8kQi4EjLZympVgRdvqjAgiZ1tgzDDA==", "dev": true }, + "connected-domain": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/connected-domain/-/connected-domain-1.0.0.tgz", + "integrity": "sha512-lHlohUiJxlpunvDag2Y0pO20bnvarMjnrdciZeuJUqRwrf/5JHNhdpiPIr5GQ8IkqrFj5TDMQwcCjblGo1oeuA==" + }, "content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -24725,6 +24760,22 @@ "integrity": "sha512-F2JHgJQ1iqwnHDcQjVBsq3n/uoaFL+iPW/eAeL7kVxy/2RrWaN4WroKjjvbsoRtv0ftelNyC01bjRhn/bhcf4A==", "dev": true }, + "ps-node": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/ps-node/-/ps-node-0.1.6.tgz", + "integrity": "sha512-w7QJhUTbu70hpDso0YXDRNKCPNuchV8UTUZsAv0m7Qj5g85oHOJfr9drA1EjvK4nQK/bG8P97W4L6PJ3IQLoOA==", + "requires": { + "table-parser": "^0.1.3" + } + }, + "ps-node-promise-es6": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/ps-node-promise-es6/-/ps-node-promise-es6-0.0.1.tgz", + "integrity": "sha512-w8TTISlSp6eduK0dAcePjzO49FPGfqqopOoZI5p4I0FZpqGaDzKFQmHp4XvPOPfq8K9vwF/gDV+qWNvcd/d8HA==", + "requires": { + "ps-node": "^0.1.6" + } + }, "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", @@ -26084,6 +26135,14 @@ "svg.js": "^2.6.5" } }, + "table-parser": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/table-parser/-/table-parser-0.1.3.tgz", + "integrity": "sha512-LCYeuvqqoPII3lzzYaXKbC3Forb+d2u4bNwhk/9FlivuGRxPE28YEWAYcujeSlLLDlMfvy29+WPybFJZFiKMYg==", + "requires": { + "connected-domain": "^1.0.0" + } + }, "tapable": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.1.tgz", diff --git a/package.json b/package.json index 600c6f7..052d279 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "notistack": "^2.0.5", "paypal-checkout": "^4.0.336", "pdf-lib": "^1.17.1", + "ps-node-promise-es6": "^0.0.1", "puppeteer": "^19.8.2", "qrcode": "^1.5.1", "raw-body": "^2.5.1", diff --git a/src/lib/htmlToPdf.ts b/src/lib/htmlToPdf.ts index 2463116..508589f 100644 --- a/src/lib/htmlToPdf.ts +++ b/src/lib/htmlToPdf.ts @@ -1,4 +1,6 @@ import puppeteer from "puppeteer"; +import ps from "ps-node-promise-es6"; +import _ from "lodash"; export const generatePdf = async (html, options): Promise => { const browser = await puppeteer.launch({ @@ -8,12 +10,32 @@ export const generatePdf = async (html, options): Promise => { '--disable-setuid-sandbox', ] }); + const browserPID = browser.process().pid; const page = await browser.newPage(); - await page.setContent(html, { - waitUntil: 'networkidle0', // wait for page to load completely - }); - const pdf = await page.pdf(options); - await browser.close(); + let pdf; + + try { + await page.setContent(html, { + waitUntil: 'networkidle0', // wait for page to load completely + }); + pdf = await page.pdf(options); + } catch (e) { + throw new Error(e); + } finally { + await page.close(); + await browser.close(); + await killPID(browserPID); + } return pdf; } + +const killPID = async (pid: number) => { + const psLookup = await ps.lookup({ pid: pid }); + for (let proc of psLookup) { + if (_.has(proc, 'pid')) { + console.log(`Killing: ${proc.pid}`) + await ps.kill(proc.pid, 'SIGKILL'); + } + } +}