I\'m trying to convert a PDF to a PNG image (at least the cover of one). I\'m successfully extracting the first page of the PDF with pdftk. I\'m using imagemagick to do the
My solution is much simpler and more direct. At least it works that way on my PC (with the following specs):
me@home: my.folder$ uname -a
Linux home 3.2.0-54-generic-pae #82-Ubuntu SMP Tue Sep 10 20:29:22 UTC 2013 i686 i686 i386 GNU/Linux
with
me@home: my.folder$ convert --version
Version: ImageMagick 6.6.9-7 2012-08-17 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP
So, here's what I run on my file.pdf
:
me@home: my.folder$ convert -density 300 -quality 100 file.pdf file.png
For a PDF that ImageMagick was giving inaccurate colors I found that GraphicsMagick did a better job:
$ gm convert -quality 100 -thumbnail x300 -flatten journal.pdf\[0\] cover.jpg
Couldn't get the accepted answer to work. Then found out that actually the solution is much simpler anyway as Ghostscript not just natively supports PNG but even multiple different "encodings":
png256
png16
pnggray
pngmono
The shell command that works for me is:
gs -dNOPAUSE -q -sDEVICE=pnggray -r500 -dBATCH -dFirstPage=2 -dLastPage=2 -sOutputFile=test.png test.pdf
It will save page 2 of test.pdf to test.png using the pnggray
encoding and 500 DPI.
Here is a german discussion about a problem like this for SVG files where it is solved by using
convert -background transparent
Perhaps this works for you, too.
One can also use the command line utilities included in poppler-utils
package:
sudo apt-get install poppler-utils
pdftoppm --help
pdftocairo --help
Example:
pdftocairo -png mypage.pdf mypage.png
To convert pdf to image files use following commands:
For PNG gs -sDEVICE=png16m -dTextAlphaBits=4 -r300 -o a.png a.pdf
For JPG gs -sDEVICE=jpeg -dTextAlphaBits=4 -r300 -o a.jpg a.pdf
If you have multiple pages add to name %03d gs -o a%03d.jpg a.pdf
What each option means: