PostScript and Encapsulated PostScript


One of the persistent mysteries of document handling is the definition of Encapsulated PostScript (EPS).

I write a lot of raw PostScript. Most of the figures in these Web pages, as well as in my publications, were just plain PS, written by hand. But I need to “encapsulate” them to include them in PDF files, or for use by most image-handling utilities.

If you read the Second edition of the Adobe Red Book (not the 3rd Edition that's available on the Web), there's a whole Appendix devoted to the Document Structuring Conventions. These are now available from Adobe as a 109-page separate document. But it isn't clear how much of this stuff actually has to be included in the preamble of a PS file to make it EPS. I don't want to have to include a lot of unnecessary %% comments, if they aren't really needed.

There's also the question of thumbnails. (These are little miniature images of the pages, often used by graphics programs as previews.) They're added by programs like ps2epsi. But EPSI stands for Encapsulated PostScript Interchange format. Do we really need them? If the original PS file is a small vector-graphics drawing, the resulting EPSI file contains a low-resolution bitmapped image of it, making the result much bulkier.

Encapsulated PostScript

Apparently, the absolute minimum required to get some other program to buy a PS file as EPS is
1. make sure the first line says


You might want to change the DSC version number from 2.1 to 3.0, depending on what the file is to be used for. If you set the number too high, you run the risk of having some other program expect more than the bare minimum. If you set it too low, you might run the risk of having some modern EPS-using program barf on your file. (Before version 2.1, some comments were required ; let's avoid that.)

Actually, this isn't always necessary. Most of the time, all you need is the %%BoundingBox: comment. On the other hand, there might be programs that expect to see

%!PS-Adobe-3.0 EPSF-3.0

although those probably also want the thumbnails.

2. make sure the file contains a proper %%BoundingBox: comment.

You can do this easily by using the

gs -dBATCH -dNOPAUSE -sDEVICE=bbox -q

command, which sends both exact and nearest-integer bounding boxes to standard error.

Strictly speaking, there should be an


comment after the BB comment; but this usually isn't needed.

That gs command line is a nuisance to type. And you'd like to be able to get the %%BoundingBox: comment into your PS file automatically. Although Debian didn't used to provide a tool to do this, there is currently a package named ps2eps that installs a Perl script (of that same name) to do this.

Before that was available, I made my own little shell script (below).

A ps2eps script

So here's a shell script to do the absolute minimum:

# ps2eps	encapsulates *.ps files

# Usage:	ps2eps	# generates file.eps

# see if it's already done:
if grep -q '^%%BoundingBox:' $1
	echo "$1 appears to be encapsulated already."

# Create output filename:
if echo $1 | grep -q '.ps$'
	filename=`basename $1 .ps`


# Use gs to determine BoundingBox:
BB=`gs -dBATCH -dNOPAUSE -sDEVICE=bbox -q $1 2>&1 | grep '%%BoundingBox'`

# edit it into the PS file with sed:
sed '1a\
'"$BB"” < $1 > $filename

Put it into a directory in your PATH, make it executable, and you're in business.


There is a shell script called ps2epsi, whose man page makes it sound like the right choice to do this already. It isn't. What it does is to generate an EPS file that contains a “thumbnail” — a miniature, rasterized version of the page image. That's sometimes wanted by Service Bureaus that convert EPS files to high-quality film or print images; but it isn't what's needed here. Ignore it.


Copyright © 2005, 2006, 2009, 2010, 2014 Andrew T. Young

Back to the . . .
graphics-format conversion page

or the website overview page