【问题标题】:Add main header on first page only in TCPDF仅在 TCPDF 的第一页上添加主标题
【发布时间】:2015-04-21 23:52:28
【问题描述】:

我正在开发一个使用tcpdf 的pdf 页面来显示codeigniter 中的某些数据。它显示了数据。但不是我需要的格式。现在它在所有页面中显示具有共同标题的数据。

我只需要在第一页显示主标题,其余数据与另一个子标题

我在控制器中的代码是

<?php
tcpdf();
$obj_pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$title = 'Main title';
$obj_pdf->SetTitle($title);
$obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $title, PDF_HEADER_STRING);

$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$obj_pdf->SetFont('helvetica', '', 9);
$obj_pdf->setFontSubsetting(false);

$obj_pdf->AddPage();
ob_start();
// we can have any view part here like HTML, PHP etc

$tbl = <<<EOD
$pdf_cont
EOD;

$obj_pdf->writeHTML($tbl, true, false, false, false, '');

ob_end_clean();

$obj_pdf->Output('sample.pdf', 'I');

这里的变量$pdf_cont 包含一个要显示的大表。我无法检查(或者我不知道如何正确执行)我在哪个页面并设置标题数据。请帮我在第一页添加主标题,在页面的其余部分添加另一个标题。

提前谢谢你...

【问题讨论】:

    标签: php codeigniter header tcpdf


    【解决方案1】:

    您可以查看 tcpdf 的示例,您可以继承 TCPDF 类并使用该类:

    class YOUR_CLASS extends TCPDF {
        /**
        * Overwrite Header() method.
        * @public
        */
        public function Header() {
            // if ($this->tocpage) or
            if ($this->page == 1) {
                // *** replace the following parent::Header() with your code for TOC/page you want page
                // parent::Header();
                // this will add logo and text to first page
                $this->Image('http://localhost/first_page_logo.png', 10, 10, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
                $this->SetFont('helvetica', 'B', 14);
                $this->Cell(0, 15, 'First page header text', 0, false, 'C', 0, '', 0, false, 'M', 'M');
            } else {
                // *** replace the following parent::Header() with your code for other pages
                //parent::Header();
                // following will add your own logo ant text to other pages
                $this->Image('http://localhost/other_pages_logo.png', 10, 10, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
                $this->SetFont('helvetica', 'B', 14);
                $this->Cell(0, 15, 'Other pages header text', 0, false, 'C', 0, '', 0, false, 'M', 'M');
            }
        }
    } // end of class
    

    然后删除:

    $obj_pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    

    与:

    $obj_pdf = new YOUR_CLASS('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    

    【讨论】:

    • 我已经看过这个了。但我无法在我的示例中找到如何使用它...
    • 我在答案中添加了更多内容。
    • 非常感谢您的回答。我得到了我需要的东西,现在我正在定制它......再次感谢你
    猜你喜欢
    • 2019-03-10
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多