Merging PDF Files

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply

Merging PDF Files

Post by tome100 » 2007-01-03T10:16:33-07:00

I am trying to merge PDF files with imagemagick by means of convert. This works fine when I run the command direct from SSH, but when I run the exact same command from PHP, it doesn't work. Another thing is that the exec("convert string", $returnvar); $returnvar never returns anything even if it doesn't / does work. I know that imagemagick is installed and working with PHP, cause I can convert TIF's to PDF's and even merge multiple tifs to pdfs. I just cant merge multiple pdfs into one.

User avatar
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2007-01-03T19:04:03-07:00

This is a BAD idea.

See IM Examples... A word about Vector Image formats ... ts/#vector
Anthony Thyssen -- Webmaster for ImageMagick Example Pages


Post by tome100 » 2007-01-04T08:50:31-07:00

Regaurdless, I still need to merge the PDF files. I know the merging works fine. I am converting TIF's to PDF's, then allowing them to merge PDF's for easy viewing. "Bad" idea or not, I still need it to work. And it works fine from SSH, but wont work in PHP.


Post by redace » 2007-01-04T09:04:02-07:00

tome100 wrote: Regaurdless, I still need to merge the PDF files.

You need somthing that works directly on vector files, since Imagmagick rasterizes PDFs before working with them.

A quick google reveals tools such as You might want to try that route.

Another route would be to merge the seperate tif files into one multipage tif, then saving as PDF.


Post by tome100 » 2007-01-04T12:33:50-07:00

The problem is not imagemagick. I was looking at the log files, and it appears when the "apache" user attempts to convert, it doesn't have access to ghostscript. ("gs: command not found") Any idea why? Ghostscript is installed and works from SSH as "root" user.


Post by oddball508 » 2007-01-04T14:23:38-07:00

Having done a lot of PDF merges in recent time (1,500,000 single page pdf files into 17,800 combined files) I found that the about PDFTK was the best. Unlike ImageMagick when you combine PDF files it does not rerender the images (which leads to image quality loss).

I do you ImageMagick and Ghostscript for every other PDF and image file processing.


Post by pbender » 2007-01-04T15:14:07-07:00

I never thought to use imagemagick for this.

I use Ghostscript directly to merge postscript and PDF files.

The command line I typically use (on a linux box) is of the form:

Code: Select all

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Merged.pdf -dBATCH 2.pdf 3.pdf
You can mix and match postscript and pdf files for the inputs.



Post by tome100 » 2007-01-04T15:54:23-07:00

I've tried the command line, pdftk, and imagemagick. The problem is that for some reason the user "apache" returns an error: sh: gs: command not found

However when I run it from the command line (as root), it works fine.

User avatar
Site Admin
Posts: 11095
Joined: 2003-05-31T11:32:55-07:00

Post by magick » 2007-01-04T16:00:45-07:00

You must either ensure that gs is in the apache execution path and has proper permissions or edit delegates.xml and ensure gs has an absolute path such as /usr/local/bin/gs.

User avatar
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony » 2007-01-04T17:45:29-07:00

You could also try to set the 'PATH' environment yourself in the PHP. Though this is better done by the sys-admins in the the web server start up script.

This is also the best way for doing IM commandline calls from PHP.

However many Sys-admins will not do this as they don't know enough about web apps, and may consider such a request a security hole. Note that it is, as it is, as the apps can make the calls anyway! It just locks the web apps to specific machine setups. Arrrgghhh...
Anthony Thyssen -- Webmaster for ImageMagick Example Pages


Post by nileshp » 2007-01-05T11:59:19-07:00

I had some what of same issue.

my problem was that server has more then one version of imagemagick installed..

so when doing convert from ssh vs command line (Web) it calling up two differnt version.

to narrow down the issue i did was exec('convert -version') on the web to track down that SSH version is newest vs teh exec that ran a older version.

so if you analize the output of the exec version and compare it to ssh version #?

so i had to use a full path when calling it from exec() function..

solution for example..

Post Reply