Search code examples
htmlcsspdf-generationweasyprintdjango-weasyprint

How to set a header in the same place across different pages by using WeasyPrint?


we are using WeasyPrint version: 53.4 & Python version: 3.9.1 to generate PDF from html template. But we are facing a problem in the running header alignment after the first page. It exactly same as the issue, and solution for that problem not working for us. I will update the sample code here :

Python :

from weasyprint import HTML, CSS

def new_test(request):
    pdf_file = HTML("invoice.html").write_pdf(stylesheets=[CSS("invoice.css")])
    response = HttpResponse(pdf_file,content_type="application/pdf")
    response["Content-Disposition"] = "attachment;filename=print_final.pdf"
    return response

HTML and CSS :

body {
  margin-top: 4.5cm;
  border: 0.1cm solid red;
  font-family: Arial, Helvetica, sans-serif;
  height: 20cm;
  width: 100%;
}

header {
  position: running(header);
  border: 0.1cm solid black;
  height: 5cm;
  width: 17cm;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  margin-top: 5cm;
  padding-top: 0cm;
}
.header_text {
  text-align: right;
  padding-right: 0;
  /* position: fixed; */
  width: 100%;
}
footer {
  position: running(footer);
  border: 0.1cm solid blue;
  height: 1cm;
}
@page {
  @top-center {
    content: element(header);
    width: 100%;
  }
  @bottom-center {
    content: element(footer);
    width: 100%;
  }
  size: A4;
}
<html>
  <head>
    <meta charset="utf-8" />
    <link href="invoice.css" media="print" rel="stylesheet" />
    <title>Invoice</title>
    <meta name="description" content="Invoice demo sample" />
  </head>

  <header id="header" class="header">
    <div class="header_text">
      <p>Company logo</p>
      <p>Company XYZ - NewCity</p>
      <p>Address - Contact Details</p>
      <p>Webbsite Info</p>
      <p>Phone Number</p>
    </div>
  </header>

  <body>
    <h3>Table 1</h3>
    <table>
      <thead>
        <tr>
          <th>Description</th>
          <th>Price</th>
          <th>Quantity</th>
          <th>Subtotal</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Website design</td>
          <td>€34.20</td>
          <td>100</td>
          <td>€3,420.00</td>
        </tr>
        <tr>
          <td>Website development</td>
          <td>€45.50</td>
          <td>100</td>
          <td>€4,550.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
      </tbody>
    </table>

    <h3>Table 2</h3>
    <table>
      <thead>
        <tr>
          <th>Description</th>
          <th>Price</th>
          <th>Quantity</th>
          <th>Subtotal</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Website design</td>
          <td>€34.20</td>
          <td>100</td>
          <td>€3,420.00</td>
        </tr>
        <tr>
          <td>Website development</td>
          <td>€45.50</td>
          <td>100</td>
          <td>€4,550.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
      </tbody>
    </table>

    <h3>Table 3</h3>
    <table>
      <thead>
        <tr>
          <th>Description</th>
          <th>Price</th>
          <th>Quantity</th>
          <th>Subtotal</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Website design</td>
          <td>€34.20</td>
          <td>100</td>
          <td>€3,420.00</td>
        </tr>
        <tr>
          <td>Website development</td>
          <td>€45.50</td>
          <td>100</td>
          <td>€4,550.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
      </tbody>
    </table>

    <h3>Table 4</h3>
    <table>
      <thead>
        <tr>
          <th>Description</th>
          <th>Price</th>
          <th>Quantity</th>
          <th>Subtotal</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Website design</td>
          <td>€34.20</td>
          <td>100</td>
          <td>€3,420.00</td>
        </tr>
        <tr>
          <td>Website development</td>
          <td>€45.50</td>
          <td>100</td>
          <td>€4,550.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
         <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
         <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
         <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
        <tr>
          <td>Website integration</td>
          <td>€25.75</td>
          <td>100</td>
          <td>€2,575.00</td>
        </tr>
      </tbody>
    </table>
  </body>

  <footer>This is a footer test</footer>
