question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Puppeteer Generated PDF is not same as Chrome Generated PDF

See original GitHub issue

Bug description

Puppeteer PDF is generating pdf (from url) different from chrome. In a single page pdf, chrome pdf is okay and generating a single page pdf. chrome_single_page.pdf But puppeteer is adding a blank page at the end. puppeteer_single_page.pdf

When I have long content and multiple page, chrome is generating the pdf okay. chrome_multi_page.pdf Puppeteer is however adding an extra blank page at the start and content spreads into multiple pages.

puppeteer_multi_page.pdf I am not sure if its a bug or I am doing something wrong here. Puppeteer Code

const browser = await puppeteer.launch({
    headless: true,
    args: [
        '--kiosk-printing',
        '--disable-gpu',
        '--disabled-dev-shm-usage',
        '--disabled-setupid-sandbox',
        '--no-first-run',
        '--no-sandbox',
        '--no-zygote',
        '--single-process',
    ],
});
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 768});
await page.goto(`${req.body.url}`, {waitUntil: ['domcontentloaded', 'networkidle0']});
const pdf = await page.pdf({
    format: 'A4',
    landscape: false,
    printBackground: false,
    preferCSSPageSize: false,
    margin: {
        top: 20,
        bottom: 20,
        left: 20,
        right: 20,
    }
});
     
await browser.close();

Puppeteer version

^19.2.2

Node.js version

v16.16.0

npm version

8.11.0

What operating system are you seeing the problem on?

macOS

Configuration file

const {join} = require('path');

/**
 * @type {import("puppeteer").Configuration}
 */
module.exports = {
    // Changes the cache location for Puppeteer.
    cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};

Relevant log output

No response

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:12

github_iconTop GitHub Comments

2reactions
OrKoNcommented, Nov 10, 2022

It looks like a regression in Chromium: you should be able to work around it by specifying the following argument when launching puppeteer: --disable-blink-features=LayoutNGPrinting

0reactions
dcecilecommented, Nov 25, 2022

The --disable-blink-features=LayoutNGPrinting workaround fixed the issue for me. (Running 19.2.2, had a page incorrectly overflowing.) Thanks @OrKoN!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer - Different results for PDF generation (local ...
The PDF created over local and server should be the same. Same font and size for the same code. What happens instead? The...
Read more >
607777 - "save as PDF" doesn't generate a 508 compliance PDF
Add assertion that PDFs generated by Chrome are not tagged. Later when experimental tagging support has landed we'll be able to add
Read more >
Convert web pages into PDFs with Puppeteer and NodeJS
Learn how you can generate a PDF document from any web page using ... you can configure and use it as non-headless Chrome...
Read more >
Generate PDF with Chrome, Puppeteer, and Serverless Stack
But with the Serverless framework, developers are not tied to using a specific platform, as it can be used to deploy functions on...
Read more >
1658333 - printToPDF produces a file that is small and low ...
Taking a screenshot of the same document in Chrome and Firefox produces different-quality pdf files: the pdf produced by Chrome has a much...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found