Search code examples
phpimagemagickimagemagick-convert

ImageMagick - convert multipage PDF into one image


I am using ImageMagick to convert PDF files into images. However, some of the PDF's have multiple pages, which is proving to be a real problem.

My local convert is below.

exec("\"C:\\Program Files (x86)\\ImageMagick-6.8.5-Q16\\convert.exe\" -density 300 -quality 75 \"{$path}{$filename}{$ext}[$page]\" \"{$targetFile}\"");

If i remove [$page] from the exec it works but creates an image per page, which isn't what I want.

I have been searching for a while now and i've ran out of hope and ideas. Is there any way I can get all of the new images into one final image, or convert the PDF straight into one image? Any help would be greatly appreciated, cheers.


Solution

  • Check out the -append and +append options.

    -append appends the images vertically, and +append appends them horizontally.

    Usage (http://linuxers.org/quick-tips/convert-pdf-file-single-image-using-imagemagick):

    According to that link, the output from a multi-page pdf convert would be ${targetFile}-0.png, ${targetFile}-1.png, ${targetFile}-n.png, etc. Once you have converted the pdf into multiple images, use the -append or +append option:

    convert ${targetFile}-* -append single_image.png

    To put it all together, try something like this (you may have to play with it a bit; I haven't used Imagemagick from the Windows' shell):

    // convert pages of pdf

    exec("\"C:\\Program Files (x86)\\ImageMagick-6.8.5-Q16\\convert.exe\" -density 300 -quality 75 \"{$path}{$filename}{$ext}\" \"{$targetFile}\"");

    // then append them

    exec("\"C:\\Program Files (x86)\\ImageMagick-6.8.5-Q16\\convert.exe\" \"{$targetFile}-*\" -append "${someName}\"");

    More resources:

    http://www.imagemagick.org/script/command-line-options.php#append

    http://www.imagemagick.org/Usage/layers/