LFS101x: Chapter 17 - Printing

Printing

Configuration using CUPS

You need to know how to configure and install a printer in order to manage printers and print directly from a computer or across a networked environment

  • Printing requires software that converts information from the application you're using to a language your printer can understand

  • The Linux standard for printing software is the CUPS (Common UNIX Printing System)

    • CUPS is the underlying software almost all Linux systems use to print from applications like a web browser or LibreOffice

    • It converts page descriptions produced by your application (ex: put a paragraph here, draw a line there, etc.) and then sends the information to the printer (It acts as a print server for both local and network printers)

    • CUPS carries out the printing process with the help of its various components, such as:

    • Configuration files

      • The print scheduler reads server settings from several configuration files

      • The two most important configuration files are

cupsd.conf

  • This file is where most system-wide settings are located

  • It doesn't contain any printer-specific details

  • Most of the settings available in this file relate to network security

    • ex: which systems can access CUPS network capabilities, how printers are advertised on the local network, what management features are offered, etc.

printers.conf

  • This file is where you'll find the printer-specific settings

  • For every printer connected to the system, a corresponding section describes the printer's status and capabilities

  • This file is generated or modified only after adding a printer to the system, and shouldn't be modified by hand

  • You can view the full list of configuration files by typing ls -lF /etc/cups

  • All CUPS-related configuration files are stored under the /etc/cups/ directory

Scheduler

  • CUPS is designed around a print scheduler that manages print jobs, handles administrative commands, allows users to query the printer status, and manages the flow of data through all CUPS components

Job files

  • CUPS stores print requests as files under the /var/spool/cups directory

    • They can actually be accessed before a document is sent to a printer
  • Data files are prefixed with the letter d while control files are prefixed with the letter c

  • After a printer successfully handles a job, data files are automatically removed

    • These data files belong to what's commonly known as the print queue

Log files

  • Log files are placed in /var/log/cups and are used by the scheduler to record activities that have taken place

  • These files include access, error and page records

  • Type sudo ls -l /var/logs/cups to view what log files exist

Filter

  • CUPS uses filters to convert job file formats to printable formats

Printer drivers

  • Printer drivers contain descriptions for currently connected and configured printers and are usually stored under /etc/cups/ppd/

Backend

  • A backend helps to locate devices connected to the system

Small Recap

  • When you execute a print command, the scheduler validates and processes the print job, creating job files according to the settings specified in the configuration files
  • Simultaneously, the scheduler records activities in the log files
  • Job files are processed with the help of the filter, printer driver, and backend, and then sent to the printer

Managing CUPS

Assuming CUPS has been installed you'll need to start and manage the CUPS daemon so that CUPS is ready for configuring a printer

  • Managing the CUPS daemon is simple; All management features can be done with the systemctl utility:

    • $ systemctl status cups

    $ sudo systemctl [enable|disable] cups

    $ sudo systemctl [start|stop|restart] cups

Configuring a Printer from the GUI

Each Linux distribution has a GUI application that lets you add, remove, and configure local or remote printers

  • Using this application, you can easily set up the system to use both local and network printers
  • The following screens show how to find and use the appropriate application in each of the distribution families covered in this course

When configuring a printer, make sure the device is currently turned on and connected to the system

  • If so it should show up in the printer selection menu

  • If the printer is not visible, you may want to troubleshoot using tools that will determine if the printer is connected

    • For common USB printers, for example, the lsusb utility will show a line for the printer

    • Some printer manufacturers also require some extra software to be installed in order to make the printer visible to CUPS, however, due to the standardization these days, this is rarely required

Adding Printers from the CUPS Web Interface

A fact that few people know is that CUPS also comes with its own web server, which makes a configuration interface available via a set of CGI scripts

This web interface allows you to:

  • Add and remove local/remote printers
  • Configure printers (Local/remote printers, Share a printer as a CUPS server)
  • Control print jobs (Monitor jobs, Show completed or pending jobs, Cancel or move jobs)

The CUPS web interface is available on your browser at http://localhost:631

  • Some pages require a username and password to perform certain actions, for example, to add a printer
    • For most Linux distributions, you must use the root password to add, modify, or delete printers or classes

Printing Operations

Printing from the Graphical Interface

Many graphical applications allow users to access printing features using the CTRL-P shortcut.

  • To print a file, you first need to specify the printer (or a file name and location if you are printing to a file instead) you want to use; and then select the page setup, quality, and color options

  • After selecting the required options, you can submit the document for printing. The document is then submitted to CUPS. You can use your browser to access the CUPS web interface at http://localhost:631/ to monitor the status of the printing job

  • Now that you have configured the printer, you can print using either the Graphical or Command Line interfaces