</html>

Here the first page we are getting the header in the proper location but after that it's not as expected. We tried to update the CSS for the second page and so on , but it's not worked.

Page 1 & 2 : Page 1 Page 2

Thanks !


Solution

  • body {
      border: 0.1cm solid red;
      box-decoration-break: clone;
    }
    
    header {
      /* position: running(header); */
      position: fixed;
      border: 0.1cm solid black;
      top: -5cm;
      width: 100%;
    }
    footer {
      position: fixed;
      border: 0.1cm solid blue;
      height: 1cm;
      bottom: -2cm;
      width: 100%;
    }
    @page {
      @top-center {
        content: element(header);
        width: 100%;
        margin-top: 5cm;
      }
      @bottom-center {
        content: element(footer);
        width: 100%;
      }
      size: A4;
      margin-top: 6cm;
    }
    <html>
      <head>
        <meta charset="utf-8" />
        <link href="invoice.css" media="print" rel="stylesheet" />
        <title>Invoice</title>
        <meta name="description" content="Invoice demo sample" />
      </head>
    
      <header id="header" class="header">
        <div class="header_text">
          <p>Company logo</p>
          <p>Company XYZ - NewCity</p>
          <p>Address - Contact Details</p>
          <p>Webbsite Info</p>
          <p>Phone Number</p>
        </div>
      </header>
    
      <body>
        <div class="body_text">
          <h3>Table 1</h3>
          <table>
            <thead>
              <tr>
                <th>Description</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Subtotal</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Website design</td>
                <td>€34.20</td>
                <td>100</td>
                <td>€3,420.00</td>
              </tr>
              <tr>
                <td>Website development</td>
                <td>€45.50</td>
                <td>100</td>
                <td>€4,550.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
            </tbody>
          </table>
    
          <h3>Table 2</h3>
          <table>
            <thead>
              <tr>
                <th>Description</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Subtotal</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Website design</td>
                <td>€34.20</td>
                <td>100</td>
                <td>€3,420.00</td>
              </tr>
              <tr>
                <td>Website development</td>
                <td>€45.50</td>
                <td>100</td>
                <td>€4,550.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
            </tbody>
          </table>
    
          <h3>Table 3</h3>
          <table>
            <thead>
              <tr>
                <th>Description</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Subtotal</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Website design</td>
                <td>€34.20</td>
                <td>100</td>
                <td>€3,420.00</td>
              </tr>
              <tr>
                <td>Website development</td>
                <td>€45.50</td>
                <td>100</td>
                <td>€4,550.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
            </tbody>
          </table>
    
          <h3>Table 4</h3>
          <table>
            <thead>
              <tr>
                <th>Description</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Subtotal</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Website design</td>
                <td>€34.20</td>
                <td>100</td>
                <td>€3,420.00</td>
              </tr>
              <tr>
                <td>Website development</td>
                <td>€45.50</td>
                <td>100</td>
                <td>€4,550.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
            </tbody>
          </table>
    
          <h3>Table 5</h3>
          <table>
            <thead>
              <tr>
                <th>Description</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Subtotal</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Website design</td>
                <td>€34.20</td>
                <td>100</td>
                <td>€3,420.00</td>
              </tr>
              <tr>
                <td>Website development</td>
                <td>€45.50</td>
                <td>100</td>
                <td>€4,550.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
            </tbody>
          </table>
    
          <h3>Table 6</h3>
          <table>
            <thead>
              <tr>
                <th>Description</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Subtotal</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Website design</td>
                <td>€34.20</td>
                <td>100</td>
                <td>€3,420.00</td>
              </tr>
              <tr>
                <td>Website development</td>
                <td>€45.50</td>
                <td>100</td>
                <td>€4,550.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
              <tr>
                <td>Website integration</td>
                <td>€25.75</td>
                <td>100</td>
                <td>€2,575.00</td>
              </tr>
            </tbody>
          </table>
        </div>
      </body>
    
      <footer>This is a footer test</footer>
    </html>
    enter image description here Fixed !