Convert PDF to PNG

I am trying to convert a PDF to a PNG image (at least the cover of one). I am successfully extracting the first page of PDF using pdftk. I use imagemagick to convert:

convert cover.pdf cover.png 

This works, but unfortunately cover.png is not working correctly (some of the alpha objects in the PDF do not display properly). I know that ImageMagick uses GhostScript for conversion, and if I do it directly with gs, I can get the desired results, but I would prefer to use the conversion library as it has other tools that I would like to use.

This command in GhostScript executes the required image:

 gs -sDEVICE=pngalpha -sOutputFile=cover.png -r144 cover.pdf 

I am wondering if there is a way to pass arguments through conversion to GhostScript, or am I stuck in calling GhostScript directly?

+71
image pdf imagemagick png ghostscript
Mar 17 '09 at 8:18
source share
11 answers

You can use one command line with two commands ( gs , convert ) connected via the channel, if the first command can write its output to stdout and the second can read its input from stdin.

  • Fortunately, gs can write to stdout ( ... -o %stdout ... ).
  • Fortunately, convert can read from stdin ( convert -background transparent - output.png ).

Problem resolved:

  • GS is used to process the alpha channel with a special image,
  • convert used to create a transparent background,
  • to avoid writing a temporary file to disk.

Complete solution:

 gs -sDEVICE=pngalpha \ -o %stdout \ -r144 cover.pdf \ | \ convert \ -background transparent \ - \ cover.png 



Update

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 with the name page-000.png , page-001.png , ... (Note that %d -counting is zero - file-000.png corresponds to page 1 of the PDF, 001 - 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 
+62
Jul 31 '10 at 20:14
source share

Of all the available alternatives, I found Inkscape to get the most accurate results when converting PDF files 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." 
+23
Mar 18 '13 at 18:59
source share

To convert PDF files to image files, use the 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 the name % 03d gs -oa%03d.jpg a.pdf

What each parameter means:

  • sDEVICE = {jpeg, pngalpha, png16m ...} - filetype
  • -o - output file (% stdout to stdout)
  • -dTextAlphaBits = 4 - font smoothing.
  • -r300 - 300 dpi
+12
Nov 04 '15 at 17:52
source share

You can also use the command line utilities included in the poppler-utils package:

 sudo apt-get install poppler-utils pdftoppm --help pdftocairo --help 

Example:

 pdftocairo -png mypage.pdf mypage.png 
+8
Sep 23 '17 at 13:24
source share

Failed to get accepted response to work. Then it turned out that in fact the solution is much simpler, since Ghostscript not only supports PNG, but even several 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 test.pdf for test.png using pnggray encoding and 500 DPI.

+3
Mar 17 '15 at 19:50
source share

Here is a discussion in Germany about a problem similar to this for SVG files, where it is resolved with

 convert -background transparent 

Perhaps this also works for you.

+2
Mar 17 '09 at 8:34
source share

I will add my decision, I even thought that his thread was out of date. Maybe this will help someone anyway.

Firstly, I need to generate a PDF file. I use XeLaTeX for this:

 xelatex test.tex 

ImageMagick and GraphicMagic now 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 a transparent background, clipped by what is actually on the page. The -density and -resize give better granularity and increase overall resolution.

I suggest checking if you can reduce the density for you. This will reduce conversion time.

+2
Jul 11 2018-12-12T00:
source share

For the PDF file that ImageMagick gave inaccurate colors, I found that GraphicsMagick did a better job:

 $ gm convert -quality 100 -thumbnail x300 -flatten journal.pdf\[0\] cover.jpg 
+2
Sep 02 '15 at 7:00
source share

My solution is much simpler and more direct. At least this works on my PC (with the following specifications):

 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 

from

 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 is what I run on my file.pdf :

 me@home: my.folder$ convert -density 300 -quality 100 file.pdf file.png 
+1
Nov 16 '13 at 13:41
source share

Since alternative tools are also listed on this page, I will mention xpdf, which has prebuilt command line tools for Linux / Windows / Mac. Supports transparency. It is free for commercial use - unlike Ghostscript, which has really outrageous prices.

In the test on a huge PDF, it was 7.5% faster than Ghostscript.

(It also has PDF to text and HTML converters)

0
Jun 28 '19 at 16:09 on
source share

You can use ImageMagick without sharing the first page of the PDF with other tools. Just do

 convert cover.pdf[0] cover.png 


However, if PDF is CMYK, PNG does not support this. It will need to be converted to sRGB, especially if it has transparency, since Ghostscript cannot handle CMYK with alpha.

 convert -colorspace sRGB cover.pdf[0] cover.png 
0
Jun 28 '19 at 16:16
source share



All Articles