# PostScript FAQ/Using PostScript
## Using PostScript
### Do I need a PostScript printer?

There are several situations when PostScript printer is desirable:
* you need to print documents with placed EPS files
* you receive print jobs from 3rd parties and need a portable page description language
* you expect to print variable content documents
* desktop printer serves as a cheap proofer for a more expensive printer or imagesetter
* vector page description is much shorter than the raster image of the page
* you run Unix-like system where PostScript language serves as a printer API.
Software RIP with a non-PostScript printer is becoming a good alternative to a PostScript printer. Unlike built-in RIP the software and hardware can be easily upgraded. The host OS also provides a better debugging environment.

### How to create a PS file?

PostScript is a low level page description language. The PostScript graphic model is based on presentation elements: lines, curves, characters, images. There is no notion of words, paragraphs, justification, footnotes, etc. Most authors prefer to work on the higher level and generate presentation formats (PostScript, PDF, PCL, etc.) from the high level page description.
TeX is a typesetting program designed for high-quality composition of material that contains a lot of mathematical and technical expressions. It has been adopted by many authors and publishers who generate technical books and papers. It was created by Professor Donald Knuth of Stanford University, originally for preparation of his book series The Art of Computer Programming.
groff (GNU troff) software is a typesetting package which reads plain text mixed with formatting commands and produces formatted output.
PostScript printer drivers on Mac OS and Microsoft Windows can generate PostScript file from the incoming GDI calls. So every application that prints can also generate PostScript. However the set of graphic primitives in the file will be limited by the GDI graphic model. Most graphic editors and DTP applications bypass GDI and generate PostScript directly. This helps to generate better PostScript and provides more flexible graphic model to the application.
Sometimes it is necessarily to create or modify PS files by hand. PostScript files can be big and contain binary data. The text editor should not expand tabs or normalize line ending. emacs is a good choice because it is extensible and includes a PostScript major mode.

### How to send a PS file to a printer?

On a Unix-like system you can use lpr:
On Microsoft Windows you can send the file to a printer as:
or
Some of the printers also support lpr protocol.
PrintFile is a freeware GUI Windows program that copies files the system print queue. It can also pick pages from the DSC-conforming PostScript file.
On Classic Mac OS, you can drop the file on the printer icon.

### What is the best way to view a PS file on the screen?

PostScript file is designed for sequential printing - images are often stored as inline streams, page size and resolution is a part of page description.
The viewer is expected to browse the document in random order and magnification. This can be achieved by converting the PostScript to an internal form that enables direct access to page elements and fast clipping of invisible objects. None of the PostScript viewers work this way.
GSview is a free graphical interface for Ghostscript developed by Russel Lang at Ghostgum Software. The page selection is implemented through DSC processing; zoom is implemented by re-interpretation of the page. GSview is available for Win32, Win32s, OS/2 and GNU/Linux. Older versions are available for Win16. There are other graphical X-Window front ends for Ghostscript - examples are mgv, Moonshiner and Okular.
PostScript can be easily converted to PDF using Ghostscript, ps2pdf online, Distiller, PDFCreator and a few of other programs. There are many more PDF viewers than PS viewers. Furthermore PDF viewers often are more user friendly than most PS viewers, the most convenient way to view a PS file is to convert it first into a PDF file. The most popular free PDF viewers are Adobe Reader, Evince, Foxit Reader, MuPDF, Okular, PDF-XChange Viewer, Sumatra PDF and Xpdf.

### How to send a binary PostScript file to an ASCII-only printer?

Many network printers don't accept PS files containing binary data.
Sometimes it is impossible to re-create the file as 7-bit ASCII but any file is easy to convert to ASCII stream and execute it through the corresponding decoding filter. The following script converts standard input to hexadecimal stream and adds the decoding code. All DSC comments will be hidden but most printers ignore them anyway. The script was tested on bash and GNU/Linux systems.
The version of the script for MS-DOS and Windows has to read '>' character from itself because echo ">" prints ">" (with the quotes).
od (octal dump) comes from the GNU Textutils package. It is available on all stock Unix-like systems (including macOS) and can be installed on Windows with Cygwin or MinGW packages.
PostScript utilities can be easily programmed using the PostScript language. The following program reads the input file or standard input, compresses it, optionally converts it using ASCII85 encoding, and writes to output file or standard output. The program is run by Ghostscript using:
Where:
    -q suppresses writing of copyright information to stdout
    -dASCII selects ASCII85 encoding
    -dLEVEL3 selects Flate encoding introduced in level 3
    encode.ps is the program
    INFILE is input file, defaults to stdin
    OUTFILE is output file, defaults to stdout
