Format Plain Text as PostScript with gslp

Need a plain text file printed with a heading, page numbers and a sane tab width, without opening a word processor? gslp turns it into print-ready Ghostscript output. The examples use Ghostscript 10.02.1, installed here as package version 10.02.1~dfsg1-0ubuntu7.9. Allow about fifteen minutes, plus time to inspect the output on your printer if you send it there.

1. Check the installed command

Confirm the binary and package before relying on version-specific behaviour:

$ command -v gslp
/usr/bin/gslp
$ dpkg-query -W -f='${Package} ${Version}\n' ghostscript
ghostscript 10.02.1~dfsg1-0ubuntu7.9

The manual page is dated for Ghostscript 10.02.1. The command does not implement a conventional --help option, so an attempted help request reports an unknown switch and then prints usage. Use man gslp for the complete installed option list:

$ man gslp

Checkpoint: if command -v finds nothing, install the distribution's Ghostscript package through your normal package-management process. Do not copy a random binary into /usr/bin.

2. Generate a file without printing it

Start with a harmless local conversion. The -p option takes an output filename attached directly to the option, and the input filename follows it:

$ printf 'alpha\tbeta\nsecond line\n' > /tmp/gslp-sample.txt
$ gslp -p/tmp/gslp-sample.ps /tmp/gslp-sample.txt
Printing /tmp/gslp-sample.txt
Page height = 49.

The exact page-height message varies with the input and settings. The useful result is an output file that identifies as PostScript:

$ file /tmp/gslp-sample.ps
/tmp/gslp-sample.ps: PostScript document text
$ head -n 1 /tmp/gslp-sample.ps
%! 

If you see /undefinedfilename, check the spelling and placement of both paths. In particular, use -p/path/to/output, not -p /path/to/output. Keep the input and output paths different so a formatting mistake cannot overwrite the source text.

Checkpoint: this step only created files under /tmp. When you no longer need them, remove those two known files with rm -- /tmp/gslp-sample.txt /tmp/gslp-sample.ps. That deletion is irreversible, so verify the names before running it.

3. Select a layout and heading

The default output uses the epson device at 180 dpi when you invoke gslp, and the current date is placed in the centre header by default. These options make a compact two-column report with a wider tab stop and an explicit heading:

$ gslp -T8 --columns 2 --heading-left 'Inventory report' \
    -p/tmp/inventory.ps /path/to/inventory.txt

-T8 sets the tab width to eight columns. --columns 2 prints two columns. The heading options accept left, centre or right fields, and the string %# is replaced by the page number. If you need to replace the default date header rather than add fields to it, use -B first to clear the header fields:

$ gslp -B --heading-left 'Inventory report, page %#' \
    -p/tmp/inventory.ps /path/to/inventory.txt

Keep the text inside the heading quoted. An unquoted heading containing spaces becomes several shell arguments and can be interpreted as input filenames.

4. Limit pages or adjust spacing

For a long file, format only a known page range. This changes the generated output, not the source file:

$ gslp --first-page 3 --last-page 5 \
    -p/tmp/pages-3-to-5.ps /path/to/report.txt

Use --spacing 2 for double spacing or --spacing 3 for triple spacing. Margins are expressed in inches, and character or space width adjustments use units of 1/72 inch:

$ gslp --spacing 2 --margin-left 0.75 --margin-right 0.75 \
    -p/tmp/readable-report.ps /path/to/report.txt

Do not guess at a page range after changing margins, font or spacing. Recheck the generated file, because those changes alter pagination. --add-to-width applies an adjustment to each character, while --add-to-space applies one to each space; both may be negative.

5. Choose a printer alias deliberately

The aliases select different Ghostscript devices and resolutions. They are names for the same formatter, not interchangeable spellings:

CommandDeviceResolutionIntended output
gslpepson180 dpiEpson-compatible output
gsbjbj10e180 dpiBubbleJet output
gsdjdeskjet300 dpiDeskJet output
gsdj500djet500300 dpiDeskJet 500 BubbleJet output
gsljlaserjet300 dpiLaserJet output

For example, format for a LaserJet-compatible device into a file for your existing print pipeline:

$ gslj -p/tmp/report.lj /path/to/report.txt
$ file /tmp/report.lj
/tmp/report.lj: Hewlett-Packard PCL printer data

The file description can differ between systems. The key check is that the command exits successfully and the output is non-empty. Do not send a file to a physical printer until you have checked the destination and the selected alias: a wrong device can produce unreadable output, waste paper or start a queued job.

6. Diagnose the usual failures

Done means