Category Archives: Ghostscript

Save single page from PDF file as PNG image file

In the open source world, the best choice for PDF command line foo (and PDF foo in general) is almost always ghostscript. This is a quick way to extract a single page from a PDF file and save it as PNG file with a given (resolution in dpi):

#!/bin/bash
INFILE="$1"
OUTFILE="$2"
PAGE="$3"
RES="$4"
 
gs -dBATCH -dNOPAUSE -sDEVICE=png16m \
    -r$RES \
    -dFirstPage=$PAGE \
    -dLastPage=$PAGE \
    "-sOutputFile=$OUTFILE" \
    "$INFILE"

Example usage:

./pdf_page_to_png.sh input.pdf output_p3.png 3 200

Convert all PDF files in a directory to PNG images

I just needed to convert several PDF vector graphics to PNG graphics. A clean way to do this is via Ghostscript (example: gs -sDEVICE=png16m -sOutputFile=tiger.png tiger.pdf). For convenience, I made a Python script that converts PDF files to PNG files via Ghostscript. I use it under Linux and Windows. Continue reading