简体   繁体   中英

Unwanted lines in TCPDF class with custom header and footer

I'm using TCPDF to generate pdf reports. I need custom headers and footers, so I extended the original class to overwrite the Header and Footer methods as suggested in the official documentation ( https://tcpdf.org/examples/example_002.phps ).

Here you are the code:

class AppPdf extends \TCPDF {

    CONST LOGO_PATH =  '/../../../public_html/public/images/logo-big.png';

    private $xLogo;
    private $yLogo;
    private $wLogo;

    public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false, $xLogo = 8, $yLogo = 0, $wLogo = 50) {
        parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);

        $this->xLogo = $xLogo;
        $this->yLogo = $yLogo;
        $this->wLogo = $wLogo;
    }

    public function Header() {
        $this->Image(__DIR__ . self::LOGO_PATH, $this->xLogo, $this->yLogo, $this->wLogo);
    }

    public function Footer() {
        $this->SetXY(34,260);
        $this->SetFont('Helvetica', 'I', 8);

        $this->SetTextColor(0, 0, 0);
        $this->MultiCell(130, 20, "footer text", 0, "C", false);
    }

} 

Then I have a base template that it is used for all the generated documents:

class BasePdf {

    CONST CREATOR = 'Creator';
    CONST TITLE = 'Title';
    CONST PDF_FONT_NAME_MAIN = 'Times';
    CONST PDF_FONT_SIZE_MAIN = 11;

    protected $pdf;

    public function __construct($xLogo = 8, $yLogo = 0, $wLogo = 50)
    {
        $this->pdf = new AppPdf('P', 'mm', 'A4', true, 'UTF-8', false, false, $xLogo, $yLogo, $wLogo);
        $this->pdf->SetCreator(self::CREATOR);
        $this->pdf->SetAuthor(self::CREATOR);
        $this->pdf->SetTitle(self::TITLE);

        $this->pdf->SetFont(self::PDF_FONT_NAME_MAIN, "", self::PDF_FONT_SIZE_MAIN);
    }

    public function getPdf()
    {
        return $this->pdf;
    }
}

The base template is used as shown in the following class:

use AppBundle\Entity\HPVExam;

class HPVReport extends BasePdf
{
    public function __construct(HPVExam $HPVExam)
    {
        parent::__construct(8, 10, 75);

        $this->pdf->AddPage();
    }
}

The problem is that this code generates pdfs with an annoying horizontal line in the top and another one in the footer, as you can see in the following image 我的pdf中无用的行的示例 .

I have already tried the suggestions provided here PHP / TCPDF: Template Bug? and here Changing or eliminating Header & Footer in TCPDF but without luck.

What I'm doing wrong? It seems that the original Header and Footer methods are not correctly overwritten... Any idea? Thank you!

只需对不打印标题的TCPDF说或去修改源代码即可。...

$pdf->SetPrintHeader(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM