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.

Puppeteer JavaScript margin and HTML CSS margin inconsistency

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: ^1.13
  • Platform / OS version: Debian (deployed via Docker FROM node:8.15.1)
  • Node.js version: 8.15.1

What steps will reproduce the problem? image

To produce the above image, print a PDF with the following code:

await page.pdf({
    format: "A4",
    displayHeaderFooter: true,
    headerTemplate: "<div style=\"margin-left:1.5cm; margin-right:1.5cm; margin-top: 0.5cm; font-size:12px; display:flex; flex-direction:row; justify-content:space-between; width:100%;\"><div>Version 1.2</div><div>Test</div></div>",
    footerTemplate: "N/A",
    printBackground: true,
    margin: {
        top: "1.5cm",
        right: "1.5cm",
        bottom: "1.5cm",
        left: "1.5cm"
    }
});

The page.setContent() can be set to anything.

What is the expected result? Expected result is that the margin-left: 1.5cm should match the margin from the page.pdf() object. Clearly it does not in this case. I need to change the margin-left property to about 1.25cm to get the same margin.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

3reactions
SoerenWebercommented, Apr 22, 2019

This seems to be caused by inconsistent resizing between body and header/footer in Chromium. It’s probably related to a scaling factor, currently 1.33, that’s used in Chromium’s print view. I guess the inconsistency stems from this line but I’m not sure why it is used that way.

As a work-around I used a CSS scale() transform function or set a zoom declaration to resize my elements to 75%. This isn’t a pixel-perfect solution, at least in my case though.

I was able to find two related issues on Chromium’s issue tracker [1][2]. For more background information on the scaling factor, another issue might be helpful [3].

[1] https://bugs.chromium.org/p/chromium/issues/detail?id=833058 [2] https://bugs.chromium.org/p/chromium/issues/detail?id=818496 [3] https://bugs.chromium.org/p/chromium/issues/detail?id=273306

0reactions
stale[bot]commented, Jul 26, 2022

We are closing this issue. If the issue still persists in the latest version of Puppeteer, please reopen the issue and update the description. We will try our best to accomodate it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Text line height inconsistency in puppeteer. Ideas how to fix?
Here is my css for the text in question: .text { font-size: 155px; margin: 0; line-height: 0.9; letter-spacing: -4px; overflow: hidden; }
Read more >
@shivanshs9/puppeteer-pdf - npm
HTML to PDF from the command line with Puppeteer. ... -mr, --marginRight [margin] Right margin, accepts values labeled with units.
Read more >
Is it possible to have background colors beyond a printers margin ...
I'm using puppeteer.js to handle pdf generation. ... So when I print it with margin, the sidebar section is cut-off at top and...
Read more >
Margin considered harmful - Hacker News
You might be able to avoid presentation-only HTML markup by preprocessing the HTML, modifying the HTML with JavaScript, using some CSS hack, ...
Read more >
CSS techniques for Improved Cross Browser Compatibility
Incompatibility of browser with the operating system (OS).Different implementations of JavaScript; Bugs in browsers; Issues with page alignment.
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