If needed, DSC processing can be done before downloading the file to the printer. Development of a DSC-aware ASCII85-based converter is left for the exercise of the reader.

### How to print an EPS file?

Encapsulated PostScript, or EPS, files are usually meant for inclusion in printable PostScript or other EPS files.
Unlike the printable PostScript they can have:
* showpage operator not included
* image shifted outside of the printable area
* image size too big or too small
* PC-style header and preview
Thus, sending EPS files to a PostScript printer directly may cause a PostScript error or only page to be printed.
The standard way is to place EPS in the page in a graphic design application and print.
In order to directly print an EPS file, you need to add a PS header that does all the necessary adjustments.
A suitable header is given as an example on the net.anastigmatix.Import resource page.

### How to check whether a PostScript file is valid ?

Send the file to a PostScript interpreter. If you like the results the file is valid. PostScript interpreters include:
* Any PostScript printer
* Ghostscript
* Acrobat Distiller
* Display PostScript
There are others of course, but these are the simplest/cheapest to get hold of.
In fact this will check correctness of the PostScript program only on the interpreter where it has been tested. Different interpreters provide very different execution environment that can make PS program take a different branch.
PostScript program can produce unexpected output without reporting any PostScript errors. In the mission-critical situation a proof page printed on the laser printer and signed by the designer is mandatory.

### How to count pages in a PS file?

Interpret the file using bbox device and count HiResBoundingBox lines.
Somewhat faster is also the command
Assuming the document is DSC-conforming one can count %%Page comments directly in the document.

### How to determine which pages are color and which are monochrome?

First, you need to decide what makes the page a color page. Probably, monochrome page has zero density of CMY components. To check this using Ghostscript you need to select a true color CMYK device, install correct CRD and rasterize the file.
The page is a color page if any pixel has non-zero components in CMY planes.

### How to print only some pages from a PS file?

DSC (Document Structure Convention) was intended to provide an easy way to parse the PostScript document, rearrange pages, add missing fonts, substitute images, etc. Although most programs generate DSC comments few programs use them. It appears that correctness of DSC comments has been never tested even by major vendors.
Sometimes the document claiming DSC conformance fails with PostScript errors after modification. Even worse the pages can be spoiled in some subtle way. For instance, some characters of the incrementally downloadable font may be silently replaced with a default character.
Page selection based on DSC comments is used by PrintFile and GSview.
PDF can serve as a convenient intermediate format esp. when page extraction is performed more than once. You can use Ghostscript to convert the file to PDF and extract a range of pages as following:
Occasionally, other graphic formats can work better than PDF. The author once wrote bilist.cpp to place two A5 pages on a single A4 page and rearrange pages into signatures for some old PCL printer.

### How to rearrange pages of a PS file for printing a brochure?

There are several ways to solve this problem. Every approach has its advantages.
Direct manipulation preserves most of PS structure and takes least processor time. Unfortunately, it is only applicable for DSC-conforming PostScript files. Although most DSC files generated by mainstream applications claim DSC conformance few have been tested as such. For instance, Microsoft PS driver generates non-conforming files by default.
psselect from psutils can select and reorder pages in the DSC-conforming PS file.
Conversion to raster image is a simple and reliable approach. For instance bilist.cpp extracts pages from the PCL file, reorders pages into signatures, and places them 2-up. Ghostscript can be used for PostScript to PCL conversion.
Conversion to PDF greatly simplifies further document processing. Pages in PDF file can be extracted and printed in any size or order. The PostScript file can be converted to PDF using several applications. For instance, Ghostscript
To extract a range of pages from a PDF file into another PDF file use
To extract a range of pages from a PDF file and convert to PS file use:
It is always safe to concatenate PDF files as following
The methods above are sufficient to rearrange pages in any order but complex jobs can be done more efficiently. The PDF file can be convertrd to a simple DSC-conforming PS file.
The output file will be very simple, because all the complexity is hidden inside Ghostscript libraries. The format is trivial to modify using any DSC utility, text editor, or script.
Updated file can be converted back to PDF.
The DSC file uses GS libraries, which is fine if the DSC file is fed to Ghostscript only. To generate a portable PS file convert it back to PS.

### Why does my PS driver generate color drawings but grayscale images?

