PostScript
**********

Debian: gs
Debian: psutils
WWW: ftp://www.gnu.org/pub/gnu/ghostscript/
WWW: ftp://ftp.dcs.ed.ac.uk/pub/ajcd/
WWW: http://www.cappella.demon.co.uk/tinyfiles/tinymenu.html

PostScript is a programming language, used to describe the way a ``page'' (usually a physical sheet of paper) should look. PostScript files are text files containing the PostScript commands for drawing images to be printed on pages.

Like plain text files, PostScript files are commonly found on the Internet (and are used by commercial printers) because, as with plain text, they can be shared across platforms and hardware without difficulty. The same PostScript file can be output on a high-end display or printed on a low-end printer, to the best of that hardware's capability.  PostScript is a compact and elegant format.

While it's possible to write directly in the PostScript language, and some people have become adept at programming PostScript, so many tools and applications convert files to and from PostScript that you don't have to. See Converting Plain Text for Output, for a way to convert plain text into PostScript.

Ghostscript is a free implementation of the PostScript language. The gs tool is a Ghostscript interpreter that is used to convert files from PostScript to other formats, usually for printing to a non-PostScript printer (see Preparing a PostScript File for Printing). The ghostview tool is used to preview PostScript files on the screen (see Previewing a PostScript File).

EPS, or Encapsulated PostScript, is a file format that describes the contents of a box within a page. EPS files can be embedded in the page of a PostScript file, and are therefore commonly used when inserting an illustration into a document (for example, all of the illustrations in the Cookbook are EPS format files). You can view and print EPS files just as you would PostScript files.

This chapter includes recipes for formatting and manipulating PostScript files. Recipes are separated according to whether they work on the individual, logical pages in a PostScript file (the numbered pages in the file that are not necessarily the physical pages of output), and those that work on the entire file as a whole.

Unless otherwise indicated, the tools in this chapter are part of Angus Duggan's PSUtils package. These tools can be useful for other purposes than those described below; see their respective man pages for more details.

Manipulating PostScript Pages
=============================

These recipes work on individual pages of PostScript files, and not the entire file itself.

Extracting DVI Pages to PostScript
----------------------------------

To extract specific pages of a DVI (``DeVice Independent'') file to PostScript, use dvips and give the page or hyphenated page ranges to output with the -pp option.

  * To extract only the first page from the file abstract.dvi and send the PostScript output to the printer, type:

          $ dvips -pp1 abstract.dvi RET

By default, dvips will output to the printer; to save the PostScript output to a file, give the file name to be used for output with the -o option.

  * To output as PostScript the pages 137 to 146 of the file abstract.dvi to the file abstract.ps, type:

          $ dvips -pp137-146 -o abstract.ps abstract.dvi RET

Extracting Pages from a PostScript File
---------------------------------------

Use psselect to select pages from a PostScript file; when you give an input file, it outputs a new PostScript file containing the specified pages.

Give the pages to select as arguments to the -p option; you can list single pages and ranges of pages separated by commas. Give ranges as two numbers between a hyphen; thus, 4-6 specifies pages four through six, inclusive. If you omit the first number in a pair, the first page is assumed, and if you omit the last number, the last page is assumed. Pages are written to the new file in the order they are specified.

     To select page 47 from the PostScript file newsletter.ps and output it to the file selection.ps, type:

          $ psselect -p47 newsletter.ps selection.ps RET

  * To select the first ten pages, page 104, pages 23 through 28, and page 2 from the file newsletter.ps and write it to the file selection.ps, type:

          $ psselect -p1-10,104,23-28,2 newsletter.ps selection.ps RET

In the preceding example, page 2 is selected twice.

Prefix a number with an underscore (_) to indicate that the given page number is relative to the last page, counting backwards toward the first page.

  * To select the second-to-last through the tenth-to-last pages from the PostScript file newsletter.ps and output them to the file selection.ps, type:

          $ psselect -p_2-_10 newsletter.ps selection.ps RET

  * To select the second-to-last through the tenth pages from the PostScript file newsletter.ps and output them to the file selection.ps, type:

          $ psselect -p_2-10 newsletter.ps selection.ps RET

