PostScript and PDF tools under Linux

Introduction

PostScript is a page description language that handles scaling of fonts and vector graphics with ease, and is far superior to pixelated formats (GIF, TIFF etc.) for most printing or publishing jobs.

There are three closely-related formats, often distinguished by characteristic file extensions:

The three formats are distinct, so it is best to understand and respect the differences, and to use the right format for the job. Nevertheless, sometimes transforming between formats is forced on you by circumstances; and anyway, the close family relationship makes transformations feasible. This is the topic of the remainder of this page.

Principal utilities

Central to all PostScript and PDF manipulations is the program GhostScript. This interprets files in any of these formats, and it can then output the document or image after applying various operations: rotation, scaling, cropping, and rasterization. GhostScript is rather user-hostile. Fortunately there are various less hostile wrapper programs, which shield users from GhostScript. The most important one is

Gnome and KDE have their own variants of gv.

Other utilities, specific to .ps and .eps, are:

Utilities specific to .pdf are:

These utilities can solve most problems, and they can be combined creatively to achieve even more elaborate effects.

It is best to check the 'man pages' for the detailed instructions for each of these programs, e.g. man psbook.

It is not guaranteed that your Linux distribution provides all the above utilities; and it may contain utilities not listed above.

Other utilities

To convert a single-page PS file to EPS, use ps2eps from the Prosper package. If you don't have Prosper installed then I can give you a copy of this utility. Usage is explained by

    ps2eps -h

To convert a multipage PS file to multiple single-page PS files, use split-psfile — also part of Prosper. This program is a simple front end to psselect. Usage is

    split-psfile <input.ps> <prefix>
and will result in a set of files <prefix>nnn.ps, with nnn equal to 001, 002, 003...

Converting eps files to pixel format

This job can be done interactively with the aid of the graphics utilities xv/xfig/gimp. But for the most precise control of the output format, or when many transformations are required, then GhostScript can't be avoided. To use gs to transform an eps page to another format, first run

   gs -h
to see what output formats are available. For TIFF, options include: The distinctions are not documented, but are related to colour depth and compression. It might be necessary to try them all.

Then run GhostScript with something like

    gs -q                 # No version message
       -sDEVICE=tiffg3    # Chosen output device
       -sOutputFile=out.tiff
       -dNOPAUSE          # Exit when done
       -dBATCH            # No interaction
       -r72x72            # Default output resolution in pixels/inch
       -g940x880          # Output geometry in pixels 
       image.eps          # Input file: in this case 940x880 points

If resolution is 72x72 and geometry equals the image size in points, then the image will be converted without cropping, with a resolution of 1 pixel/point. To improve resolution of the output image, increase both 'r' and 'g' values by the same factor. (If they are not scaled by the same factor then cropping or blank areas will occur.)

Finding files

Sometimes the biggest problem with images files is simply to find the one you want. If you don't know the name of the figure then visual identification may be the answer. The following script helps by generating an array of thumbnails, with the aid of ImageMagick. (Note: it can't cope with filenames containing spaces. Any suggestions?)

#!/bin/bash
  
# Put this file in some directory in your path ('e.g. ~/bin') or in
# a the root directory of the branch that you want to explore.  Call
# it something like 'thumbs'.  Then run it (this is helped by 
# 'chmod +x thumbs').  Assuming output to 'x:' the thumbnails will be 
# shown in an X11 window: step through the pages by pressing Esc
# or 'q'.  It may take 5 or 10 seconds to generate each page.

# Many customizations are possible: feel free to experiment.
# Prerequisites are ImageMagick and bash.  And linux, of course!

# Define which images you wish to turn into thumbnails
declare -a LIS
#LIS=(`find * -name '*.tiff' -o -name '*.jpeg' -o -iname '*.jpg'`)
LIS=(`find * -name '*.eps'`)
if [ ${#LIS} -eq 0 ]; then echo "No files found"; exit; fi
if [ ${#LIS[@]} -eq 1 ]; then display $LIS; exit; fi

# Layout of thumbnails on each output page
let pageH=842  # A4, points
let pageW=595  # A4, points
let omargin=15 # outer margin; around the set of NxM images
let imargin=5  # image margin; around each individual image
let nRows=5
let nCols=4
let pixH=($pageH-2*$omargin)/$nRows-2*${imargin}
let pixW=($pageW-2*$omargin)/$nCols-2*${imargin}
imageGeom=${pixW}'x'${pixH}'+'${imargin}'+'${imargin}

# Compute the number of A4 pages that will be generated
let nPages=${#LIS[@]}-${#LIS[@]}%$((nRows*nCols))
let nPages=nPages/$((nRows*nCols))
if [ $(( ${#LIS[@]}%(nRows*nCols) )) -gt 0 ]; then let nPages+=1; fi

trap exit 2 15   # enable Ctrl-C; however cursor should be in terminal window
for ((pageN=1;pageN<=nPages;pageN++)); do
  # Compute index of start of subsequence
  let off=nRows*nCols*$((pageN-1))
  
  # Build list of filenames to include in the current page
  names=""
  for ((i=0;i<nRows*nCols;i++)); do
      names=`printf '%s %s ' $names ${LIS[(($off+$i))]}`
  done
  if [ $nPages -gt 1 ]; then echo Page $pageN "/" $nPages; fi
  
  # For more, see file:///usr/share/doc/imagemagick/www/montage.html
  # The final item defines the output destination.  Some possibilities are: 
  #    x:                    a simple window
  #    out_$pageN.ps         a sequence of numbered files
  #    miff:- | display -    ImageMagick's display utility
  #    ps:- | gv -           GhostView
  montage  -set label "%i\n%wx%h" -pointsize 10 $names -tile $nRowsx$nCols -frame 3 -geometry ${imageGeom} -shadow -page A4 -gravity Center  x:

done

Conclusion

The above utilities allow most things to be done, though perhaps in several steps. The missing link is cropping of figures. This can be done interactively in xv/xfig/gimp/display, but doing it programmatically is sometimes desirable: perhaps ImageMagick is the answer?

 


Validate HTML CSS Last changed 2009/01/06 Chris Rennie