使用Google Puppeteer将HTML转换为大文件的PDF

问题描述

使用人偶生成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) 在新的Promise() 在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 \ 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.MEA \ Documents \ EY_SARGE_PROJECT \ git \ 26-06-2020 \ puppeteer-proj \ git \ html2pdf-puppeteer \ node_modules \ puppeteer \ lib \ cjs \ puppeteer \ common \ ExecutionContext.js:106:27) 在DOMWorld.evaluate(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 \ common \ DOMWorld.js:79:24) 在process._tickCallback(internal / process / next_tick.js:68:7)

使用以下代码启动木偶

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-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));
                });
            });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)