Wednesday, January 22, 2014

Print text files with multiples pages per sheet

a2ps is the venerable tool for formatting text files for Postscript printers. This post focuses on how to call a2ps to print multiple pages per sheet.

To install a2ps and gv aka ghostview, which a2ps uses to preview output files:

$ sudo apt-get install a2ps
$ sudo apt-get install gv


You can layout a sheet of paper into rows and columns of pages.

To print the emacs config file in the above format, any of the following will do:

$ a2ps ~/.emacs
$ a2ps --rows 1 --columns 2 ~/.emacs
$ a2ps -2 ~/.emacs

Note that with no explicit instruction, the default layout is 1x2 in the landscape orientation.

a2ps provides shortcuts to specify the number of rows and columns for common configurations. For example, -2 is equivalent to 1 row and 2 columns. Valid shortcuts are -1 to -9.

Paper size

North American a2ps users need to modify the output paper size. a2ps was written in Europe, and uses A4 as the default paper size. North America uses a different standard, and a common paper size is called Letter - 8.5 x 11 inches. Printing A4 on Letter-sized paper results in text being cropped at the end of each line in the right column.

To modify the paper size to Letter:

$ a2ps -M Letter .emacs

Instead of overriding the paper size in every single run, you can change the default locally (per user) or globally (system-wide). To specify the paper size for a user, add the following line to $HOME/.a2ps/a2psrc.

Options: --medium=Letter

To change the default system-wide, edit the file /etc/a2ps-site.cfg. Look for the Options: --medium line and change the value to Letter.

Preview of output

By default, a2ps sends the output to the printer. Sometimes you want to override that behavior in order to preview the output. You may redirect the output to a PostScript file or directly to ghostview.

To create a PostScript output file, and open it using ghostview:

$ a2ps -o -M Letter .emacs
[.emacs (plain): 31 pages on 16 sheets]
[Total: 31 pages on 16 sheets] saved into the file `'
[101 lines wrapped]
$ gv

a2ps provides a shortcut to preview its output directly in ghostview.

$ a2ps -P display -M Letter .emacs

The -P parameter normally specifies the printer name. However, display is a special name to redirect output to ghostview.

Multiple files

a2ps also supports multiple input files. By default, each file begins printing on a new sheet ("file alignment"). Empty cells in the layout are not filled. For example, given 2 input files - 1.txt and 2.txt - that are each 1 page long, each file will be printed on a separate sheet, leaving the sheets half empty.

You can control the file alignment using -A parameter. If file alignment is fill, a2ps prints a file beginning in the next available cell, leaving no empty cell in between it and the previous file.

$ a2ps -A fill  1.txt 2.txt
[1.txt (plain): 1 page on 1 sheet]
[2.txt (plain): 1 page on 1 sheet]
request id is ML-1640-Series-162 (0 file(s))
[Total: 2 pages on 1 sheet] sent to the default printer

a2ps has too many useful features to be covered in a single post. Read the man page if you want to print double-sided, or you want to number each line in the output.

1 comment:

Jesse said...

This program is a great find for me. I had been using lpr to print text files from the command line, but the font was too big which made the hardcopy look terrible. a2ps makes the document look really nice.