Gianluca78
Gianluca78

Reputation: 784

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 example of the unwated lines in my pdfs.

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!

Upvotes: 2

Views: 1072

Answers (1)

user5667374
user5667374

Reputation:

Just say to TCPDF that dont print the header or go and modify the source....

$pdf->SetPrintHeader(false);

Upvotes: 1

Related Questions