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
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.
You can use one commandline with two commands (gs
, convert
) connected through a pipe, if the first command can write its output to stdout, and if the second one can read its input from stdin.
... -o %stdout ...
). convert -background transparent - output.png
).Problem solved:
Complete solution:
gs -sDEVICE=pngalpha \
-o %stdout \
-r144 cover.pdf \
| \
convert \
-background transparent \
- \
cover.png
If you want to have a separate PNG per PDF page, you can use the %d
syntax:
gs -sDEVICE=pngalpha -o file-%03d.png -r144 cover.pdf
This will create PNG files named page-000.png
, page-001.png
, ... (Note that the %d
-counting is zero-based -- file-000.png
corresponds to page 1 of the PDF, 001
to page 2...
Or, if you want to keep your transparent background, for a 100-page PDF, do
for i in {1..100}; do \
\
gs -sDEVICE=pngalpha \
-dFirstPage="${i}" \
-dLastPage="${i}" \
-o %stdout \
-r144 input.pdf \
| \
convert \
-background transparent \
- \
page-${i}.png ; \
\
done
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."
Try to extract a single page.
$page = 4
gs -sDEVICE=pngalpha -dFirstPage="$page" -dLastPage="$page" -o thumb.png -r144 input.pdf
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
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)