34

I am trying to create a header in TCPDF, however it always have a border underneath of it. Is there a way I can remove the bottom border?

Stavr00
  • 3,219
  • 1
  • 16
  • 28
Bill
  • 17,872
  • 19
  • 83
  • 131
  • 3
    class MYPDF extends TCPDF { public function Header() { $image_file = K_PATH_IMAGES.'pdf-header.jpg'; $this->Image($image_file, 160, 10, 40, '', 'JPG', '', 'T', false, 20, '', false, false, 0, false, false, false); $this->SetFont('helvetica', 'B', 10); } public function Footer() { $this->SetY(-15); $this->SetFont('helvetica', 'I', 8); } } – Bill Nov 02 '11 at 23:49
  • try the code above should eliminate the problem – Bill Nov 02 '11 at 23:49
  • 1
    An alternative which doesn't involve editing the tcpdf class or extending it is presented in [this answer](https://stackoverflow.com/questions/2217132/changing-or-eliminating-header-footer-in-tcpdf) – Ian Gregory Jun 18 '12 at 21:12

6 Answers6

129

This works for some versions:

// Call before the addPage() method
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
Bert H
  • 1,087
  • 1
  • 15
  • 29
András
  • 3,395
  • 1
  • 21
  • 27
  • 16
    This works fine but I would like to specify what these lines must be added before calling AddPage() method. – Dayron Gallardo Aug 05 '13 at 15:30
  • I think there are other default values – András Sep 12 '13 at 07:37
  • Not sure why the above answer was marked as correct, this is the correct answer. Thank you. – user1785684 Dec 04 '18 at 09:55
  • There is no configuration constant to tell that to TCPDF? Because this means you have to put these 2 lines for every TCPDF usage, which can be painful if you have several PDF exports – Xenos Mar 13 '19 at 10:32
  • 2
    @Xenos You can always subclass TCPDF and override the `->AddPage()` calling those two lines, and then calling the parent. – Xavi Montero May 01 '19 at 16:10
  • @XaviMontero which means I'll have to change all TCPDF instantiation in favor of the subclas instance... It's a way to bypass this issue but this can only be done before using TCPDF, and no afterward (when poor legacy architecture wasn't already using a subclass nor a central factory)... – Xenos May 02 '19 at 07:32
  • @Xenos Well... if the code is "very legacy" and does a "new" all over there, I see one fast way to change this: Make your class to also be named TCPDF but locate it in another namespace such as `\MyApp\Pdf`. Then search for all the files containing `new TCPDF` or there any static usages `TCPDF::` and just at the beginning of the file place `use \MyApp\Pdf\TCPDF`. I'd would be great to use Dependency Injection if you could; but in the meantime you refactor, this does the trick. – Xavi Montero May 03 '19 at 09:15
38

If you don't want to subclass or change the tcpdf source just call the setHeaderData method and specify white line color.

$pdf->setHeaderData('',0,'','',array(0,0,0), array(255,255,255) );  
James
  • 12,636
  • 12
  • 67
  • 104
  • 2
    This actually answers the question. If you are, however, looking to completely disable the Header and Footer (like I was), then the answer from @András is what you want. – Ethan Hohensee Apr 18 '17 at 02:37
  • Not worked for me dear, still showing a horizonal row / line in header section of pdf. Thanks. – Kamlesh Aug 17 '23 at 11:33
  • https://stackoverflow.com/a/17172044/10329023 solution worked for me. Thanks – Kamlesh Aug 17 '23 at 11:34
4

Problem solved by extends the TCPDF class and modify the header and footer.

class MYPDF extends TCPDF { 

    public function Header() 
    { 
        $image_file = K_PATH_IMAGES.'pdf-header.jpg'; 
        $this->Image($image_file, 160, 10, 40, '', 'JPG', '', 'T', false, 20, '', false, false, 0, false, false, false); 
        $this->SetFont('helvetica', 'B', 10); 
    } 

    public function Footer() 
    { 
        $this->SetY(-15); 
        $this->SetFont('helvetica', 'I', 8); 

    }
}
fkerber
  • 1,032
  • 9
  • 24
Bill
  • 17,872
  • 19
  • 83
  • 131
3

tcpdf.php:

// print an ending header line
$this->SetLineStyle(array('width' => 0.25 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 255, 255)));
Patrik Ján
  • 110
  • 3
0

If the other solutions posted in this thread does not work, I solved in this way:

TL;DR
In Footer() function in Tcpdf class (tcpdf.php):
Replace this lines:

$this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L'); // line 3527 in version 6.3.1
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R'); // line 3530 in version 6.3.1

With this lines

$this->Cell(0, 0, $pagenumtxt, 0, 0, 'L');
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 0, 0, 'R');

Alternative way 1
Comment this lines in Footer() function in Tcpdf class (tcpdf.php):
In my file (version 6.3.1) they were placed at line 3524

//Print page number
if ($this->getRTL()) {
    $this->SetX($this->original_rMargin);
    $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
} else {
    $this->SetX($this->original_lMargin);
    $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
}

This will disable the render of the page number in the footer but at least it removes the unwanted line.

Alternative way 2
If these methods does not work, search in the tcpdf.php file for this string:

$this->SetLineStyle(array

You should find 3 occurrences, replace the 'color' property of the array with the value [0,0,0] (or the rgb color of your background), this should cause the line to turn white (or the color you set). I used this method to troubleshoot where the problem was by putting custom weird color and seeing which one was being rendered.

Explanation
The line is rendered because in the lines

$this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
$this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');

The border property is set to 'T' (top border). You can disable the border by set the border property to 0 (see docs here, $border param). If that doesn't work you can entirely disable the render of the page number (and thus the border), or you can set the border to a custom color that matches you actual background.

-1

Comment this line in Header() function of tcpdf Class :

$this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
Kjuly
  • 34,476
  • 22
  • 104
  • 118
Rkk
  • 1