Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 780
Pooshonk Avatar asked Jun 07 '13 13:06

Pooshonk


People also ask

How to convert multiple pages of PDF to a single image?

Not only are you able to convert PDF to Word, PPT, Excel, and other file formats, but you can also convert multiple-page PDF to a single image. The "Batch Process" feature will help you to import multiple PDF files and convert PDF files to the file formats you needed.

How do I convert a PDF file to an image?

There is a quick and convenient way to convert PDF to one or more images. Command line tool ImageMagick does that (and a lot more). You can convert an entire PDF document to a single image, or, if you like, there is an option to output pages as a series of enumerated image files.

How to convert page 2 of a PDF file to png?

If you include a C-style integer format string, for example if you add %03d to the end of your output file name, you will get output_file_name-001.png, output_file_name-002.png, output_file_name-003.png, etc. This will actually convert page 2 of PDF to PNG, since numbering starts with 0.

How do I open a multi page PDF file?

Open multi-page PDF files This PDFelement Pro provides you an easy way to import multi-page PDF files to the program. You can click on the "Open" button on the left corner and select the desired PDF file for the program. Or you can direct drag and drop your PDF files into the program.


1 Answers

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/

like image 142
salsbury Avatar answered Sep 20 '22 18:09

salsbury