Printing from the Command-Line Interface

CUPS provides two command-line interfaces (descended from the System V and BSD flavors of UNIX)

  • This means that you can use either lp (System V) or lpr (BSD) to print text, PostScript, PDF, and image files

    • These commands are useful in cases where printing operations must be automated (from shell scripts, for instance, which contain multiple commands in one file)

    • lp is just a command line front-end to the lpr utility that passes input to lpr

Using lp

lp and Ipr accept command line options that help you perform all operations that the GUI can accommplish

  • lp is typically used with a file name as an argument

  • lp input examples:

    • lp (To print the file to default printer)

    • lp -d printer (To print to a specific printer, which is useful if multiple printers are available)

    • program | lp echo string | lp (To print the output of a program)

    • lp -n number (To print multiple copies)

    • lpoptions -d printer (To set the default printer)

    • lpoptions can be used to set printer options and defaults (Each printer has a set of tags associated with it, such as the default number of copies and authentication requirements)

    • You can type lpoptions help to obtain a list of supported options

    • lpoptions can also be used to set system-wide values, such as the default printer

    • lpq -a (To show the queue status)

    • lpadmin (To configure printer queues)

Managing Print Jobs

In Linux, command line print job management commands allow you to monitor the job state as well as managing the listing of all printers and checking their status, and canceling or moving print jobs to another printer

  • lpstat -p -d (To get a list of available printers, along with their status)

  • lpstat -a (To check the status of all connected printers, including job numbers)

  • cancel job-ID or lprm job-ID (To cancel a print job)

  • lpmove job-ID newprinter (To move a print job to new printer)

Manipulating Postscript and PDF Files

Working with PostScript and PDF

PostScript is a standard page description language

  • It effectively manages scaling of fonts and vector graphics to provide quality printouts

  • It's purely a text format that contains the data fed to a PostScript interpreter

    • The format itself is a language that was developed by Adobe in the early 1980s to enable the transfer of data to printers

Postscript has been for the most part superseded by the PDF format (Portable Document Format) which produces far smaller files in a compressed format for which support has been integrated into many applications

  • However, one still has to deal with postscript documents, often as an intermediate format on the way to producing final documents

PostScript Features

  • It can be used on any printer that is PostScript-compatible

    • ex: any modern printer
  • Any program that understands the PostScript specification can print to it

  • Information about page appearance, etc. is embedded in the page

Working With Enscript

enscript is a tool that's used to convert a text file to PostScript and other formats

  • It also supports RTF (Rich Text Format) and HTML (HyperText Markup Language)

    • Ex: You can convert a text file to two columns (-2) formatted PostScript using the command enscript -2 -r -p psfile.ps textfile.txt
    • This command will also rotate (-r) the output to print so the width of the paper is greater than the height (aka landscape mode) thereby reducing the number of pages required for printing
  • enscript command line examples

    • enscript -p psfile.ps textfile.txt (Converts a text file to PostScript, which will be save to psfile.ps)

    • enscript -n -p psfile.ps textfile.txt (Converts a text files to n columns where n=1-9, which will be later saved in psfile.ps)

    • enscript textfile.txt (Prints a text file directly to the default printer)

Converting between PostScript and PDF

Most users today are far more accustomed to working with files in PDF format, viewing them easily either on the Internet through their browser or locally on their machine

  • The PostScript format is still important for various technical reasons that the general user will rarely have to deal with

From time to time, you may need to convert files from one format to the other, and there are very simple utilities for accomplishing that task

  • ps2pdf and pdf2ps are part of the ghostscript package installed on or available on all Linux distributions

    • As an alternative, there are pstopdf and pdftops which are usually part of the poppler package, which may need to be added through your package manager
  • Unless you are doing a lot of conversions or need some of the fancier options (which you can read about in the man pages for these utilities), it really does not matter which ones you use.

Another possibility is to use the very powerful convert program, which is part of the ImageMagick package

  • Some newer distributions have replaced this with Graphics Magick, and the command to use is gm convert.

  • Usage examples:

    • pdf2ps file.pdf (Converts file.pdf to file.ps)

    • ps2pdf file.ps (Converts file.ps to file.pdf)

    • pstopdf input.ps output.pdf (Converts input.ps to output.pdf)

    • pdftops iput.pdf output.ps (Converts input.pdf to output.ps)

    • convert input.ps output.pdf (Converts input.ps to output.pdf)

    • convert input.pdf to output.ps (Converts input.pdf to output.ps)

