5

I am new to javascript and nodejs

I am trying to generate a PDF table using node.js using google puppeteer.

I followed a youtube tutorial and the code below is the same as used in the tutorial. Note: I dont take credit for the below code.

I used a sample HTML template which I populate data from a json file.

    const puppeteer = require('puppeteer');
    const fs = require('fs-extra');
    const hbs = require('handlebars');
    const path = require('path');
    const data = require('./stressData2.json');

    const compile = async function(templateName, data){
        const filePath = await path.join(process.cwd(), 'templates', `${templateName}.hbs`);
        const html = await fs.readFile(filePath, 'utf-8');
        return await hbs.compile(html)(data);
    };

    (async function(){
        try{
            const browser = await puppeteer.launch();
            const page = await browser.newPage();
            const content = await compile('stressTemplate', data);
            await page.setContent(content);
            await page.pdf({
                path: 'pdfs/mypdf.pdf',
                format: 'A4',
                printBackground: true
            });

            console.log('done');

            await browser.close();
            await process.exit();
        } catch(e){
            console.log('Error', e);
        }
    })();

I am able to stress test till 100000 results, but when I push to 200000 I get the below error

{ Error: Protocol error (Runtime.callFunctionOn): Target closed.
    at Promise (/userdir/node_modules/puppeteer/lib/Connection.js:186:56)
    at new Promise (<anonymous>)
    at CDPSession.send (/userdir/node_modules/puppeteer/lib/Connection.js:185:12)
    at ExecutionContext.evaluateHandle (/userdir/node_modules/puppeteer/lib/ExecutionContext.js:
115:75)
    at ExecutionContext.<anonymous> (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
    at ExecutionContext.evaluate (/userdir/node_modules/puppeteer/lib/ExecutionContext.js:58:31)
    at ExecutionContext.<anonymous> (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
    at Frame.evaluate (/userdir/node_modules/puppeteer/lib/FrameManager.js:447:20)
  -- ASYNC --
    at Frame.<anonymous> (/userdir/node_modules/puppeteer/lib/helper.js:144:27)
    at Frame.setContent (/userdir/node_modules/puppeteer/lib/FrameManager.js:541:16)
    at Frame.<anonymous> (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
    at Page.setContent (/userdir/node_modules/puppeteer/lib/Page.js:615:42)
    at Page.<anonymous> (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
    at /userdir/git/NodeJsServer/serverPdfGenerator.js:18:20
  message: 'Protocol error (Runtime.callFunctionOn): Target closed.' }

I searched online and I am not able to fix this, has anyone faced this issue before I seen this post as well UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed. (Puppeteer) but not able to resolve it

PR Link on Github: https://github.com/GoogleChrome/puppeteer/issues/3683

Gowrav
  • 289
  • 1
  • 4
  • 20
  • 1
    Any solutions for page.setContent(largeHTMLContent). I think, page.setContent(largeHTMLContent) causes, above error, but still unable to solve the issue. Anyone found any solution for above mentioned issue? – MechaCode Jun 17 '20 at 06:11

0 Answers0