Printing issues in Chrome cause table headers and footers to display incorrectly in tables with thead and t

Currently, I am utilizing puppeteer for generating PDFs on a Linux Alpine system. To achieve proper headers and footers on each page, I am employing the thead, tbody, tfoot trick.

An anomaly caught my attention in how Chrome and Chromium handle layout while printing - specifically, skipping the initial page when rendering a table.

To reproduce this issue (tested with Chrome 73.0.3683.75 on Ubuntu 18.04), font-rendering may play a role in affecting the layout.

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <style>
    thead {
      display: table-header-group;
    }
    
    tfoot {
      display: table-footer-group;
    }    
    
    table {
      border-collapse: collapse;
    }
    
    html,
    body,
    table,
    tr,
    td {
      margin: 0;
      padding: 0;
      border: none;
      width: 100%;
    }
  </style>
</head>

<body>
  <table>
    <thead style="background:#fcc">
      <tr>
        <td>
          <div style="height: 231px">head</div>
        </td>
      </tr>
    </thead>
    <tbody style="background:#cfc">
      <tr>
        <td>
          ...
                   content section truncated for brevity
          ...        
        </td>
      </tr>
      <tr>
        <td>widow</td>
      </tr>
    </tbody>
    <tfoot style="background:#ccf">
      <tr>
        <td>
          <div style="height:231px">foot</div>
        </td>
      </tr>
    </tfoot>
  </table>
</body>

</html>

Upon loading this page and initiating print using ctrl+p, the table starts rendering from the second page of the document as shown here: https://i.sstatic.net/6M88A.png. The expected behavior would be to initiate rendering on the first page. Is there a CSS rule or workaround like

page-break-inside: do-it-please-instead-of-messing-up-my-table
?

Edit: I overlooked mentioning a setting - use Margins None to replicate the issue demonstrated here: https://i.sstatic.net/JKWNg.png

Moreover, an incidental concern arises: By removing the second table row,

<tr><td>widow</td></tr>
, another unexpected rendering glitch emerges: https://i.sstatic.net/dVP4H.png. In this situation, the tbody initiates rendering on the first page but displaces the tfoot to the subsequent page. Ideally, I would like the tbody to break appropriately in such scenarios as well.

Answer №1

Just submitted a bug report to the chromium team regarding this issue: https://bugs.chromium.org/p/chromium/issues/detail?id=962435#c13

Update - it has now been marked as resolved (although I have not tested it myself): https://bugs.chromium.org/p/chromium/issues/detail?id=962435#c24

Unfortunately, it seems that fixing this issue is not a top priority. As a workaround, I switched from puppeteer to openhtmltopdf, which handles headers and footers better.

I also created a wrapper for easier use in a microservice environment: https://github.com/modfin/betterpress

Answer №2

We encountered a similar issue, but we found a clever workaround that solved it. By creating a td with a rowspan, the problem was resolved seamlessly. Take a look at the snippet provided below.

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <style>
    thead {
      display: table-header-group;
    }
    
    tfoot {
      display: table-footer-group;
    }
    
    table {
      border-collapse: collapse;
    }
    
    html,
    body,
    table,
    tr,
    td {
      margin: 0;
      padding: 0;
      border: none;
      width: 100%;
    }
  </style>
</head>

<body>
  <table>
    <thead style="background:#fcc">
      <tr>
        <td>
          <div style="height: 231px">head</div>
        </td>
      </tr>
    </thead>
    <tbody style="background:#cfc">
      <tr>
        <td rowspan="2">
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
          <p>content</p>
        </td>
        <td style="width: 0px"></td>
      </tr>
      <tr style="height: 0px"><td style="width: 0px"></td></tr>
      <tr>
        <td>widow</td>
      </tr>
    </tbody>
    <tfoot style="background:#ccf">
      <tr>
        <td>
          <div style="height:231px">foot</div>
        </td>
      </tr>
    </tfoot>
  </table>
</body>

</html>

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

Alter the border line's color based on the specific section or div it overlays

I am attempting to utilize jQuery in order to change the color of my border line depending on its position within the divs. I have set the position to absolute and it is positioned on both divs. My goal is to make the line on the top div appear as grey, wh ...

Can you explain the distinction between using inline-block and inline-table display properties?

It appears that these display selectors are indistinguishable based on my assessment. According to the Mozilla CSS documentation: inline-table: The inline-table value doesn't have a direct HTML equivalent. It functions like a <table> HTML elem ...

Retrieve the width of a fixed div element based on the parent element's width, set

I attempted to set a fixed div to occupy 100% of its parent, but it is taking 100% of the screen instead. You can find the code I used here: http://codepen.io/rodboc/pen/ZOOEWp HTML <div id="wrapper"> <div class="box"> <div class="h ...

