【发布时间】:2020-03-06 16:18:07
【问题描述】:
我正在使用 tcpdf 生成 pdf。我只希望页眉出现在第一页并拥有它,这样它就不会出现在其余页面上。但是,我希望剩余页面的上边距向上移动,因为这些页面上没有标题。
我正在使用 MYPDF 扩展 TCPDF 以自定义标题以仅显示在第一页上。
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
//Page header
public function Header() {
if (count($this->pages) === 1) { // Do this only on the first page
$html .= 'header text';
}
$this->writeHTML($html, true, false, false, false, '');
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// 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, 25, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(10);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 20);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('droidsansfallback', '', 10);
// add a page
$pdf->AddPage();
$pdf->resetColumns();
$pdf->setEqualColumns(3, 57); // KEY PART - number of cols and width
$pdf->selectColumn();
$content = '';
$content .= '
<table cellspacing="0" cellpadding="3">
';
$content .= fetch_data();
$content .= '</table>';
$pdf->writeHTML($content);
$pdf->Output('file.pdf', 'I');
【问题讨论】:
标签: tcpdf