How to position footer at bottom of final page when creating PDF with Puppeteer

I’m working on generating PDFs with Puppeteer and I’m looking for assistance to place a footer element correctly at the bottom of the last page. The content is dynamic and can cover multiple pages, but I want to ensure that the footer stays at the bottom of the final page only.

I attempted to use CSS properties like ‘position: fixed’ and calculated heights using the DOM’s scrollHeight and offsetTop methods, but those solutions aren’t very effective due to my table setup, which includes both thead and tfoot elements, along with ‘page-break-inside’ styles.

Here’s a sample of my HTML structure:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .main-content {
        background-color: #ccc;
      }
      .page-footer {
        background-color: #f4a261;
      }
    </style>
  </head>
  <body>
    <div class="main-content">
      <table>
        <thead>
          <tr>
            <th>Product Name</th>
            <th>Quantity</th>
            <th>Price</th>
            <th>Total</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td colspan="4">Item details row 1</td>
          </tr>
          <tr>
            <td colspan="4">Item details row 2</td>
          </tr>
          <tr>
            <td colspan="4">Item details row 3</td>
          </tr>
          <tr>
            <td colspan="4">Item details row 4</td>
          </tr>
          <tr>
            <td colspan="4">Item details row 5</td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <td colspan="4">Summary totals</td>
          </tr>
        </tfoot>
      </table>
    </div>
    <div class="page-footer">
      This footer should appear at the bottom of the last page
    </div>
  </body>
</html>

The content within the table is generated dynamically and the number of pages varies. I’m looking for a reliable method to guarantee that the footer div consistently remains at the bottom of the last page when the PDF is created. Any advice or solutions would be greatly appreciated!

Try position: absolute with bottom: 0 on your footer instead of fixed. Wrap everything in a container with position: relative and min-height: 100vh. Works way better with puppeteer than fixed positioning. You’ll probably need bottom padding on your main content so it doesn’t overlap.

Had the same issue a few months back when generating invoices with Puppeteer. Here’s what actually worked for me: ditch fixed positioning completely and use CSS flexbox with viewport height units instead. Set your body to display: flex, flex-direction: column, and min-height: 100vh. Give your main content flex-grow: 1 and footer flex-shrink: 0. This naturally pushes the footer down no matter how much content you have. For tables, wrap them in a flex container div - fixes most positioning headaches. Also throw margin-top: auto on your footer div just to be safe. Puppeteer handles page breaks weird compared to regular browsers, so avoiding fixed positioning is key. This setup’s been rock solid for me across different content lengths and page counts.

Had this exact issue building a reporting system last year. CSS Grid solved it after I gave up fighting with positioning. Set your body to display: grid with grid-template-rows: 1fr auto and min-height: 100vh. Main content goes in the first area, footer in the second. The 1fr expands content to fill space while auto keeps the footer’s natural height. Grid doesn’t mess with table layouts like flexbox can. I’d also add box-sizing: border-box to everything - helps with height calculations. Best part? Grid handles last page positioning automatically. No JavaScript height calculations or fixed positioning hacks that break Puppeteer’s rendering.