PostScript (PS)
| Full name | PostScript |
|---|---|
| Extensions | .ps |
| MIME type | application/postscript |
| Type | page description language (a program that draws pages), usually plain text |
| Starts with | 25 21 50 53 (%!PS), usually %!PS-Adobe-3.0 |
| Specification | PostScript Language Reference, Third Edition (PDF) |
| Maintained by | Adobe (no longer developed) |
| License | open specification; the name is an Adobe trademark |
A PostScript file is a program in the PostScript language. A printer or an interpreter such as Ghostscript runs the program, and the program draws text, vector graphics and images onto pages. Pages are described in points (1/72 inch) and print at the full resolution of the device.
PostScript is still used as a printer language and as the output of older Unix and TeX tools (dvips, groff, a2ps). For documents you want to view or share, PDF has replaced it: a PDF has the same imaging model but is a data structure, not a program, so a viewer can open any page directly.
When to use PostScript#
- Use when a printer, RIP or print workflow needs PostScript.
- Use when an old toolchain produces it (
dvips,groff -Tps,a2ps,enscript); convert to PDF afterwards. - Avoid for documents to share or archive: use PDF. Most current systems can’t open
.pswithout installing Ghostscript. - Avoid opening files from untrusted sources without a sandbox: a PostScript file is a program and interpreters have had many security bugs.
- Avoid for a single graphic to place in another document: use EPS, SVG or PDF.
File structure#
A PostScript file is plain text (sometimes with binary image or font data) made of PostScript operators in postfix order: operands first, then the operator. Only the interpreter knows what the file draws. To let other programs process a file without running it, well-formed files follow the Document Structuring Conventions (DSC): special comments that start with %% and mark the header, the prolog and each page.
%!PS-Adobe-3.0
%%Creator: my program
%%Title: hello.ps
%%Pages: 1
%%BoundingBox: 0 0 612 792
%%DocumentMedia: Letter 612 792 0 () ()
%%LanguageLevel: 2
%%EndComments
%%BeginProlog
/F { findfont exch scalefont setfont } bind def
%%EndProlog
%%BeginSetup
%%EndSetup
%%Page: 1 1
24 /Helvetica F
72 720 moveto (Hello, PostScript) show
newpath 72 600 moveto 300 0 rlineto stroke
showpage
%%Trailer
%%EOF
The coordinate origin is the bottom-left corner of the page and the y axis goes up. 72 720 moveto is 1 inch from the left edge, 10 inches from the bottom. showpage outputs the current page.
DSC comments#
| Comment | Meaning |
|---|---|
%!PS-Adobe-3.0 |
first line: the file follows DSC version 3.0. %!PS alone means no DSC promise |
%%Pages: n |
number of pages; (atend) means the value is in the trailer |
%%BoundingBox: llx lly urx ury |
area that contains all marks, in integer points |
%%HiResBoundingBox: |
the same with decimals |
%%DocumentMedia:, %%PageMedia: |
paper size and type |
%%LanguageLevel: n |
PostScript level needed: 1, 2 or 3 |
%%Creator:, %%Title:, %%CreationDate:, %%For: |
metadata |
%%EndComments |
end of the header |
%%BeginProlog / %%EndProlog |
procedure definitions used by all pages |
%%BeginSetup / %%EndSetup |
device setup: paper size, duplex, resolution |
%%Page: label ordinal |
start of a page |
%%BeginFont, %%BeginResource |
embedded fonts and other resources |
%%Trailer, %%EOF |
end of the document |
With DSC a tool can count pages, extract a page range or reorder pages (psselect, psnup from psutils) by copying text between %%Page: comments. A file that breaks the conventions (for example, a page that depends on state set by an earlier page) prints fine but breaks such tools.
Language levels#
| Level | Adds |
|---|---|
| 1 | the base language: paths, Type 1 and Type 3 fonts, grayscale images |
| 2 | color spaces (CMYK, CIE), compression filters (LZW, DCT/JPEG, RunLength), forms, patterns, setpagedevice |
| 3 | Flate (zlib) compression, smooth shading, more color spaces (DeviceN), masked images, CID fonts |
Binary data such as images is usually written as ASCII85 or hex so the file stays 7-bit text.
Other things at the start of a file#
- Files printed to a file from Windows printer drivers often start with PJL commands (
1B 25 2D 31 32 33 34 35 58,ESC%-12345X@PJL ...) before%!PS. - An EPS file can start with a 30-byte binary header
C5 D0 D3 C6followed by the PostScript and a TIFF or WMF preview. See EPS. %!PS-Adobe-3.0 EPSF-3.0marks a single-page Encapsulated PostScript file.
Converting PostScript#
Ghostscript does almost every conversion:
# PostScript to PDF
ps2pdf in.ps out.pdf
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf in.ps
# PostScript to PNG, one file per page, 150 dpi, anti-aliased
gs -dNOPAUSE -dBATCH -sDEVICE=png16m -r150 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 \
-sOutputFile=page-%03d.png in.ps
# extract text
gs -dNOPAUSE -dBATCH -sDEVICE=txtwrite -sOutputFile=out.txt in.ps
# PDF to PostScript
pdf2ps in.pdf out.ps
Since Ghostscript 9.50 -dSAFER is the default: the PostScript program can’t read or write arbitrary files. Keep it on for files you didn’t create.
Specifications and documentation#
- PostScript Language Reference, Third Edition (Adobe, PDF): the language, operators and imaging model
- Document Structuring Conventions Specification 3.0 (Adobe Technical Note 5001, PDF)
- Ghostscript documentation: command-line options, output devices, the C API
- Ghostscript output devices and vector devices (pdfwrite, ps2write, eps2write)
- PostScript on Wikipedia
- Document Structuring Conventions on Wikipedia
Software that opens PostScript files#
| Software | Windows | macOS | Linux | Web | Can | Notes |
|---|---|---|---|---|---|---|
| SumatraPDF | ✓ | view | free, open source; opens .ps when Ghostscript is installed | |||
| Ghostscript | ✓ | ✓ | ✓ | view, convert | the reference interpreter; gs, ps2pdf; AGPL or commercial license | |
| Okular | ✓ | view | free, open source KDE viewer; uses Ghostscript through libspectre | |||
| Evince | ✓ | view | GNOME document viewer, uses libspectre | |||
| Zathura | ✓ | view | keyboard-driven viewer with the zathura-ps plugin | |||
| Inkscape | ✓ | ✓ | ✓ | view, edit, convert | imports PS/EPS through Ghostscript, exports PS and EPS | |
| GIMP | ✓ | ✓ | ✓ | view, convert | rasterizes PostScript pages through Ghostscript | |
| Adobe Acrobat Pro | ✓ | ✓ | convert | commercial; Acrobat Distiller converts PS to PDF | ||
| Adobe Illustrator | ✓ | ✓ | view, edit | commercial | ||
| CorelDRAW | ✓ | ✓ | view, edit | commercial; imports PS and EPS | ||
| ImageMagick | ✓ | ✓ | ✓ | convert | command line; reads PostScript through Ghostscript | |
| IrfanView | ✓ | view, convert | free for non-commercial use; needs Ghostscript and the PostScript plugin |
macOS Preview opened PostScript up to macOS 12. macOS 13 removed it from Preview and macOS 14 removed the built-in PostScript-to-PDF converter (pstopdf). On a current Mac, install Ghostscript (brew install ghostscript) and run ps2pdf.
Libraries#
Open-source libraries that interpret, render or write PostScript. See all PostScript libraries for a comparison.
| C/C++ | cairo, Ghostscript, libspectre |
|---|---|
| C# | Ghostscript.NET, Magick.NET |
| Java | xmlgraphics-commons |
| Go | canvas |
| Python | Pillow, pycairo, python-ghostscript |
| Ruby | MiniMagick |
| Rust | cairo-rs |
| PHP | Imagick |
Related formats#
- PDF: the successor, with the same imaging model in a structure that doesn’t need to be executed
- EPS: a single-page PostScript file with a bounding box, for placing into other documents
- DVI: TeX output, usually converted to PostScript with
dvipsor to PDF - SVG: the vector graphics format for the web
- XPS: Microsoft’s fixed-layout format, used in the Windows print path