Write html to custom header tcpdf
Asked Answered
S

3

7

I'm struggling to understand how the header function is working in tcpdf.

Is is possible to use $pdf->writeHTML to the header?

http://www.tcpdf.org/examples/example_003.phps

I would like to display 3 columns in the header.

 Column1 Column2 Column3
Sair answered 22/9, 2014 at 13:15 Comment(0)
S
14

Problem solved, credits to Simon @ https://sourceforge.net/p/tcpdf/discussion/435311/thread/505a9e13/

class MYPDF extends TCPDF {
public function Header() {
    $headerData = $this->getHeaderData();
    $this->SetFont('helvetica', 'B', 10);
    $this->writeHTML($headerData['string']);
}
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setHeaderData($ln='', $lw=0, $ht='', $hs='<table cellspacing="0" cellpadding="1" border="1"><tr><td rowspan="3">test</td><td>test</td></tr></table>', $tc=array(0,0,0), $lc=array(0,0,0));
Sair answered 23/9, 2014 at 15:49 Comment(5)
Do you know how to make Header() customisable with parameters?Heathenize
I've found that if you want to customize, just create a function that uses writeHTMLCell() and calling it directly after addPage(). I have yet to find a way to write dynamic headers with TCPDF. Writing the HTMLCell allows me to just pop the same sized HTML in all necessary pages. If you have dynamic content that will break pages then this won't work :( I have yet to try footers. I don't see it being so simple.Caribbean
I tried t his solution just blank. Nothing shows on header section.Pasto
Uncaught Error: Class 'TCPDF' not found in. always show this error.Rising
Uncaught Error: Class 'TCPDF' not found in. always show this error.Rising
T
6

Problem solved, this method is less complex and is more direct in insert HTML code in header

class MYTCPDF extends TCPDF {

  public function Header(){
     $html = '<table cellspacing="0" cellpadding="1" border="0"><tr><td rowspan="3">test</td><td>test</td><td>test</td></tr></table>';
     $this->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'top', $autopadding = true);
  }
    }
Tonality answered 15/4, 2015 at 14:16 Comment(0)
G
0

this way works as well:

class MYPDF extends TCPDF{
    public function Header(){
        $html = '<table cellspacing="0" cellpadding="1" border="0"><tr><td rowspan="3">test</td><td>test</td><td>test</td></tr></table>';
        $this->writeHTML($html, true, false, false, false, '');
    }
}


$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setPrintHeader(true);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->Output('pdfHeader.pdf', 'I');
Gombach answered 6/6, 2021 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.