How to put HTML data into header of tcpdf?

PHPLover picture PHPLover · Jan 24, 2013 · Viewed 30.3k times · Source

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

Answer

Arturo picture Arturo · Jan 25, 2013

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