TCPDF自定义页眉和页脚
自定义页面页眉和页脚是通过扩展TCPDF类和重写header()和footer()方法定义。
require_once('/tcpdf/examples/tcpdf_include.php'); // Extend the TCPDF class to create custom Header and Footer class MYPDF extends \TCPDF { //Page header 自定义页眉 public function Header() { $this->SetFont('stsongstdlight', '', 18); //$this->SetHeaderData('', '', ''.' 001', '', array(0,64,255), array(0,64,128)); // Title $this->Cell(0, 10, 'title', 0, false, 'C', 0, '', 0, false, 'M', 'M'); $this->SetFont('stsongstdlight', '', 12); $this->SetX(1); $this->SetY(2+$this->GetY()); $this->Cell(0, 10, 'title2', 0, 0, 'C'); $this->SetX(1); $this->SetY(8+$this->GetY()); $this->Cell(180, 0, '', 1, 0, 'C', 0, '', 0, true); } // Page footer 自定义页脚 public function Footer() { // Position at 15 mm from bottom $this->SetY(-15); $this->Cell(180, 0, '', 1, 0, 'C', 0, '', 0, true); // Set font $this->SetX(1); $this->SetFont('stsongstdlight', '', 8); // Page number $this->Cell(0, 10, '123333 ', 0, false, 'C', 0, '', 0, false, 'T', 'M'); $this->SetFont('stsongstdlight', '', 8); $this->SetX(1); $this->SetY(5+$this->GetY()); $this->Cell(0, 10, '3434343 ', 0, false, 'C', 0, '', 0, false, 'T', 'M'); $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); } }
使用tcpdf
$pdf = new MYPDF(); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Nicola Asuni'); $pdf->SetTitle("标题");//标题 $pdf->SetSubject('TCPDF Tutorial'); $pdf->SetKeywords('TCPDF, PDF, example, test, guide'); //echo PDF_HEADER_LOGO;exit; // set default header data //$pdf->SetHeaderData(PDF_HEADER_LOGO, 180) // set header and footer fonts // $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); //$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, 15, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); // set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // set some language-dependent strings (optional) if (@file_exists(dirname(__FILE__).'/lang/zho.php')) { require_once(dirname(__FILE__).'/lang/zho.php'); $pdf->setLanguageArray($l); } // --------------------------------------------------------- // add a page $pdf->AddPage(); // writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') // writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true) // create some HTML content $pdf->SetFont('stsongstdlight', '', 10); $html="内容";//可以放入html标签 // output the HTML content $pdf->writeHTML($html, true, false, true, false, '');// $pdf->Output('example_006.pdf', 'I');