Concatenate PDF in PHP

As requirement for one project we need it to concatenate PDF files to have just one file as input.

First download in your work space TCPDF and FPDI

Class to concatenate pdf, pdfConcat.php:

require_once("tcpdf/tcpdf.php");
require_once("fpdi/fpdi.php");
 
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++) {
                    $tplidx = $this->ImportPage($i);
                    $s = $this->getTemplatesize($tplidx);
                    $this->AddPage(’P', array($s['w'], $s['h']));
                    $this->useTemplate($tplidx);
               }
          }
     }
}

Usage:

include_once("pdfConcat.php");
$pdf =& new concat_pdf();
$pdf->setFiles(array("doc.pdf","pauta.pdf", "4bp.pdf", "5bp.pdf"));
$pdf->concat();
$pdf->Output("newpdf.pdf", "I");

After this I recommend to you to keep the file already merged in the server, then you will not need to generate it another time. To do this you can use $pdf->Output(“newpdf.pdf”, “F”); instead.

Related Posts Related Websites
Help sharing and Flatter me ;)

20 Comments

  1. Jim Geraci says:

    First off, AWESOME!

    Second, is there a way to check for a returned error? I’m using this on a form and I want to have the user return to the previous page to try again if the creation fails.

    Thanks,
    Jim

  2. Chema says:

    Thanks!

    ufff, I’m sorry now I really can’t remember but I’m certain there’s a way :S

  3. Balkanski says:

    First of all, great script!

    Second, I have a question:
    When I concatenate pdf files I get one horizontal line on each page just above my header.
    It is situated 2 or 3 mm from the top end of the paper.

    Is it a feature or a bug, and if the first, is there a way I could hide it?

    Thanks in advance and keep writing such great scripts.

  4. Chema says:

    No there’s nothing in my code that is doing that.

    Regards and thanks!

  5. Balkanski says:

    Hi again.

    Here is what I have done step by step:

    Download TCPDF – Version: 4.8.031
    Download FPDF_TPL-1.1.4
    Download FPDI_1.3.2

    Extract the archives in one dir in my localhost workspace.

    Move ‘fpdf_tpl.php’ file to ‘FPDI_1.3.2′ dir in order it could be loaded properly.

    Create an index.php file.

    Copy the upper ‘concat_pdf’ class in it.

    Copy the upper ‘Usage’ code.

    Use a standart PDF file in the setFiles() method, downloaded from the web, not previously generated with TCPDF.
    (By the way, If I use a file, which is generated by TCPDF, the result is the same)

    Launch the script.

    The output is the same as the source PDF file with one difference – on each page there is a line 2 or 3 mm below the top edge of the page. It starts from the ‘left_margin’ position and ends to the ‘right_margin’ position.

    If we use TCPDF to build a page, the same result may be achieved by the following code used in the Header() method:

    $this->Line($this->left_margin, 3, $this->getPageWidth() – $this->right_margin, 3);

  6. Chema says:

    I think you should go to the support forums of TCPDF, no?

  7. Balkanski says:

    I solved the problem by using FPDF and ignoring the ‘fpdi2tcpdf_bridge.php’ script, which is loaded by default if an already included TCPDF class is found.

    Actually, the original example uses it, instead of TCPDF.

    http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/

    Now the annoying line above the header is gone.

  8. adrianbj says:

    Alternative way to get rid of the annoying horizontal line in the header if you want to keep using TCPDF

    Just add these lines after: $pdf =& new concat_pdf();

    // remove default header/footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);

  9. Balkanski says:

    Thanks from me too :)

  10. Jez says:

    And thanks from me. :D
    This has been driving me nuts for weeks.

  11. Jan Thelen says:

    Many thanky from Germany

  12. Ryan Dagey says:

    Hi, I was working out your example on our server where we need this sort of thing & I’m having some trouble. The TCPDF examples from the install works fine, so I’m pretty sure TCPDF is working right, and I don’t know how to test the FPDI. The problem is that the concatenation builds a blank document, always no matter what I try. I’ve tried with a minimum of just a couple of documents, but I always get the same output: blank. I’m just wondering if you have any suggestions I can try, as FPDI doesn’t seem to have good support options, and I’m pretty sure TCPDF is working OK. Maybe some way to test if FPDI is working or some suggestions on debugging steps? Thanks if you have any time to help, this would be a great replacement for pdftk!

  13. Chema says:

    Hi Ryan, right now nothing come across my mind, but you can check in the FPDI web for examples of work, this code worked for me anyway ;)

    regards

  14. adrianbj says:

    $pagecount = $pdf->setSourceFile($filepath);

    will return the number of pages in a document and will confirm that FPDI is working.

    HTH

  15. Ryan Dagey says:

    Thanks for the suggestion adrianbj, that is returning the right number of pages (each PDF we want to concatenate is 1 page). The PDF that appears for either download or on the server has the right number of pages (one per file), it’s just that each page is completely blank. I setup a test environment to try to work on this located here: http://ryan.neoterichovercraft.com/pdf/ (the execution file is pdf.php)

    I changed it to just try to concatenate the same file twice to see if that would help, but no go, here’s the PHP that I’m using:
    files = $files;
    }

    function concat() {
    foreach($this->files AS $file) {
    $pagecount = $this->setSourceFile($file);
    for ($i = 1; $i ImportPage($i);
    $s = $this->getTemplatesize($tplidx);
    $this->AddPage(‘P’, array($s['w'], $s['h']));
    $this->useTemplate($tplidx);
    }
    }
    }
    }

    $pdf =& new concat_pdf();
    $pdf->setFiles(array(’1.pdf’, ’1.pdf’));
    //$pdf->setFiles(array(’1.pdf’, ’2.pdf’, ’3.pdf’));
    $pdf->concat();
    $pdf->Output(“output.pdf”, “I”);
    ?>

    I’m sure I’m missing obvious, but it’s eluding me.

  16. adrianbj says:

    Ryan,

    A couple of thoughts:

    Do you have FPDF_TPL on your server?

    The basic free version of FPDI only supports PDF format up to 1.4 (v5): http://www.setasign.de/products/pdf-php-solutions/fpdi/downloads/ You can pay for a version that support more recent PDF file formats, or just make sure to save all the PDFs in this older version.

  17. adrianbj says:

    Something else to look at – are you pointing to the full server path of the PDFs (ie /var/www/pdfs/1.pdf etc)?

    Also, is PHP logging any errors?

  18. Ryan Dagey says:

    Thanks for the assistance, yes FPDF_TPL is on the server here: http://ryan.neoterichovercraft.com/pdf/fpdi/fpdf_tpl.php (my basic understanding is that tcpdf.php is included first, within my PHP, providing the TCPDF class, followed by fpdi.php which includes fpdf_tpl.php providing FPDF_TPL as an extension of FPDF that itself is defined as an extension of TCPDF via the file fpdi2tcpdf_bridge.php). Originally, I had not downloaded fpdf_tpl.php and the system generated an error.

    I downloaded FPDF_TPL along with FPDI and inserted fpdf_tpl.php into the fpdi/ directory. I think that’s the right location? As a workaround, I did try to use FPDF directly based on a comment above (not using the bridge), but the result kept giving me an error the “D”/”I”/”F” was not a valid output parameter.

    The PDFs would be newer PDFs generated by BullZip (1.5 I believe), so I used BullZip and converted the PDFs (originally Word documents) to 1.4 and then to 1.3, neither of those options seemed to make a difference, the final output was still blank.

  19. Ryan Dagey says:

    No, the PDFs are located in the same directory as the testing structure I setup, so the array is just array(“1.pdf”, “1.pdf”). I can try full path to see if it makes a difference, and there are no errors on from the script itself, I’m not sure if PHP logs anywhere via syslog.

Leave a Reply

Follow me