Viewing PDF Content

Linux has many standard programs that can read PDF files, as well as many applications that can easily create them (including all available office suites, usch as LibreOffice)

The most common Linux PDF readers are:

  • evince, which is available on virtually all distributions and is the most widely used program

  • okular, which is based on the older kpdf and is available on any distribution that provides the KDE environment

These open source PDF readers supports and can read file following the PostScript standard

  • The proprietary Adobe Acrobat Reader, which was once widely used on Linux systems, is fortunately no longer available, as it did defective rendering and was unstable and poorly maintained

Manipulating PDFs

At times, you may want to merge, split or rotate PDF files by using operations that include (Not all of these operations can be achieved while using a PDF viewer)

  • Merging/splitting/rotating PDF documents
  • Repairing corrupted PDF pages
  • Pulling single pages from a file
  • Encrypting and decrypting PDF files
  • Adding, updating, and exporting a PDF's emtadata
  • Exporting bookmarks to a text file
  • Filling out PDF forms

In order to accomplush these tasks there are several programs available, such as

  • qpdf

    • Widely available on Linux distributions and is vert full-featured

    • Ex:

    • qpdf --empty --pages 1.pdf 2.pdf -- 12.pdf (Merge the two documents 1.pdf and 2.pdf, and the output will be saved to 12.pdf)

    • qpdf --empty --pages 1.pdf 1-2 -- new.pdf (Write only pages 1 and 2 of 1.pdf, and the output will be save to new.pdf)

    • qpdf --rotate=+90:1 1.pdf 1r.pdf (Rotate page 1 of 1.pdf 90 degrees clockwise and save to 1r-all.pdf)

    • qpdf --rotate=+90:1-z 1.pdf 1r-all.pdf (Rotate all pages of 1.pdf 90 degrees clockwise and save to 1r-all.pdf)

    • qpdf --encrypt mypw mypw 128 -- public.pdf private.pdf (Encrypt with 128 bits public.pdf using as the passwd mypw with output as private.pdf)

    • pdf --decrypt --password=mypw private.pdf file-decrypted.pdf (Decrypt private.pdf with output as file-decrypted.pdf)

  • pdftk

    • Was once very popular but depends on an obsolete unmaintained package (libgcj) and a number of distributions have dropped it; thus we recommend avoiding it

    • Ex:

    • pdftk 1.pdf 2.pdf cat output 12.pdf (Merge the two documents 1.pdf and 2.pdf, and the output will be saved to 12.pdf)

    • pdftk A=1.pdf cat A1-2 output new.pdf (Write only pages 1 and 2 of 1.pdf, and the output will be saved to new.pdf)

    • pdftk A=1.pdf cat A1-endright output new.pdfabc (Rotate all pages of 1.pdf 90 degrees clockwise and save the result in new.pdf)

ghostscript

  • Often invoked using gs

  • Widely available and well-maintained as an interpreter for the PostScript and PDF langauages (The executable program associated with it is abbreviated to gs)

  • This utility can do most of the operations pdftk can, as well as many others (See man gs for details)

  • It's usage is a little complex by the rather long nature of the options

    • Ex:

    • Combine three PDF files into one: gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFIle=all.pdf file1.pdf file2.pdf file3.pdf

    • Split pages 10 to 20 out of a PDF file: *gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dDOPDFMARKS=false -dFirstPage=10 -dLastPage=20*

      -sOutputFile=split.pdf file.pdf

Encrypting PDF Files with pdftk

If you’re working with PDF files that contain confidential information and you want to ensure that only certain people can view the PDF file, you can apply a password to it using the user_pw optio

  • One can do this by issuing a command such as pdftk public.pdf output private.pdf user_pw PROMPT

    • When you run this command, you'll receive a prompt to set the required password, which can have a maximum of 32 characters

    • A new file, private.pdf, will be created with the identical content as public.pdf, but anyone will need to type the password to be able to view it

Using Additional Tools

You can use other tools to work with PDF files, such as:

  • pdfinfo (It can extract information about PDF files, especially when the files are very large or when a graphical interface is not available)

    • Ex: pdfinfo /usr/share/doc/readme.pdf
  • flpsed (It can add data to a PostScript document. This tool is specifically useful for filling in forms or adding short comments into the document)

  • pdfmod (It's a simple application that provides a graphical interface for modifying PDF documents)

    • Using this tool, you can reorder, rotate, and remove pages; export pages from a document; edit the title, subject, and author; add keywords; and combine documents using drag-and-drop action