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 conversion:
convert cover.pdf cover.png
This works, but unfortunately the cover.png comes through incorrectly rendered (some of the alpha object in the PDF aren't rendered properly). I know ImageMagick uses GhostScript to do the conversion and if I do it directly with gs I can get the desired results, but I'd rather use the convert library as it has other tools I'd like to leverage.
This command in GhostScript accomplishes the desired image:
gs -sDEVICE=pngalpha -sOutputFile=cover.png -r144 cover.pdf
I'm wondering is there any way to pass arguments through convert to GhostScript or am I stuck with calling GhostScript directly?
This question is related to
image
pdf
png
imagemagick
ghostscript
As this page also lists alternative tools I'll mention xpdf which has command line tools ready compiled for Linux/Windows/Mac. Supports transparency. Is free for commercial use - opposed to Ghostscript which has truly outrageous pricing.
In a test on a huge PDF file it was 7.5% faster than Ghostscript.
(It also has PDF to text and HTML converters)
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.
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
Try to extract a single page.
$page = 4
gs -sDEVICE=pngalpha -dFirstPage="$page" -dLastPage="$page" -o thumb.png -r144 input.pdf
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
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
You can use ImageMagick without separating the first page of the PDF with other tools. Just do
convert -density 288 cover.pdf[0] -resize 25% cover.png
Here I increase the nominal density by 400% (72*4=288) and then resize by 1/4 (25%). This gives a much better quality for the resulting png.
However, if the PDF is CMYK, PNG does not support that. It would need to be converted to sRGB, especially if it has transparency, since Ghostscript cannot handle CMYK with alpha.
convert -density 288 -colorspace sRGB -resize 25% cover.pdf[0] cover.png
I'll add my solution, even thought his thread is old. Maybe this will help someone anyway.
First, I need to generate the PDF. I use XeLaTeX for that:
xelatex test.tex
Now, ImageMagick and GraphicMagic both parse parameters from left to right, so the leftmost parameter, will be executed first. I ended up using this sequence for optimal processing:
gm convert -trim -transparent white -background transparent -density 1200x1200 -resize 25% test.pdf test.png
It gives nice graphics on transparent background, trimmed to what is actually on the page. The -density
and -resize
parameters, give a better granularity, and increase overall resolution.
I suggest checking if the density can be decreased for you. It'll cut down converting time.
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:
Out of all the available alternatives I found Inkscape to produce the most accurate results when converting PDFs to PNG. Especially when the source file had transparent layers, Inkscape succeeded where Imagemagick and other tools failed.
This is the command I use:
inkscape "$pdf" -z --export-dpi=600 --export-area-drawing --export-png="$pngfile"
And here it is implemented in a script:
#!/bin/bash
while [ $# -gt 0 ]; do
pdf=$1
echo "Converting "$pdf" ..."
pngfile=`echo "$pdf" | sed 's/\.\w*$/.png/'`
inkscape "$pdf" -z --export-dpi=600 --export-area-drawing --export-png="$pngfile"
echo "Converted to "$pngfile""
shift
done
echo "All jobs done. Exiting."
Source: Stackoverflow.com