Use the -e option to select all even-numbered pages, and use the -o option to select all odd-numbered pages.

  * To select all of the even pages in the file newsletter.ps and write them to a new file, even.ps, type:

          $ psselect -e newsletter.ps even.ps RET

  * To select all of the odd pages in the file newsletter.ps and write them to a new file, odd.ps, type:

          $ psselect -o newsletter.ps odd.ps RET

Use an underscore (_) alone to insert a blank page, and use -r to output pages in reverse order.

  * To select the last ten pages of file newsletter.ps, followed by a blank page, followed by the first ten pages, and output them to a new file, selection.ps, type:

          $ psselect -p_1-_10,_,1-10 newsletter.ps selection.ps RET

  * To select the pages 59, 79, and 99 in the file newsletter.ps, and output them in reverse order (with the 99th page first) to a new file, selection.ps, type:

          $ psselect -p59,79,99 -r newsletter.ps selection.ps RET

NOTE: The same result as the preceding example above could have been done by omitting the -r option and just listing the three pages in the reverse order:

     $ psselect -p99,79,59 newsletter.ps selection.ps RET

Combining PostScript Pages
--------------------------

Use psnup to print multiple PostScript pages on a single sheet of paper; give as an option the number of pages to be combined (or put ``up'') on each sheet.

  * To make a new PostScript file, double.ps, putting two pages from the file single.ps on each page, type:

          $ psnup -2 single.ps double.ps RET

To specify the paper size, give the name of a standard paper size as an argument to the -p option: a3, a4, a5, b5, letter, legal, tabloid, statement, executive, folio, quarto, or 10x14. You can also specify any height and width with the -h and -w options; units can be specified in centimeters (followed by cm) or inches (followed by in). If no size is specified, psnup assumes a paper size of a4.

Use the -l option when pages are in landscape orientation (rotated 90 degrees counter-clockwise from portrait orientation), and -r when pages are in seascape orientation (rotated 90 degrees clockwise from portrait orientation).

Pages are placed in ``row-major'' layout in the output file, where logical pages are placed in rows across the page. Use the -c option to specify a ``column-major'' layout, where logical pages are placed in columns down the page. Scale the size of the pages by giving a percentage to multiply the page size by as an argument to the -s option; for example, -s .5 scales pages to 50 percent of their original size.

To draw a border around each page, specify the border's width in points as an argument to the -d option (if no width is specified, a value of 1 is assumed).

Arranging PostScript Pages in Signatures
----------------------------------------

A signature is a group of pages in a document corresponding to sheets of paper folded and bound; these pages are normally not in sequential order in a document (for example, in a document with eight-page signatures, page 8 and page 1 might both be printed on the same sheet of paper).

To rearrange the pages of a PostScript file by signature---usually for printing the file as a book or booklet---use psbook. Give as arguments the name of the PostScript file to read from and the name to use for the output file; it reads the contents of the first, rearranges the pages, and then writes the PostScript output to the second file.

  * To rearrange the pages of file newsletter.ps into a signature and write it to the file newsletter.bound.ps, type:

          $ psbook newsletter.ps newsletter.bound.ps RET

By default, psbook uses one signature for the entire file. If the file doesn't contain a multiple of four pages, it adds blank pages to the end.

To specify the size of the signature to use---in other words, the number of pages that will appear on a single piece of paper---give the number as an argument to the -s option. Signature size is always a multiple of four.

  * To rearrange the pages of file newsletter.ps into an eight-sided signature and write it to newsletter.bound.ps, type:

          $ psbook -s8 newsletter.ps newsletter.bound.ps RET

Manipulating PostScript Documents
=================================

These recipes work on a PostScript document as a whole.

Resizing a PostScript Document
------------------------------

