Converting PS and PDF files with pstoedit

The Chameleon


pstoedit lets you convert Postscript and PDF files to vector-based image formats.

By Hagen Höpfner

www.sxc.hu

Postscript and PDF are excellent formats for viewing and printing documents, but if you ever try to edit a PDF or Postscript file, you'll soon discover that your options are limited. Experts may argue that Postscript code is editable as it is, but if you ever try to edit a Postscript file directly, you'll find that the process is anything but convenient. pstoedit is a command-line tool that offers a simple solution to the problem of editing Postscript and PDF.

Installation

Suse 9.3 has the 3.33 pstoedit binary, but if you want to install the current 3.42 version, you need to build the tool from source code. As the new version introduces many changes, it is a good idea to opt for the build. You can pick up the source code from [3]. Besides gcc, you will need a number of additional programs to handle all the output formats.

The LibEMF [4] library is required to convert from Postscript to EMF. Users with Suse 9.3 will also need the libEMF-devel package; you can use either Yast or an RPM search engine like Pbone [5] to locate the library. Users with Debian Sarge can build LibEMF themselves. If you have problems building the library, change to the /libemf subdirectory with the libemf.cpp and libemf.h files. Give the command patch < libEMF-1.0.fixes-2002-09-30.diff to install the patch from the LibEMF download page at [6].

If necessary, you can teach pstoedit how to use Graphicsmagick 1.1.6 [7] - an extension of the popular Imagemagick package - and Ming [8] (see the "Installing Ming and Graphicmagicks" box). The former allows pstoedit to convert from Postscript to any image format supported by Graphicsmagick. And Ming helps you generate SWF-formatted Flash images from Postscript files - although this does require the CVS version of the Ming library.

Before you launch into the pstoedit install, first download the sources from [4]. After unpacking the archive, edit the configure file, changing the following line:

SPECIAL_CXXFLAGS="-DBUGGYGPP -pedantic -Wall -Wwrite-strings-Wcast-qual -Wpointer-arith "

to:

SPECIAL_CXXFLAGS= "-DBUGGYGPP -Wall -Wwrite-strings -Wcast-qual -Wpointer-arith "

If the pedantic option is enabled, the compiler will refuse to compile pstoedit with the Graphicsmagick add-on. Follow this up with a variant on the well-known three-card trick of ./configure --with-libemf-include=/usr/include/libEMF && make && su -c "make install". Debian users need to check to ensure the libEMF folder really is located below /usr/include. If pstoedit fails to find the libraries when launched, enter ldconfig in rootC> mode to update the links. pstoedit itself should be located below /usr/local/bin on your hard disk.

Installing Ming and Graphicsmagick

Suse users can start by launching Yast to install cvs, and then logging in to the Ming CVS by entering

cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/ming login

Press [Enter] when prompted for a password. If this is the first time you have used cvs, repeat this step after the error message. Then enter the following:

cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/ming co ming

This places a subdirectory with the latest Ming files in your current working directory. To build Ming on Suse 9.3, you need gcc and gcc-c++, giflib-devel, libpng-devel, bison,libjpeg-devel, and flex. For a trouble free build, Debian users need the giflib-bin, libungif4-dev, giflib3g, bison, flex, swig, and libpng2-dev packages. Enter ./configure && make && make install to build and install.

Make sure you have version 1.1.6 of Graphicsmagick: Debian users additionally need libperl-dev from the Etch repository for the build. Now repeat the same three steps you used to compile and install Ming. Don't forget to install plotutils when you are finished; this allows you to use its output formats [9].

Catch the Tiger!

The tiger shown in Figure 1 is a familiar sight in articles that discuss Postscript. Find this file in the /usr/share/ghostscript/8.15/examples/ directory. Suse 9.3 does not have a genuine Postscript file with the tiger, but you will find an EPS-image instead - users with other distros may need to select a different sample file, but the instructions are similar.

Figure 1: We'll use this Postscript tiger to experiment with some pstoedit features.

pstoedit, which is based on Ghostscript, can handle PS, EPS, and even PDF as input formats. The number of available output formats depends on the modules available to pstoedit. Basically, the program uses the following syntax:

pstoedit INPUTFILE OUTPUTFILE

For example, pstoedit tiger.eps tiger.emf on Suse 9.3 creates an EMF version of the tiger. Once you convert the file to EMF, you can import the file into OpenOffice and edit it with Draw.

Under normal circumstances, pstoedit evaluates the output filename to decide which module to use. Entering pstoedit tiger.eps tiger.fig is doomed to fail, because it gives the converter too many alternatives. Instead of an Xfig-formated tiger, you just get a list of available modules and the formats they can generate. The Xfig-formated tiger shown in Figure 2 is generated by entering

pstoedit -f plot-fig tiger.eps tiger.fig

The -f parameter helps pstoedit select the right module.

Figure 2: Xfig is the classic vector-oriented editor in Unix and Linux. pstoedit lets you convert PS, EPS,, or PDF files to FIG.

pstoedit is not just a converter. Several output parameters let you modify the output image while converting. pstoedit --help gives you a comprehensive list of options. The -xscale .5 option gives you an image half the width of the original, and -yscale 2 an image with twice the original height. Postscript files often comprise multiple pages. The -split parameter splits a multi-page file. Some modules also have options: if you use the fig module to create Xfig images, rather than the plot-fig module, the -metric parameter changes the units to centimeters from the default of inches.

Interestingly, pstoedit lets you create programs from Postscript files. pstoedit tiger.eps tiger.tk creates the Tk applet shown in Figure 3 from the tiger image, and you can run sh tiger.tk to launch the applet. If you prefer to avoid scripting languages, you can use the java1 and java2 modules, which let you create Java code. As an alternative, specify tiger.swf as the output file for a Flash image.

Figure 3: The tiger as a Tk program. pstoedit not only converts Postscript into other image formats, it also gives you Tk applets.

Unfortunately, support for the Graphicsmagick add-on is not error-free at this time of writing. For example, the module interprets the tiger as a file with two pages. To talk the module into generating a pixel image (in JPG format, for example), stipulate pstoedit -f magick tiger.eps tiger%d.jpg. You can simply close the window that pops up at this point. This results in two files, tiger1.jpg and tiger2.jpg, with the big cat in the first file and nothing in the second.

Conclusions

pstoedit is a powerful tool for converting files from Postscript. Kudos to Wolfgang Glunz, who not only developed pstoedit, but also provided much-needed installation support.

INFO
[1] pstoedit homepage: http://www.pstoedit.net/
[2] Postscript facts: http://en.wikipedia.org/wiki/Postscript
[3] pstoedit download: http://prdownloads.sourceforge.net/pstoedit/pstoedit-3.42.tar.gz?download
[4] LibEMF homepage: http://libemf.sourceforge.net/
[5] RPM search engine: http://rpm.pbone.net
[6] LibEMF patch: http://sourceforge.net/tracker/download.php?group_id=31968&atid=403913&file_id=32104&aid=616742
[7] Graphicsmagick homepage: http://www.graphicsmagick.org/
[8] Ming homepage: http://ming.sourceforge.net/
[9] Plotutils homepage: http://www.gnu.org/software/plotutils/
[10] EPS facts: http://en.wikipedia.org/wiki/Encapsulated_Postscript