Troubleshooting puppeteer PDF: Unable to set table height using CSS for tr:last-child { height: 100%; }

My Node.js code snippet is as follows:

await page.setContent(html, {waitUntil: 'networkidle2'});

const pdf = await page.pdf({
  format: 'A4',
  printBackground: true,
  margin: {
    top: '14mm', // default is 0, units: mm, cm, in, px
    right: '10mm',
    bottom: '35mm',
    left: '12mm'
  },
  displayHeaderFooter: true,
  headerTemplate: header,
  footerTemplate: footer
});

I have managed to create an HTML that displays the height correctly in Chrome: https://i.sstatic.net/g8buZ.png

However, when converting this into a pdf, the TR height is not taken into account (as shown by the red line): https://i.sstatic.net/WePvF.png

I have tried various methods to adjust the table height and the TR elements to fill the entire page for the PDF output without success...

Update 1:

To visualize how the page appears, I included a screenshot capture with

await page.screenshot({path: '/tmp/full_img.png', fullPage: true});

The resulting image shows this view: https://i.sstatic.net/bbgpp.png

From this, it is evident that the page renders correctly in the "browser" but the "printed" PDF does not.

Update 2:

I also added borders to the page objects, revealing that the body (yellow) and the main container (green), which holds the tables, are displaying correctly while only the table (cyan) is deviating from the design:

https://i.sstatic.net/sa7KX.png

Update 3:

Comparing the "Print" option in Chrome on my local browser to Puppeteer's output, they now align consistently:

https://i.sstatic.net/rkRNB.png

Easier to test of course... Below is the updated HTML link as well: https://github.com/puppeteer/puppeteer/files/5747755/ut.html.txt

Answer №1

We recommend incorporating the following into your stylesheet

@media screen {
    section, article {
        width: 100%;
    }
}

Answer №2

After extensive hours of investigation, I have uncovered two critical issues!

First and foremost, it appears that both the code and the table require specific CSS attributes:

//node.js
await page.emulateMediaType('screen'); //<-- crucial

//css
.itemstable {
        border-collapse: collapse;
        border-spacing: 0 0;
        -webkit-border-vertical-spacing: 0;
        width: 100%;
        height: 100%;
        min-height: 198mm !important;
        position: relative;
        table-layout: fixed;
    }

Although I cannot confirm if all of these attributes are absolutely necessary, I will refrain from making any further changes! :)

The second issue seems to be related to a potential bug on Windows!

Upon testing this HTML code on Edge, Chrome, and Vivaldi browsers on Windows, I encountered the same error across all three platforms, as well as when running Puppeteer with Node.js on Windows!

I enlisted the help of a colleague who tested the code on his Mac, and the PDF displayed the correct table/tr height. Subsequently, I conducted tests using WSL (Windows Subsystem for Linux) and AWS Lambda (Node.js), where the code worked flawlessly after implementing the aforementioned CSS modifications!

Answer №3

I encountered a similar issue while using puppeteer to generate a PDF invoice. I was able to resolve it by setting the height: 50vh for the <table> tag (or through CSS). Additionally, I included a

<tr style="height:100%"><td></td></tr>
at the end with the same structure.

<table style="height: 50vh">
      <thead>
        <tr>
          <th>1</th>
          <th>2</th>
          <th>3</th>
          <th>4</th>
          <th>5</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Content 1</td>
          <td>Content 2</td>
          <td>Content 3</td>
          <td>Content 4</td>
          <td>Content 5</td>
        </tr>
        <tr style="height: 100%">
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
</table>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Node.js server along with MySQL database and socket.io technology for real-time

I need assistance in creating a Node.js server that integrates with MySQL and socket.io. Can someone please provide me with some sample code? Our database table consists of 20 columns, and every 30 seconds we receive new data through various operations s ...

Could somebody clarify the situation with the `push` function?

