Thursday, October 7, 2010

php Converting PDF-s to pictures Imagemagick GhostScript

SkyHi @ Thursday, October 07, 2010
Ever wondered how to render PDF-s into individual pictures page by page? It is really simple.

Warning to all of those who are allergic to command line: The following method does imply use (albeit very simple) of CLI.

Here we will be using a tool called ImageMagick. This is a widely available tool with unparalleled versatility in a variety of areas, but is by no means the only one: there are plenty of other tools to do the same thing. The following is only one method of many.


1) So first of all you will need to install ImageMagick. In Ubuntu you would do:

sudo apt-get install imagemagick


2) Then open up a terminal, and browse to the location of the PDF you want to convert. Say, if your PDF was in /home/username/PDFs, you would do the following:

cd /home/username/PDFs


3) Let’s say that you have a PDF file called test.pdf and you want it to convert to PNG files. You also want these PNG files’ filename to begin page. Then you would run:

convert test.pdf page.png

This will give you as many PNG files named page-01.png, page-02.png, … as there are pages in your original PDF.


If you wanted JPEG files instead just run:

convert test.pdf page.jpg


You might also want to control the resolution of the resulting image files. To do that you must specify the -density option. It is hard to predict what number you should give, so try to experiment to get the desired result. If you wanted a density of, say, 800, you would run:

convert -density 800 test.pdf page.png


Want to select certain pages only? No problem, to convert only the first page – run:

convert "test.pdf[0]" page.png


You may also specify a range. So to convert the first three pages run:

convert "test.pdf[0-2]" page.png


Several ranges are also possible:

convert "test.pdf[0-2,4-6]" page.png

(Note that the output files will be consecutively named in the latter example, so you will get files page-0.png, -1, -2, -3, -4 and -5.png. NOT as you would expect page-0.png, -1, -2, -4, -5 and -6)


Edit: If you are inclined to use another conversion utility, try pdftoppm from the package poppler-utils. Works very well, too…




Or

gs -sDEVICE=jpeg -sOutputFile=test.jpg test.pdf 
 
 
 
REFERENCES
  
http://blog.dr-ivan.com/2009/06/28/tips-tricks-converting-pdf-s-to-pictures-in-linux
http://answers.google.com/answers/threadview?id=134543
http://stackoverflow.com/questions/467793/how-do-i-convert-a-pdf-document-to-a-preview-image-in-php