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).

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
PHPLover
  • 1
  • 51
  • 158
  • 311
  • 1
    Please look into similar issue below http://stackoverflow.com/questions/9513145/tcpdf-set-different-headers-for-different-pages-in-one-document – Minesh Jan 24 '13 at 07:08

4 Answers4

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).

Dirk
  • 10,668
  • 2
  • 35
  • 49
Arturo
  • 713
  • 7
  • 14
  • 4
    -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()`. – slhck Dec 10 '13 at 12:33
  • not working for me. anyone can explain, how make this? – Noufal Binu Apr 30 '21 at 14:03
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 abc@gmail.com\n"
. "www.abc.com";
$pdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING);

This is work for me.

vijay
  • 731
  • 5
  • 15
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.

vinzcoco
  • 89
  • 1
  • 9
-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));
Antoine Subit
  • 9,803
  • 4
  • 36
  • 52