【问题标题】:TCPDF Header and Top Margin on fist page only then no header and different top margin on remaining pagesTCPDF Header 和 Top Margin 仅在第一页上,然后没有 header 和不同的 top margin 在其余页面上
【发布时间】: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


    【解决方案1】:

    这样的事情对我有用:

    public function Header() {
        if ($this->page == 1) {
            $html .= 'header text';
            $this->writeHTML($html, true, false, false, false, '');
        } else {
            $this->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
        }
    }
    

    只在第一页上写标题 HTML,然后更改其他页面的边距。

    【讨论】:

      猜你喜欢
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多