通过puppeteer生成的PDF内容丢失 [英] Content is missing at the end of the PDF while generated through puppeteer

查看:1129
本文介绍了通过puppeteer生成的PDF内容丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将URL发送给puppeteer来生成PDF。我给page.pdf方法指定了特定的高度和宽度。我能够获得PDF的确切高度,但最后缺少一些内容。

I am trying to generate the PDF by sending the URL to puppeteer. I gave specific height and width as options to page.pdf method. I am able to get the exact height of the PDF but at the end some content is missing. And it is only happening with more height PDF's.

下面是我的代码:

  let pdf = {};
        const puppeteerArguments = [];
        
        puppeteerArguments.push('--disable-web-security', '--allow-running-insecure-content', '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-gpu',
            '--no-first-run',
            '--hide-scrollbars',
            '--disable-dev-shm-usage',
            '--allow-cross-origin-auth-prompt',
            '--enable-hardware-overlays',
            '--start-fullscreen'
            );


        const browser = await puppeteer.launch({headless: true, args: puppeteerArguments, sloMo: 0});

        const page = await browser.newPage();

        const {url} = payload;

        await page.setViewport({
            width: 1024,
            height: 7500,
            deviceScaleFactor: 1,
            isMobile: false
        });

        page.setExtraHTTPHeaders({
            "cookie": '******',
        });


        await page.goto(url, {waitUntil: 'networkidle0', timeout: 0}).then(() => {
            console.log('load success');
        });

        await page.emulateMedia('screen');

        await page.addStyleTag({
            content: `
                  html {
                    -webkit-print-color-adjust: exact !important;
                    -webkit-filter: opacity(1) !important;
               }`
        });

        const selector = '.widgetsRendered'
        await page.waitForFunction(selector => !!document.querySelector(selector), {polling: 600, timeout: 0}, selector);
        
        await page.waitFor(20000);

        const path = `fullPage.pdf`;

        pdf = await page.pdf({
            path,
            width: 1560 + 'px',
            height: 7500 + 'px',
            scale: 1,
            displayHeaderFooter: false,
            margin: 'none',
            printBackground: true,
        });
        return pdf;

我正在使用 puppeteer: ^ 2.1.1

下面是问题的图像。

有人可以帮忙。

推荐答案

删除 -webkit-filter: opacity(1)!important; addStyleTag 方法的内容解决了我的问题。

Removing -webkit-filter: opacity(1) !important; from content of addStyleTag method resolved my issue.

这篇关于通过puppeteer生成的PDF内容丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