Different ways to adjust the positioning of the HTML canvas created using p5.js

I am attempting to integrate the output canvas from p5.js into my webpage by following a tutorial heretutorial. I am hoping that the canvas will appear where I have positioned it within the HTML body. However, no matter what I try with CSS properties like ...

What are some ways to ensure my web application appears correctly on Internet Explorer versions 8 and 9?

When I compare how my application looks in IE versus Chrome and Firefox, it seems like there are styling issues. I want to address this problem. I attempted to use the IE developer tool but couldn't find any options to customize styles and CSS. How d ...

Problem encountered when implementing multiple filters in Vanilla JavaScript

I am facing an issue with my HTML page that contains multiple filters using Vanilla JavaScript (no jQuery). The filtering process involves counting matches of filter selections against the data attributes of each element. However, I'm puzzled as to w ...

Error with HTML form validation

After clicking the "Send Request" button, the query string ?req_flag=0 is not appearing in the URL. What could be causing this issue? I want my URL to look like this: localhost/flavourr/send_req.php?req_flag=0&f_e_mail_add=value <pre> <form ...

Endlessly refreshing occurs when attempting to load a separate CSS stylesheet using document.write

I am attempting to implement two separate stylesheets on my WordPress blog - one for web access and another for our iOS app. Currently, we are adding ?app=true to the URL when accessing content through the app in order to distinguish between the two. Using ...

Do not engage with the website while animations are running

I'm working on a JavaScript project where I want to create textareas that grow and center in the window when clicked, and shrink back down when not focused. However, I ran into some issues with the .animate() function that are causing bugs. During QA ...

The functionality of Ng-Show is acting up

$scope.stay = function() { alert("Inside Keep me In") $scope.timed = false; $scope.isLogStatus = true; } $scope.displayAlert = function() { $scope.timed = true; alert("inside display") } function idleTimer() { var t; $window.o ...

Is there a way to automatically increase the width of a textarea?

Is there a way to automatically adjust the width of a textarea? I know how to make it grow in height, but I haven't come across a method to only increase the width. To provide a visual example, I'm looking for something similar to how iMessage e ...

Creating diagonal ribbon designs can add a unique and dynamic touch to any

Can anyone help me figure out how to create a ribbon similar to the image below using CSS? I have managed to make the slanted filled box with text inside, but I am having trouble with the flaps. Check out this CodePen link for reference. ...

Encountering a 404 error while loading CSS in a Django application

I'm currently in the process of developing a chatbot application. I'm encountering issues with loading the CSS for the frontend using django's static data load feature. Here are the file paths: CSS path: C:\Documents\Django_Works ...

ReactStrap: Transparency issue with DropDown items on mobile devices

I've encountered an issue with my dropdown menu that works perfectly on desktop: https://i.sstatic.net/73Z0X.png However, on mobile devices, it appears to be transparent for some reason: https://i.sstatic.net/dhtOw.png Here is the code snippet ...

Angular does not display results when using InnerHtml

I'm currently in the process of creating a weather application with Angular, where I need to display different icons based on the weather data received. To achieve this functionality, I have developed a function that determines the appropriate icon to ...

Tips for managing the dimensions of the <label> element within a React component

I'm facing an issue where the element size is not matching the box size as expected. Additionally, the width property doesn't seem to work in React. Does anyone know how to solve this problem? const DragDrop = () => { ... return ( &l ...

Is it acceptable to utilize a UUID as an HTML tag ID without encountering any problems?

I need to handle dynamic content accessible through an index on a side panel. When a user selects an element from the side panel, I use the ID to determine which data they're requesting so that I can generate the corresponding content for the main sec ...

What steps can I take to ensure that an image does not exceed the size of its parent tag?

Is there a way to ensure that items placed in a container do not exceed the boundaries of the container? I am trying to use my logo as the home button, but when I set the image height and width to 100%, it becomes larger than the nav bar. The CSS for the n ...

Is there a sophisticated CSS compressor that can eliminate unnecessary code and separate identical rules with commas?

Consider this input{margin:0}body{margin:0;background:white} can also be written as input,body{margin:0}body{background:white} or like this input,body{margin:0}body{margin:0;padding:0} and simplified to input,body{margin:0}body{padding:0} Bottom Li ...

What could be causing the CSS styling for a Bootstrap 4 button to fail when it's applied?

Starting my first Bootstrap project and running into issues with CSS for my button. Trying to change it to green but it's not working, even though it worked in the past. This is the button I'm trying to style (the blue one): https://i.sstatic.n ...