How to put HTML data into header of tcpdf?
Asked Answered
S

4

13

I'm using the tcpdf library to generate the pdf document. I'm using smarty template engine to hold the data. Below is the script to put in the header data:

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'PQR', 
'XYZ');

I want to put HTML table content of smarty template in place of XYZ, the table content is going to be dynamic(meaning the data in table may vary for each PDF document).

Sensibility answered 24/1, 2013 at 7:4 Comment(1)
Please look into similar issue below #9513645Phillie
D
21

As @vinzcoco says, you must extend TCPDF to achieve what you want. Here is a simple improvement that I think it could be useful for you:

class MyTCPDF extends TCPDF {

    var $htmlHeader;

    public function setHtmlHeader($htmlHeader) {
        $this->htmlHeader = $htmlHeader;
    }

    public function Header() {
        $this->writeHTMLCell(
            $w = 0, $h = 0, $x = '', $y = '',
            $this->htmlHeader, $border = 0, $ln = 1, $fill = 0,
            $reseth = true, $align = 'top', $autopadding = true);
    }

}

Now, once you've got your MyTCPDF object available, you just need to do this to set the HTML header content:

$mytcpdfObject->setHtmlHeader('<table>...</table>');

and the HTML content won't be hardcoded into the Header() method (more flexible for you).

Doughy answered 25/1, 2013 at 9:48 Comment(2)
-1, not sure how this is supposed to work. If you call writeHTMLCell with $html, it will always be the same, regardless of what you set in setHtmlHeader().Cliquish
not working for me. anyone can explain, how make this?Thaw
M
6

I used the following method to set header

$PDF_HEADER_LOGO = "logo.png";//any image file. check correct path.
$PDF_HEADER_LOGO_WIDTH = "20";
$PDF_HEADER_TITLE = "This is my Title";
$PDF_HEADER_STRING = "Tel 1234567896 Fax 987654321\n"
. "E [email protected]\n"
. "www.abc.com";
$pdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING);

This is work for me.

Miles answered 17/12, 2014 at 9:0 Comment(0)
N
5

You must instance your PDF class and extend the TCPDF class. After your PDF class should look like this:

class MyTCPDF extends TCPDF{
  public function Header(){
     $html = '<table>...</table>';
     $this->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'top', $autopadding = true);
  }
}

You must adapt this to your own project.

Numerator answered 25/1, 2013 at 8:54 Comment(0)
S
-1

First, you need to copy your image into folder images at examples folder like this :

yourimage.jpg into library folder/examples/images

Then you shoud use this code :

 $pdf->SetHeaderData('../images/yourimage.jpg', '140', '', '', array(0, 0, 0), array(255, 255, 255));
Sclerite answered 3/7, 2023 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.