Zwen2012
Zwen2012

Reputation: 3498

TCPDF: Pdf has always a hr-line on the top

I generate my PDF with TCPDF. I have always an horizontal line on the top, but I never created it. In my $renderedView is simple text. Can someone tell me, where this line comes from? Thanks for any help!

$pdf = new \TCPDF();
        $pdf->AddPage();
        $pdf->SetFont('courier', '', 9);
        $pdf->SetAuthor('Me');
        $pdf->writeHTML($renderedView, true, 0, true, 0);
$pdf->Output($filePath, 'F');

Upvotes: 2

Views: 5514

Answers (2)

Satizh J
Satizh J

Reputation: 317

I usually use this format to edit header and footer,

<?php
require_once('functions/TCPDF/tcpdf.php');
$renderedView="text";
// Extend the TCPDF class to create custom Header and Footer
    class MYPDF extends TCPDF {

    //Page header
    public function Header() {

      $this->SetFont('Gotham Medium', 'C', 50);
      $this->SetTextColor(209,183,49);

      $this->Ln(5);        
      $this->Cell(278, 15, 'custom header', 0, false, 'C', 0, '', 0, false, 'M', 'M');

    }

    // Page footer
    public function Footer() {
        $this->SetY(-15);
       $this->SetFont('helvetica', 'I', 10);
      $this->Cell(278, 15, 'custom footer', 0, false, 'C', 0, '', 0, false, 'M', 'M');

    }
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'UTF-8', false);


        $pdf->AddPage();
        $pdf->SetFont('courier', '', 9);
        $pdf->SetAuthor('Me');
        $pdf->writeHTML($renderedView, true, 0, true, 0);
        $pdf->Output($filePath, 'F');

By default TCPDF will include header and footer. that is the reason for that horizontal line. however you can customise the header and footer in above code.

For example,

  1. to disable header, use the below code.

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'UTF-8', true);

  2. to disable footer, use THE below code.

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

  3. to disable both, use.

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'UTF-8', false);

As you can see the first boolean indicates the header and the second boolean indicates the footer in your pdf file.

Upvotes: 3

Jan Slabon
Jan Slabon

Reputation: 5058

The comment from @Jaydeep Mor is the more correct answer to this problem. It is not required to extend the class but disabling the internal method execution is possible by these method calls:

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

Upvotes: 7

Related Questions