javascript - Chrome headless rendering completely wrong to pdf - Stack Overflow

I am trying to print a page with several nvd3 charts to pdf using puppeteer. When using the printing fu

I am trying to print a page with several nvd3 charts to pdf using puppeteer. When using the printing function via the Chrome browser I obtain the following decent output (don't mind the blue borders):

When I try to visit the same page using chrome headless via puppeteer, I get this output instead, spanning over 3 pages:

This is the code that I am using:

const puppeteer = require('puppeteer');

(async() => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.setCookie({
        name: 'cookie-a',
        value: '...',
        domain: 'example'
    });
    await page.setCookie({
        name: 'cookie-b',
        value: '...',
        domain: 'example'
    });

    await page.goto('/?bookmark=bot-overview', {waitUntil: 'networkidle'});

    setTimeout(async function () {
        await page.pdf({ path: 'page.pdf', format: 'A4', landscape: true });
        browser.close();
    }, 10000);
})();

I also tried to fix height and width to pixel values, cm and mm, but it doesn't affect the output. Notice that the entire page is styled through pages and bootstrap with their print media stylesheet. I also added several css rules for the printing media (among which the blue borders). One of these rules forces the whole page to be 297mmx210mm big, i.e. the exact size of an A4 page.

I am trying to print a page with several nvd3 charts to pdf using puppeteer. When using the printing function via the Chrome browser I obtain the following decent output (don't mind the blue borders):

When I try to visit the same page using chrome headless via puppeteer, I get this output instead, spanning over 3 pages:

This is the code that I am using:

const puppeteer = require('puppeteer');

(async() => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.setCookie({
        name: 'cookie-a',
        value: '...',
        domain: 'example.'
    });
    await page.setCookie({
        name: 'cookie-b',
        value: '...',
        domain: 'example.'
    });

    await page.goto('http://example./search/?bookmark=bot-overview', {waitUntil: 'networkidle'});

    setTimeout(async function () {
        await page.pdf({ path: 'page.pdf', format: 'A4', landscape: true });
        browser.close();
    }, 10000);
})();

I also tried to fix height and width to pixel values, cm and mm, but it doesn't affect the output. Notice that the entire page is styled through pages and bootstrap with their print media stylesheet. I also added several css rules for the printing media (among which the blue borders). One of these rules forces the whole page to be 297mmx210mm big, i.e. the exact size of an A4 page.

Share Improve this question asked Aug 29, 2017 at 14:23 TotemTotem 4745 silver badges19 bronze badges 1
  • Have you already tried to define the media type using page.emulateMedia() before creating the PDF? – flozia Commented Sep 20, 2017 at 12:44
Add a ment  | 

1 Answer 1

Reset to default 6

I was facing issues like yours. After a while I was able to fix some of them:

    const puppeteer = require('puppeteer');
    const devices = require('puppeteer/DeviceDescriptors');

    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        const url = 'https://example.';

        await page.emulateMedia('screen'); //<--
        await page.goto(url, {waitUntil: 'networkidle'});

        await page.screenshot({path: 'full_img.png', fullPage: true});
        await page.pdf({
          path: 'the_file.pdf', 
          format: 'A4', 
          printBackground: true, //<---
          displayHeaderFooter: true, //<---
          scale: 1,
          landscape: false,
          pageRanges: ""
      });
    })();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745310345a4621966.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信