TCPDF - Header image only displays on first page
Asked Answered
A

3

5

I am using TCPDF to generate a 2 page pdf document.

I have added a header and a footer to the document. The text part of the header and footer shows up correctly on each page however when I include an image logo in the header it is only showing up on the first page.

   public function Header()
{
    $this->Image('/home/xxxxxx/public_html/xxxxxxxx/uploads/logo/logo.png',10,6,0,13);

    $this->SetFont('helvetica','B',20);
    $this->Cell(80);
    $this->Cell(0,0, $project->name . ' - Project Plan',$frame,0,'R');
    $this->Ln(8);
    $this->SetFont('helvetica','',10);
    $this->Cell(0,0, $organisation->name,$frame,0,'R');
    $this->Ln(10);
}

Does anyone have any idea what I am doing wrong here?

Thanks

Apologia answered 5/10, 2018 at 9:17 Comment(1)
UPDATE: This appears to only happen with png images - I have tried with a jpg image and it works correctly. I guess this is a bug? Does anyone know a way around this?Apologia
K
10

I think is not related to header/footer, but I think TCPDF has bug that broken Image function with same image file loaded multiple times as reported here TCPDF - image displayed only once

bug also present on actual version tecnickcom/tcpdf:6.2.26

I resolve this problem by loading image outside and pass to the function as string.

public function Header()
{
    $this->Image('@'.file_get_contents('/home/xxxxxx/public_html/xxxxxxxx/uploads/logo/logo.png'),10,6,0,13);

    $this->SetFont('helvetica','B',20);
    $this->Cell(80);
    $this->Cell(0,0, $project->name . ' - Project Plan',$frame,0,'R');
    $this->Ln(8);
    $this->SetFont('helvetica','',10);
    $this->Cell(0,0, $organisation->name,$frame,0,'R');
    $this->Ln(10);
}
Kleiman answered 20/6, 2019 at 8:24 Comment(1)
This does work for me (similar issue: stackoverflow.com/q/59720761/209139), but the PDF takes a long time to generate. Has this bug actually been filed with TCPDF anywhere, do you know?Available
S
3

As said it is a tcpdf bug. In my case I had two logos in header one png and the other jpg. The problem happened only with the .png. Changing the image type from png to jpg solved the issue.

Salesin answered 2/4, 2020 at 13:1 Comment(1)
This may be related to missing images after usage of page-break-inside: avoid or nobr="true" as stated in github.com/tecnickcom/TCPDF/pull/121. Changing the filetype or removing transparency does indeed help.Abran
L
1

I solved this by using base64 encoded string like this:

$image = base64_encode(file_get_contents('path_to_image'));

Then you can use it like this:

<img src="@<?= $image ?>" />
Lawerencelawes answered 15/9, 2020 at 15:25 Comment(1)
This works for me, thanksOverweigh

© 2022 - 2024 — McMap. All rights reserved.