mpdf / mpdf

PHP library generating PDF files from UTF-8 encoded HTML
https://mpdf.github.io
GNU General Public License v2.0
4.37k stars 1.06k forks source link

First header of named page doesn't apply styles correctly #1320

Closed antman3351 closed 3 years ago

antman3351 commented 3 years ago

Hello, I have a problem with the first header of a named page. Mpdf doesn't apply font-weight style correctly.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>title</title>
    <style>
        @page {
            size: 210mm 297mm;
            margin-top: 10mm;
            margin-right: 10mm;
            margin-bottom: 10mm;
            margin-left: 10mm;
        }

        @page P_mpdf_page_27 {
            margin-top: 81mm;
            odd-header-name: H_mpdf_section_22;
            even-header-name: H_mpdf_section_22;
        }

        #mpdf_page_27 {
            page: P_mpdf_page_27;
        }

        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
        }

        #Line1 {
            font-size: 10px;
            font-weight: normal;
        }

        #Line2 {
            font-size: 12px;
            font-weight: normal;
        }

        #Line3 {
            font-size: 14px;
            font-weight: normal;
        }

        #Line4 {
            font-size: 16px;
            font-weight: normal;
        }

    </style>
</head>
<body>
<section id="mpdf_page_27">
    <htmlpageheader name="H_mpdf_section_22">
        <div id="Line1">Line 1</div>
        <div id="Line2"> Line 2</div>
        <div id="Line3"> Line 3</div>
        <div id="Line4"> Line 4</div>
    </htmlpageheader>
    <div style="height:100mm; background-color: aqua;">foo</div>
    <div style="height:100mm; background-color: hotpink;">bar</div>

    <div style="height:100mm; background-color: greenyellow;">foo</div>
    <div style="height:100mm; background-color: skyblue;">bar</div>
</body>
</html>

In the attached image you can see how the 1st page is bolder (it's not quite a bold, but not normal)

image

Thanks!

antman3351 commented 3 years ago

I think the problem is the 1st head is being write twice. If you open the document with LibreOffice Draw you can see the elements of the 1st header are double while the 2nd page header is correct.

antman3351 commented 3 years ago

I did some more digging.

So in my case the first "block" is a named page, on every new named mpdf adds a new page by calling AddPage() that will add a new page unless it's the first page an is empty ( I think that's what the check if (!($pagesel && $this->page == 1 && (sprintf("%0.4f", $this->y) == sprintf("%0.4f", $this->tMargin)))) { in _beginpage() does. So a new page may or may not be added, but AddPage() adds $this->writer->write( '___HEADER___MARKER' . $this->uniqstr ); without checking, so in my case ___HEADER___MARKER' . $this->uniqstr is added again.

I think

$this->writer->write( '___PAGE___START' . $this->uniqstr );
$this->writer->write( '___BACKGROUND___PATTERNS' . $this->uniqstr );
$this->writer->write( '___HEADER___MARKER' . $this->uniqstr );

should be moved into the _beginpage() function inside the first if or you can check that the $this->page has been incremented after calling _beginpage() if it has to be written after the template import.

With that change the heads work for me. Is this fix ok?