joining pdf files in php

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I would like to join few pdf files uploaded separetly into my website
into one downloable pdf file. Is it possible in php or is it neccessary
to download all these files one by one?

Thx for help,

Re: joining pdf files in php

Quoted text here. Click to load it

I use the free PDF library called FPDF with the FPDI extension at

This lets you create a PDF file and then import other PDF files into it.

The documentation for FPDF mentions two free tools to catenate PDF files,  
but I have no experience with them (see item #20 in the FAQ for FPDF).  

Re: joining pdf files in php

Paul Czubilinski wrote:

Quoted text here. Click to load it

If you've got Ghostscript installed on your server, it's easy enough to
join a few PDFs together...

    # Where is Ghostscript installed?
    $gs = '/usr/bin/gs';

    # Which files do we want to join together?
    # Specify them in the right order!
    $infiles = array(

    # What is the output file name?
    $outfile = '/home/me/public_html/pdf/out.pdf';

    # Assemble arguments to supply to Ghostscript.
    $args  = "-dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite ";
    $args .= "-sOutputFile='' ";
    foreach ($infiles as $f)
        $args .= "'' ";

    # Call Ghostscript to do the real work.
    system("$gs $args");

Toby A Inkster BSc (Hons) ARCS
Contact Me  ~

Re: joining pdf files in php

Paul Czubilinski wrote:
Quoted text here. Click to load it

I have a bookmark or at least example code on my computer in the office.  
I will see if I can dig it up tomorrow when I am there. I can't remember  
exactly how I went about doing it, but I think it was using an existing  
package I found on the net...

Re: joining pdf files in php

Justin Koivisto wrote:
Quoted text here. Click to load it


// This was created for use with FPDI 1.01
$dir = 'pdfs/';

$pdfs = $_POST['pdfs'];
if($pdfs) {
// this is a test file for appending pages of PDF files together to
create a new file
//    define('FPDF_FONTPATH','font/');
    require 'fpdi.php';

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

        function concat_pdf($orientation='P',$unit='mm',$format='A4') {

        function set_files($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);


    $pdf= new concat_pdf();

} else {
    echo '<form method="post" action="index.php">';
    echo '<div style="padding: 3px; font-weight: bold;">Please check the
PDFs you would like to combine into one:</div>';
    $dh = opendir($dir);
    while (false !== ($file = readdir($dh))) {
        if (preg_match('/\.pdf$/i', $file)) {
            echo '<div style="padding: 3px;"><input type="checkbox" name="pdfs[]"
value="'.$dir.$file.'" /> &nbsp;'.$file.'</div>';
    echo '<div style="padding: 3px;"><input type="submit" name="submit"
value="Submit" /></div>';
    echo '</form>';

I did notice that there is a newer version out now, but I haven't done
anything with this for about a year now...

Justin Koivisto, ZCE -

Re: joining pdf files in php

Many thanks Justin, I will try your code.


Site Timeline