A Guide To Linux Printing

By: Steve Singer
Monday, May 31, 1999 07:39:10 PM EST
URL: http://www.linuxplanet.com/linuxplanet/tutorials/210/1/

Introduction

Introduction.

Are you wondering how the Linux printing system works? Or maybe you just think of the process as magic. The software that handles printing under Linux is extremely flexible and this article explains how it works.

----------------------------------------------------------
Please see some of our other Linux printing articles:
More Than HPLIP Service for Linux
Sharing Linux Printers Across Subnets
Make Your Own Holiday Cards with GIMP
Printing Your Custom GIMP Holiday Cards
TurboPrint for Linux Saves the Day-- Again
----------------------------------------------------------

Overview

A file printed in Linux will pass through several programs. The printing request will typically originate from an application that will invoke the printing client software. The file will pass from the printing client, to the printing spooler and then through a series of filters and being finally sent to your printer. Linux's printing system was designed to make it easy to add support for network printers and a variety of formatting options. Normally all of the steps are transparent to the end user. However, a good understanding of the process helps when debugging printing problems.

The Application

Linux applications can have a variety of different levels of printing support. The most basic is support to text. Many text based applications including Pine can only print plain ASCII text. Almost any printer available can print ASCII text however plain text is boring and unattractive. To get around this problem most X-Windows based applications can also print graphics. The most common type of printer supported in the Linux world is Postscript. Postscript was developed by Adobe in the 1980's as a portable printer language. Unfortunately only expensive high end printers tend to support Postscript. Postscript's portability is still achieved in the Linux world using an emulator program named Ghostscript. Ghostscript converts Postscript files to the printer specific formats of most popular(and many unpopular) printers on the market. More about Ghostscript will be discussed in the Filters section. Lastly, some applications available for Linux have built in support for many different types of printers. These applications can usually either print in Postscript(emulated by Ghostscript) or in your printer's native format.

The Print Client

Files are normally submitted to the Linux printing system using the program lpr. Lpr is called by most applications when data needs to be sent to the printer. Invoking lpr by hand is a matter of calling the lpr program from the command line. Both users and applications can specify options for the print job by passing command line arguments to lpr. The most common option to set is the printer name. When you create a printer in Linux a name is normally assigned to it. Multiple printer configurations each with a different name can connect to the same physical printer. The two configurations might use different filter scripts or a different Ghostscript driver. Specifying, the printer name on the lpr command line is done with the -P option. To print the file test.ps to the printer named laser1, you would use the command "lpr -Plaser1 test.ps". Additional command line options for lpr are documented in lpr's man page. Lpr's task is to send the file to the print spooler. Network printers have the file sent by lpr to the remote print server. When printing to a printer connected to the local machine lpr will pass the file to the local print spooler.

The Print Spooler (lpd)

Lpd is the Linux print spooler daemon. Normally lpd starts when your machine boots and continues to run waiting to accept a printing request. Lpd can accepts jobs from either the local print client or other computers on the network. Lpd is responsible for holding print jobs until the printer is ready to accept them. Printer configuration information is stored in the file /etc/printcap. The name of the printer, the physical device that the printer is attached to, what filter to use and the settings for other more advanced options are stored in printcap. The file format of printcap is cryptic but many tools exist such as Red Hat's "printtool" that will create the file for you based on options selected from the GUI front end. Detailed information about the format of the printcap file can be found in the Linux Printing HOWTO. Once the printer becomes ready to accept a print job the spooler looks in the print queue for that printer and selects the first file waiting to be printed.

The Filter

Often the raw output of an application is not suitable to be printed. The print spooler can pass the file through a user defined filter which will convert the data to a format that is easier to print. The most common uses of a filter is to convert Postscript input to output that is specific to your printer. A program named Ghostscript is usually used to do the actual conversion. A filter is a shell script that executes a series of commands sending the output back to the print spooler. Several pieces of software are available that contain pre-built filters. Most Linux distributions come with a GUI front end that configures printers. The front end will automatically build an appropriate filter for your printer based on the options you select. In Red Hat Linux the program "printtool" does this task. Users who want more advanced printer filters including the ability to print JPEG or GIF files directly from the command line should look at Magiccap.. Magiccap is a comprehensive filter package that uses Ghostscript and netpbm(image conversion software) to print a variety of graphic formats.

The Printer

Once your file has been processed by the filter it is ready to be printed. Parallel ports on a PC are typically called LPT1, and LPT2. "Parallel Printer Support" must be compiled into the Linux kernel to use the printer ports. Printer ports like other character devices in Linux correspond to files in the /dev directory. On a typical Linux system the file /dev/lp1 corresponds to LPT1. Data written to /dev/lp1 is sent to your printer. Often people will try copying a text file to /dev/lp1 to ensure that Linux talks with the printer correctly. For example the command "cat /etc/hosts >/dev/lp1" should result in your printer printing the hosts file. Do not worry if the output is not formatted correctly, filters are often used to correct this. If your printer did not receive any data a problem exists. Sometimes the kernel will log an error message in the systems log (usually /var/log/messages) that might hint at the problem.

Managing Printers

Once your printers have been setup Linux provides a few tools that can be used to manage them. Viewing a list of files currently in the print queue can be done with the lpq command. Running lpq with no options will return information about all files in the print queue. Deleting a file from the queue can be done with the command lprm followed by the job number of the print job you wish to delete. Job numbers can be obtained from the output of lpq. A tool named lpc is also used to manage printers. Lpc can be used by the system administrator to enable or disable a printer, change the order of jobs in the print queue, and perform a few other administrative functions. After you have invoked lpc from the command line you will be prompted to enter a command, the help command provides information about the commands supported by lpc. Additional information on lpc can be found in the man page, or by consulting the Linux Printing HOWTO.

Copyright Jupitermedia Corp. All Rights Reserved.