当我使用 puppeteer 生成 PDF 文件时出现以下错误,
错误:协议错误(Runtime.callFunctionOn):目标已关闭。在 Promise (C:\Users\Rakshith.Shivaram1.MEA\Documents\EY_SARGE_PROJECT\git\26-06-2020\puppeteer-proj\git\html2pdf-puppeteer\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection. js:208:63) 在 CDPSession.send (C:\Users\Rakshith.Shivaram1.MEA\Documents\EY_SARGE_PROJECT\git\26-06-2020\puppeteer-proj\git\html2pdf-puppeteer\node_modules \puppeteer\lib\cjs\puppeteer\common\Connection.js:207:16) 在 ExecutionContext._evaluateInternal (C:\Users\Rakshith.Shivaram1.MEA\Documents\EY_SARGE_PROJECT\git\26-06-2020\puppeteer-proj \git\html2pdf-puppeteer\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:200:50) 在 ExecutionContext.evaluate (C:\Users\Rakshith.Shivaram1.
使用以下代码启动 puppeteer
const browser = await puppeteer.launch({
pipe: true,
args: [
'--headless', '--disable-gpu', '--full-memory-crash-report', '--unlimited-storage',
'--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'
]
})
对于setContent
超时
await page.setContent(htmContent, { waitUntil: 'networkidle0', timeout: 80000 })
请让我知道是否可以从 HTML 内容生成大型 PDF 文件?
将 Small HTML 内容转换为 PDF 文件的完整代码,
const browser = await puppeteer.launch({
// executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
headless: false,
pipe: true,
args: [
'--headless', '--disable-gpu', '--full-memory-crash-report', '--unlimited-storage',
'--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'
]
}).catch((el) => {
console.log('browser', el);
next(createError(el));
});
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(0);
// await page.waitFor(80000);
await page.setRequestInterception(true);
page.on('request', interceptedRequest => {
interceptedRequest.continue();
});
const version = await page.browser().version();
console.log('chromium version', version);
// var buffer = new Buffer(htmContent);
// var bufferBase64 = buffer.toString('utf-8');
await page.setContent(htmContent,
{
waitUntil: 'load',
timeout: 0
}).catch((ep) => {
console.log('setContent', ep);
next(createError(ep));
});
// await page.setDefaultTimeout(0);
await page.waitFor(300000).then(async () => {
// page.emulateMediaType('print');
// const pdf = await page.pdf({ fullPage: true });
console.log('page.waitFor(300000) done');
page.on('load', () => console.log('Page loaded!'));
const pdf = await page.pdf({ fullPage: true });
await page.waitFor(300000).then(() => {
console.log('page.waitFor(300000) done');
res.set('Content-Type', 'application/pdf');
res.header("Access-Control-Allow-Origin", "*");
res.send(pdf);
});
});
// const pdf = await page.pdf({ fullPage: true });
// res.set('Content-Type', 'application/pdf');
// res.header("Access-Control-Allow-Origin", "*");
// res.send(pdf);
await browser.close().catch((eb) => {
console.log('browser.close', ep);
next(createError(eb));
});
});