0
votes

I have to merge a number of pdf files, and am using the solution detailed here to build a command to Ghostscript:

// $output_file_name = 'test_file_complete.pdf'
// $original_file = 'test_file.pdf'
// $append_files = array('../toappend/1.pdf', '../toappend/2.pdf', '../toappend/3.pdf', '../toappend/4.pdf')
public function merge_documents($output_file_name, $original_file, $append_files) 
{
    $cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$output_file_name ";

    $cmd .= $original_file . " ";

    foreach($append_files as $file) {
        $cmd .= $file . " ";
    }

    $result = shell_exec($cmd);
 }

The $cmd string I build is:

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=test_file_complete.pdf test_file.pdf ../toappend/1.pdf ../toappend/2.pdf ../toappend/3.pdf ../toappend/4.pdf

Note that the first file to be merged is in the current directory, and the others are up a level in another directory.

In the command line, when I navigate to the directory of the original file and run this, it works perfectly. However, in my function it fails with the message:

(string) Error: /undefinedfilename in (TEMP_quote_2014_09_05_13_16_04.pdf) Operand stack:

Execution stack: %interp_exit .runexec2 --nostringval--
--nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push Dictionary stack: --dict:1162/1684(ro)(G)--
--dict:0/20(G)-- --dict:77/200(L)-- Current allocation mode is local Last OS error: 2

If I run that code using only the files in the "toappend" directory, it will merge those fine.

1

1 Answers

0
votes

If I use absolute paths, this ceases to be an issue. It's no problem for me to do this, however this info on how ghostscript finds files might come in handy for people having a similar issue.