Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDI merge PDF files, strange line appears

Tags:

php

pdf

fpdi

I have to merge PDF files when a user needs to. The files are already existing and everything is fine. I'm using the fallowing code to merge the files:

class concat_pdf extends FPDI 
{
    var $files = array();

    function setFiles($files) 
    {
        $this->files = $files;
    }

    function concat() 
    {
        foreach($this->files AS $file) 
        {
            $pagecount = $this->setSourceFile($file);

            for($i = 1; $i <= $pagecount; $i++) 
            {
                $this->AddPage('P');
                $tplidx = $this->ImportPage($i);
                $this->useTemplate($tplidx);
            }
        }
    }
}

$pdf = new concat_pdf();
$pdf->setFiles($files); //$files is an array with existing PDF files.
$pdf->concat();
$pdf->Output("bulk.pdf", "D");

All files are merged and all the content is there. The problem is, at the top of each page in the new file, a black line appears. The contents, margins, etc. are all absolutely the same as the original file, but this line comes out of nowhere (that I can tell). It is not thick, but is clearly visible. It doesn't mess with the other content or anything, but is not needed there and I need to remove it.

I've tried changing the second parameter to the ImportPage() function to all the options described in the documentation, but there's no difference whatsoever. Since this is the only thing that I see I can change in this few lines of code, I really don't know what is causing the black line to appear. I've searched for similar issues, but so far - no luck. Anyone have an idea? Thanks in advance!

beforeafter

like image 684
Nikoloff Avatar asked May 09 '12 20:05

Nikoloff


2 Answers

A better thing to do as you won't have to modify the source is to add the lines:

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

at the beginning of your concat() function.

like image 152
Robert Avatar answered Nov 18 '22 07:11

Robert


To avoid editing the TCPDF library, overwrite the methods Footer and Header in your extended class.

class concat_pdf extends FPDI 
{
    public function Footer() {}
    public function Header() {}
}
like image 40
Javier Tacón Avatar answered Nov 18 '22 07:11

Javier Tacón