Overlapping header on second page of pdf generated using HTML
Asked Answered
P

2

8

Target: To give margin of upto 8cm to a dynamically generated pdf, which has repeated header and footer on every page.

Current issue: Although I am able to give margin to the pdf and the content gets properly aligned on the first page, but from the second page onwards the content of the body starts to overlap with the header of the page,

Page 2 and beyond - ISSUE

Page 2 and beyond - ISSUE

How it SHOULD LOOK LIKE How it SHOULD LOOK LIKE

What I tried: Tried the solution in related questions like :- Content overlapping with header on second page of PDF and Table headers overlapping on second page when printing/PDF and other ones too, but none of them seem to be working.

Code:-

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="preconnect" href="https://fonts.gstatic.com" />
    <link
      href="https://fonts.googleapis.com/css2?family=Mulish:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
      crossorigin="anonymous"
    />
    <title>Document</title>
    <style>
      /* Styles go here */
      @media print {
        * {
          -webkit-print-color-adjust: exact !important;
        }
      }
      * {
        font-family: "Mulish", sans-serif;
      }
      .section-padding {
        padding-bottom: 20px;
      }
      .table > :not(:last-child) > :last-child > * {
        border-bottom-color: inherit;
      }
      strong {
        font-weight: 700 !important;
      }
      .page-header,
      .page-header-space {
        height: 250px;
      }
      .page-footer,
      .page-footer-space {
        height: 100px;
      }
      .page-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
      }
      .page-header {
        position: fixed;
        top: 0mm;
        width: 100%;
      }
      .page {
        page-break-after: always;
      }
      @page {
        margin: 5cm;
      }
      @media print {
        thead {
          display: table-header-group;
        }
        tfoot {
          display: table-footer-group;
        }
        body {
          margin: 0;
        }
        table {
          page-break-inside: auto;
        }
        tr,
        div {
          page-break-inside: avoid;
          page-break-after: auto;
        }
      }

    </style>
  </head>
  <body>
    <div class="page-header">
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
      Header</br>
    </div>
    <div class="page-footer">
      Footer
    </div>
    <table style="padding: 0; margin: 0; width: 100%;">
      <thead>
        <tr>
          <td>
            <!--place holder for the fixed-position header-->
            <div class="page-header-space"></div>
          </td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <div class="page" style="color: green; font-weight: bold;">
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
                Main content</br>
            </div>
          </td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>
            <!--place holder for the fixed-position footer-->
            <div class="page-footer-space"></div>
          </td>
        </tr>
      </tfoot>
    </table>
    <script
      src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
      integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
      integrity="sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

PS: The issue would be reproducible only if the margin exceeds the value of 1.5cm, also if any other third party tool or any other way is possible to achieve the target please feel free to share.

Thank you!

Pretext answered 23/6, 2021 at 14:3 Comment(0)
P
1

After searching through a lot of materials, finally, I can conclude that with increasing page margin the max value one can give to the page header i.e <thead> decreases.

For example,

  • 0cm page margin - max header height could be 250px
  • 1cm page margin - max header height could be 230px

and so on.

.page-header,
.page-header-space {
  height: 250px;
}

A point to add, the values given are not strict values and will change based on your use case, but by varying header size based on the margin value, one can achieve the target given in the question.

Pretext answered 3/7, 2021 at 20:42 Comment(0)
B
0

The main issue seems to be that you placed your "Main content" in one single row.
I managed to make it work doing something similar to this.

Also I moved the header and footer placeholders to thead and tfoot respectively.
Additionally, the "@page{margin: 5cm;}" seems to break it, to be honest I don't know why.
I had to modify the code quite a bit but I hope it helps you as a template for what you want to achieve.

This is the code I ended up with:

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="preconnect" href="https://fonts.gstatic.com" />
    <link
    href="https://fonts.googleapis.com/css2?family=Mulish:wght@400;700&display=swap"
    rel="stylesheet"
    />
    <link
    href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
    rel="stylesheet"
    integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
    crossorigin="anonymous"
    />
    <title>Document</title>
    <style>
      /* Styles go here */
      @media print {
        * {
        -webkit-print-color-adjust: exact !important;
        }
      }
      * {
        font-family: "Mulish", sans-serif;
      }
      .section-padding {
        padding-bottom: 20px;
      }
      .table > :not(:last-child) > :last-child > * {
        border-bottom-color: inherit;
      }
      strong {
        font-weight: 700 !important;
      }
      .page-header,
      .page-header-space {
        height: 250px;
      }
      .page-footer,
      .page-footer-space {
        height: 100px;
      }
      .page-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
      }
      .page-header {
        position: fixed;
        top: 0mm;
        width: 100%;
      }
      .page {
        page-break-after: always;
      }
      /* @page {
        margin: 5cm;
      } */
      @media print {
        body {
        margin: 0;
        }
        table {
          page-break-inside: auto;
        }
        tr {
          page-break-inside: avoid;
          page-break-after: auto;
        }
        thead {
          display: table-header-group;
        }
        tfoot {
          display: table-footer-group;
        }   
      }
    </style>
    </head>
  <body>
    <table style="padding: 0; margin: 0; width: 100%;">
      <thead>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
        <tr><td>Header</td></tr>
      </thead>
      <tbody class="page" style="color: green; font-weight: bold;">
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
        <tr><td>Main content</td></tr>
      </tbody>
      <tfoot>
        <tr><td>Footer</td></tr>
      </tfoot>
    </table>

    <script
      src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
      integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
      integrity="sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG"
      crossorigin="anonymous"
    ></script>
    </body>
</html>
Byssus answered 28/6, 2021 at 14:4 Comment(1)
Thank you for answering, but I found two issues on testing the code, 1 - As you mentioned it breaks beyond 5cm, and 2 - Footer doesn't stick to the bottom of the last page instead sticks to where the content endsPretext

© 2022 - 2024 — McMap. All rights reserved.