When the driver generates PostScript stream for monochrome printer it often converts color images to grayscale for better transmission time, faster rendering, and lower resource usage. Conversion of vector objects to grayscale doesn't save much space or time. So the driver leaves it to the PostScript interpreter.
This problem often happens when the job is printed to Generic PostScript printer driver. Generic driver is not the best one. It is limited to the common subset of PostScript language available on most printers. It is monochrome, has small choice of media, fonts, no special features.The PostScript file from Generic driver will print on wide variety of printers but many features will be missing.
Although it appears like every printer uses a special driver it is the same driver configured for different printers. The driver takes configuration parameters from PPD file. When possible the native PPD file should be used. The Acrobat Distiller PPD is a good choice to generate a color PostScript file free of vendor-specific features.

### How to recombine plates in preseparated PostScript file?

It is very simple to rasterize the pages and recombine the pages into the composite file. Rasterization can be done at 8 bit per plane or 1 bit per plane depending whether any color correction is desired for the composite image.
Merging the planes on vector level is much more difficult. The only known tool for this job is the Seps2Comp Acrobat plug-in from Creo (now discontinued and unsupported). The PostScript file can be converted to PDF using a number of programs including Ghostscript and Acrobat Distiller.

### How to remove black frames around n-up pages?

Windows driver generates black frames around virtual pages when it does n-up printing. The frames are drawn in nupshowpage procedure. To remove the frames this procedure must be modified. On level 3 interpreters Hack-MSDriver-n-up IdiomSet can identify and replace this procedure when the job file is loaded by the interpreter. No modification of the job file is needed. To activate the replacement you need to copy the idiom file to /Resource/IdiomSet/ directory on the printer or host-based interpreter. The PostScript root directory on a host-based interpreter likely to be different from the system root. For one-time replacement you can also prepend the IdiomSet file to the job file.
For level 1 and 2 interpreters one has to remove framenup { ... } if fragment from the job file itself using any text editor.

### How to print A4 on US Letter or vice versa?

To be written.
The document can be scaled or just shifted.
Fix pagedevice, policy 3,

### How to extract figures from PostScript files?

Figures in PostScript document are often included as EPS files and can be easily extracted with a text editor. In the DSC-conforming document the EPS file is framed by using a text editor.
Don't forget that EPS files can be nested.
EPS files extracted from the PostScript file don't have a preview, which can be re-generated with Ghostscript.

### How to change color PostScript to black-and-white?*

If you can run Perl, look for bw_convert, a tiny perl script that will change every RGB color declaration in a PostScript file to a black or grey value. Simply enough, it replaces "setrgbcolor" with "pop pop pop 0 setgray". The script can be found at bw_convert. The script author is not documented on that site. However, this script is really an excellent example of how not to do things; the author has not really grasped that PostScript is a programming language, and that simple lexical changes will often fail, while adding an extra line of code at the start would be more effective. The script also does not touch images, CMYK data, or level 2 color operators.
A similar thing can be done by adding the following line at the beginning of a PostScript file:
It will overwrite setrgbcolor with setgray and calculate a grayscale color. Still this will not affect images and might not change everything that you wanted to change. There also could be other problems so no warranty on that.

### How to tweak RGB to CMYK conversion but leave other objects intact?

Level 3 PostScript has a powerful method for tweaking correction of device-dependent colors to device-independent colors: /UseCIEColor page device key and DefaultGray, DefaultRGB, DefaultCMYK color spaces.
When UseCIEColor flag is on, grayscale, RGB, and CMYK colors are interpreted as the colors in DefaultGray, DefaultRGB, and DefaultCMYK color spaces. It is little known that an array form of device color spaces ( e.g. [DeviceCMYK]) can be defined as a Default* resource effectively disabling the corresponding conversion.
Conversion of CMYK to device-independent colors is often undesirable because of the loss of black generation information.

### How to get date and time?

Built-in access to the date and time is with currentdevparams and setdevparams on the %Calendar% device, which appeared in the PostScript Language Reference Manual version 2012 supplement for PostScript LanguageLevel 2. The following code will write the date and time to standard output:
The standard does not provide for timezone information and the clock is generally set for local time. Its range extends to 2079. If the interpreter is running on a host operating system, the OS is likely to determine whether setdevparams can actually set the clock.
A host-based PostScript interpreter can access the file system, including named pipes and devices, so possibilities are endless. This example was tested on GNU+Linux using the specific interpreter Ghostscript.
Type the following on one terminal:
Run gs -dNODISPLAY on another terminal in the same directory, then type
Now, typing t at the Ghostscript prompt shows the current time, using the host OS date command instead of the %Calendar% device.
PostScript has been used to implement an HTTP server.