Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


Living Linux

Outputting Text

04/04/2000

As we discussed last week, the next few columns in this series will describe some of the available tools and applications for doing word processor-like things with Linux.

GNU Enscript is a tool that takes plain text as input and writes PostScript output, doing any number of formatting changes in between. It's great for quickly making nice output of a plain text file -- you can use it to do things like output text in a font of your choosing, or paginate text with graphical headers at the top of each page.

By default, enscript paginates its input, outputs it in a 10-point Courier font, and puts a simple header at the top of each page containing the filename, date and time, and page number in bold. Use the -B option to omit this header.

If you have a PostScript printer connected to your system as the default printer, enscript spools its output right to the printer. You can verify this on your system by looking at the enscript configuration file, /etc/enscript.cfg. The line DefaultOutputMethod: printer specifies that output is spooled directly to the printer; stdout instead of printer sends to the standard output instead.

Most Linux installations nowadays have print filters set up so that PostScript spooled for printing is automatically converted to a format the printer understands (if your system doesn't have this set up for some reason, convert the PostScript to a format recognized by your printer with the gs tool).

For example, to convert the file report.txt to PostScript with default formatting, and spool the output right to the printer, you'd type:

$ enscript report.txt RET

You can write the output to a file instead with the -p option, which takes the name of the file to output as an argument. This is useful for previewing the output before you print it -- view it on the screen with a PostScript viewer application like ghostview.

To write the file report.txt to a PostScript file report.ps, and then preview it, you'd type:

$ enscript -p report.ps report.txt RET
$ ghostview report.ps RET

Printing text in a font

To output text in a particular font, give the name of the font to use as a quoted argument to the -f option.

Specify both the font family and size in points: give the capitalized name of the font family (with hyphens to indicate spaces between words) followed by the the size in points. For example, Courier14 outputs text in the Courier font at 14 points, and Times-Roman12.2 outputs text in the Times Roman font at 12.2 points. A list of available font names are in the file /usr/share/enscript/afm/font.map; the enscript man page describes how to use additional fonts that might be installed on your system.

To print the contents of the text file report.txt in the Helvetica font at 12 points, type:

$ enscript -B -f "Helvetica12" report.txt RET

When headers are used, they're normally output in 10-point Courier Bold; to specify a different font for the text in the header, give its name as an argument to the -F option.

To print the contents of the text file report.txt with text set in 10-point Times Roman and header text set in 18-point Times Bold, type:

$ enscript -f "Times-Roman10" -F "Times-Bold18" report.txt RET

 

Printing a poster or sign

Enscript is useful for making a quick-and-dirty sign from a text file. For example, suppose the text file "sign" contains the following text:

CAUTION

WET PAINT!

You can print a sign out of it by specifying a large font, such as Helvetica Bold at 72 points, and omitting the default headers:

$ enscript -B -f "Helvetica-Bold72" sign RET

Getting fancy with headers

You can customize the header text by quoting the text you want to use as an argument to the -b option. Use the special symbol "$%" to specify the current page. For example:

$ enscript -b "Page $% of the annual report" report.txt RET

Use the -G option to output text with graphic headers, with the header text set in blocks of various shades of grey. You can create your own custom fancy headers, too -- the way to do this is described in the CUSTOMIZATION section of the enscript man page.

Outputting in landscape mode

To output text in landscape mode -- where the entire text is rotated 90 degrees counterclockwise -- use the -r option.

To print the contents of the text file report.txt in landscape mode and with text set in 28-point Times Roman, type:

$ enscript -r -f "Times-Roman28" report.txt RET

Selecting the pages to output

To specify which pages are output, give the range of number(s) as arguments to the -a option. To print just the odd or even pages, use the special odd and even arguments. This is good for printing double-sided pages: first print the odd, and then feed the output pages back into the printer and print the even.

For example, to print the odd pages of the text file report.txt, type:

$ enscript -a odd report.txt RET

Then, to print the even, type:

$ enscript -a even report.txt RET

Language highlighting

Enscript recognizes the formatting of more than 40 languages and formats, from the Perl and C programming languages to HTML, and can highlight portions of the text based on its syntax; there's also one for email and news articles, called "mail", that highlights the message header lines. In Unix-speak, this is called "pretty-printing."

Give the name of the language or format with the -E option (be careful not to put a space between the E and the language name). For example, to pretty-print the HTML file index.html, type:

$ enscript -Ehtml index.html RET

To pretty-print an email saved to the file "important-mail", type:

$ enscript -Email important-mail RET

 

 

The chart below describes some of the more popular enscript options.

Common Options for GNU Enscript

-[number]

Specify number of columns per page; e.g. for four columns/page, use "-4".

-#[number]

Specify number of copies; e.g. to print three copies, use "-#3".

-a[pages]

Specify the page numbers to be printed. Give specific pages by their number, page ranges between hyphens (for pages 34-45 inclusive, use "-a34-45"); the special "odd" prints odd pages and "even" prints even pages.

-d[printer]

Spool output to the printer named "printer".

-E[language]

"Pretty-print" the text written in a given language with context highlighting; to list the supported languages, run "enscript --help-pretty-print".

-H[number]

Specify height of highlight bars, in lines (without [number], the value of 2 is used).

-i[number]

Indent lines by [number] characters, or follow [number] with a letter denoting the unit to use:

  c   centimeters
  i   inches
  p   PostScript points

-I[filter]

Pass input files through [filter]---can be a tool or quoted command.

-j

Print borders around columns.

-L[numbers]

Specify number of lines per page.

-u[text]

Specify a quoted string "underlay" to print underneath every page.

-U[number]

Specify number of logical pages to print on each page of output.

--highlight-bar-gray=[number]

Specify the level of gray color used in printing the highlight bars.

--margins=[left]:[right]:[top]:[bottom]

Adjust left, right, top and bottom page margins; numbers are in PostScript points, and when specifying, any can be omitted.

--rotate-even-pages

Rotates each even-numbered page 180 degrees.

Next week: how to process and use the TeX and LaTeX files you might encounter out there.

arrowMore Living Linux articles.


Michael Stutz spent the 1990s making open-source recordings of music and writing fiction and other text (including the open source etext novel Sunclipse). He also wrote a comprehensive, general-purpose copyleft license that anyone could use on any published data.

Michael Stutz was one of the first reporters to cover Linux and the free software movement in the mainstream press.


Read more Living Linux columns.

Discuss this article in the O'Reilly Network Linux Forum.

Return to the Linux DevCenter.

 

Copyright © 2009 O'Reilly Media, Inc.