Something seems off with the behavior of the push method. Instead of pushing to only one index within the forEach, it appears to be pushing to all three indexes. Can anyone see what might be causing this unexpected result? let arrayToReduce = [ [ 1, 2, ...

Concerns with the dimensions of HTML thumbnails

view image description here I seem to be having an issue with the size of thumbnails on my website. The problem arises when a thumbnail with a long title is displayed, as it ends up taking up more space and affecting the layout. Is there a solution to ens ...

Could you show me how the easyrtcid is generated in the demonstration of audio-only chat? I would appreciate a step-by

Currently, I am utilizing the easyrtc webpage to test out the audio only chat demo and everything seems to be running smoothly. However, when connecting, the easyrtcid variable is automatically assigned a random string. I was wondering if there is a way t ...

Sending arguments to a bash script using npm

I recently put together a batch script designed to run multiple jest tests simultaneously. Here's an outline of how it works: #!/bin/bash echo 'Each test will be executed 10 times per loop, with a total of 200 instances being run.' echo &apo ...

The drag and drop feature seems to be malfunctioning in the Selenium Webdriver when using the Node.js (JavaScript) test automation framework

I am currently utilizing the selenium webdriver along with a customized automation framework built in nodejs. My goal is to implement drag and drop functionality for a slider using the actions class, but unfortunately I am encountering issues. Below you ca ...

Something seems to be preventing Div from appearing and there are no error messages appearing

I've been attempting to create a menu, but the toggle div for the menu isn't visible Following a tutorial where an individual sets up a menu, there is a div with a menu icon in the top left corner. Despite meticulously copying the code from the ...

I am interested in extracting information from a website by utilizing PHP

Can data be scrapped from a website using PHP without the use of any external tools? I attempted with the following code, but it proved to be insufficient: <?php $url = 'http://www.example.com'; $output = file_get_contents($url); echo $output ...

Extract content from an HTML form within a specific cell using Cheerio

A sample HTML table is displayed below: <tr class="row-class" role="row"> <td>Text1</td> <td> <form method='get' action='http://example.php'> <input type='hidden' ...

Extending an element beyond the boundaries of its container

I currently have 3 different divisions within the body of my document: a container, a parent, and a child element. My goal is to make the child element extend beyond its parent on the left side. However, when I try to achieve this by using position: ab ...

What is the reason behind Svelte not scoping the tag under a class, but instead using individual tag.class to style a component?

It is common practice to scope CSS styles for components like this: .my-component-01 h1 { ... } .my-component-01 h2 { ... } However, Svelte takes a different approach by applying the styles directly to the tags: h1.svelte-hlsza1{color:orange} h2.svelte- ...

Is there a way to prevent this picture from shifting?

I am currently revamping the content on my work website using Joomla. I have received the old copy and now I need to enhance it. The website in question is 24x7cloud.co.uk. At the bottom of the page, I aim to include an "Accreditation's" section. Howe ...

Reveal and Conceal, the ever-changing show

As I work on my blog, I want to make the layout more compact by having a link that reveals comments and entry forms when clicked. I've seen this feature on other sites as "Comments (5)", but I'm unsure how to implement it myself. Below is a snip ...

How come my npm is stuck at version 2.14.12 and not updating to 3.7.2?

I recently installed node from the official website and it came with npm version 2.14.12. I am now trying to update npm to version 3 using the following commands: $ npm -v 2.14.12 $ sudo npm install npm@latest -g /Users/sudiptasen/.node/bin/npm -> /U ...

IE has trouble with CSS inline-block and width-auto styling, leading to incorrect rendering

Recently, I've noticed a strange behavior in Internet Explorer 9-11 and Edge (Spartan). In all browsers except for IE, my example displays as desired: However, in Internet Explorer, it looks different: I used to encounter this issue on multiple bro ...

Exploring the power of internal linking with GatsbyJS

I am currently developing a website using gatsbyjs. I have concerns about the crawlability of "onClick" for SEO purposes and I would appreciate some assistance. This is my current code: render={data => ( <div className='feed'> ...

Transfer data dynamically between JSP pages using parameters

In my Java application, users can log in and predict football matches. I have created an admin JSP page where I retrieve all team names from a database table to set up this week's matches. Later, after the matches finish, I set the final results to co ...

Element receives scrollbar upon reaching 100% width

I am intrigued as to why the overflow: auto; rule is causing a scrollbar to appear in this particular scenario CSS: textarea { width: 100%; margin:0; padding:0; } .span4 { width: 300px; } aside { overflow: auto; } html: <asid ...

Adding a custom validation function to the joi.any() method - the easy way!

Is there a way to enhance joi.any() with a new rule that can be applied to any existing type, such as joi.boolean() or joi.string()? I already know how to extend joi by creating a custom type but that doesn't allow me to combine the new type with exis ...

Generate visual representations of data sorted by category using AngularJS components

I am facing an unusual issue with Highcharts and Angularjs 1.6 integration. I have implemented components to display graphs based on the chart type. Below is an example of the JSON data structure: "Widgets":[ { "Id":1, "description":"Tes ...