Merge multiple PDF files into one with PHP Ghostscript
<p>I'm trying to merge multiple PDF files on my server using Ghostscript with the following code :</p>

<pre><code>$files = array(
        '1.pdf',
        '2.pdf',
        '3.pdf'
);

$output = 'single.pdf';

$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$output ";

foreach($files as $file)
    $cmd .= "pdfs/$file ";

var_dump(shell_exec($cmd));
</code></pre>

<p>The script that I use is in the root folder and the PDF files are in a folder named <em>pdfs</em> :</p>

<pre><code>/[pdfs]
|__ 1.pdf
|__ 2.pdf
|__ 3.pdf
script.php
</code></pre>

<p>The permissions for the folders and files are all <code>777</code>.</p>

<p>But despite all this, nothing is happening and the returned value is <code>NULL</code>.</p>

<p>So what am I doing wrong ? What is the exact way to merge multiple PDF files into one using Ghostscript ?</p>