我是javascript和nodejs的新手
我正在尝试使用Google puppeteer使用node.js生成PDF表。
我遵循了youtube教程,并且下面的代码与该教程中使用的代码相同。 注意:下面的代码我不认为。
我使用了一个示例HTML模板,该模板填充了json文件中的数据。
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);
}
})();
我能够进行压力测试直到100000结果,但是当我推到200000时 我收到以下错误
{ 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.' }
我在线搜索,但无法解决此问题,之前有人遇到过此问题吗? 我也看到了这篇文章UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed. (Puppeteer),但无法解决
Github上的PR链接:https://github.com/GoogleChrome/puppeteer/issues/3683