Published: 23. 3. 2012   Category: GNU/Linux

pdf2png – CLI utility for conversion

This simple BASH script uses GhostScript and ImageMagick convert for conversion of PDF file to one or several PNG images. It is possible to select the number of page or an interval, also output resolution and a number of colors.

For example, try to convert this graph, in common way:

convert -geometry 640x data1_amat.pdf data1_amat.png

The convert will use -geometry to set the horizontal size, here 640 px, the vertical size is given by the aspect ratio of image. There is the result:

Now, try to do the same with the pdf2png:

pdf2png -x 640 data1_amat.pdf

The output is stored in file data1_amat_0001.png:

The image size is 75 kB, with ImageMagick it is 226 kB, and the main difference is in quality, the first image is blurry. The difference is in the way of conversion, my script first produce image in huge resolution and than resize it to specified size and a little bit of unsharp mask filter is used to improve the sharp edges.

Usage

Invoke pdf2png without any parameters or with -h, to display this help:

PDF2PNG
Usage:  pdf2png [OPTIONS] FILE

Options:
        -h   ... this help
        -a   ... convert all pages
        -f N ... from page number N
        -t N ... to page number N
        -p N ... only page N (default N = 1)
        -d N ... resolution in dpi (default N = 300)
        -x N ... bitmap  width in pixel (if y not given, save aspect ratio)
        -y N ... bitmap height in pixel (if x not given, save aspect ratio)
        -u N ... unsharp radius (default N = 1.0)
        -c N ... number of colors (default N = 16777216)

If no additional options is not given the defaults are used, you can change them inside the script, see the variables on the top.

The name of output is same as original PDF, but the suffix with number of page is added.

Download

pdf2png (3770 B)

Put this file in $PATH, for example /usr/local/bin.

Change log