Use psresize to resize a PostScript file. It takes as arguments the file to resize and the output file to write to; you must also specify the page size of the output file, using the same format as with the psnup tool: use -p or -h and -w to specify the size of the output file, and use -P to specify the size of the input file (see Combining PostScript Pages).

  * To resize the PostScript file double.ps to US letter-sized paper, writing output to a new file, doublet.ps, type:

          $ psresize -pletter double.ps doublet.ps RET

Combining PostScript Documents
------------------------------

Use psmerge to concatenate and merge multiple PostScript files into a single file. Give the names of the files to be merged as arguments, and psmerge outputs them to the standard output in the order given. You can also specify an output file name with the -o option (don't put any spaces between the file name and the option).

  * To merge the files slide1.ps, slide2.ps, and slide3.ps into a new PostScript file, slideshow.ps, type:

          $ psmerge -oslideshow.ps slide1.ps slide2.ps slide3.ps RET

NOTE: As of this writing, psmerge only works with PostScript files that were made with the same application---which means, for example, that you can merge multiple files made with TeX, or multiple files made with xfig, but not a combination of the two.

Arranging a PostScript Document in a Booklet
--------------------------------------------

To arrange the pages in a PostScript file to make booklets, rearrange the file in a signature with psbook, use psnup to arrange the pages---two to a printed page in landscape mode---and then use pstops to output first the odd and then the even pages.

The trick to doing this properly is to first determine exactly what you need to do and then calculate the proper measurements for use with pstops.

  * To make a booklet from the file newsletter.ps:

      1. Rearrange the pages into a signature:

               $ psbook newsletter.ps newsletter.signature.ps RET

      2. Put the pages two to a page in landscape orientation, at 70 percent of their original size (typed all on one line):

               $ psnup -l -pletter -2 -s.7 newsletter.signature.ps >
               newsletter.2up.ps RET

      3. Output the odd pages:

               $ pstops "2:0(1in,0in)" newsletter.2up.ps > odd.ps RET

      4. Output the even pages:

               $ pstops "2:-1(1in,0in)" newsletter.2up.ps > even.ps RET

Then, to print the booklet, you send odd.ps to the printer, load the printed pages in the manual feed tray and then send even.ps to the printer. This prints the odd and even pages on opposite sides of the sheets.

  * To make a double-sized booklet on letter-sized paper in landscape orientation, from a file using letter-sized portrait orientation, type:

          $ psbook input.ps > temp1.ps RET
          ...PROCESSING MESSAGES...
          $ psnup -l -pletter -2 -s.7 temp1.ps > temp2.ps RET
          ...PROCESSING MESSAGES...
          $ pstops "2:0(1in,0in)" temp2.ps > odd.ps RET
          ...PROCESSING MESSAGES...
          $ pstops "2:-1(1in,0in)" test2.ps > even.ps RET
          ...PROCESSING MESSAGES...
          $

Converting PostScript
=====================

These recipes show how to convert PostScript files to other formats. See also the recipes for preparing PostScript files for printing, Preparing a PostScript File for Printing.

Converting PostScript to PDF
----------------------------

Use ps2pdf, part of the gs (``Ghostscript'') package, to convert a PostScript file to PDF. Give as arguments the name of the PostScript file to read from, and the name of the PDF file to write to.

  * To make a PDF file sutra.pdf from the input file sutra.ps, type:

          $ ps2pdf sutra.ps sutra.pdf RET

This command writes a new file in PDF format called sutra.pdf. The original file, sutra.ps, is not altered.

NOTE: To make proper PDF conversions, make sure that you have gs version 6.01 or higher installed; use the -v option with gs to output the installed version.

Converting PostScript to Plain Text
-----------------------------------

To convert a PostScript file to plain text, use ps2ascii. Give as arguments the name of the PostScript file to read from, and the name of the text file to write to.

  * To make a text file, sutra.txt, from the input file sutra.ps, type:

          $ ps2ascii sutra.ps sutra.txt RET

This command writes a text file called sutra.txt. The original file, sutra.ps, is not altered.