16.printing

Introduction

Configuration

Printing Operations

Manipulating Postscript and PDF Files

Knowledge Check (Verified Certificate track only)

Summary

Introduction

  • Configure a printer on a Linux machine.
  • Print documents.
  • Manipulate postscript and PDF files using command line utilities.

Configuration

Printing on Linux

  • Printing itself requires software that converts information from the application you are using to a language your printer can understand.
  • The Linux standard for printing software is the Common UNIX Printing System (CUPS).

CUPS (Common UNIX Printing System) Overview

  • It converts page descriptions produced by your application (put a paragraph here, draw a line there, and so forth) and then sends the information to the printer. It acts as a print server for both local and network printers.
  • Printers manufactured by different companies may use their own particular print languages and formats.
  • CUPS uses a modular printing system which accommodates a wide variety of printers and also processes various data formats.

How Does CUPS Work?

CUPS carries out the printing process with the help of its various components:

  • Configuration files
  • Scheduler
  • Job files
  • Log files
  • Filter
  • Printer drivers
  • Backend

![[/Untitled 84.png|Untitled 84.png]]

Configuration Files

  • The print scheduler reads server settings from several configuration files, the two most important of which are **cupsd.conf** and **printers.conf**. These and all other CUPS related configuration files are stored under the **/etc/cups/** directory.
  • **cupsd.conf** is where most system-wide settings are located; it does not contain any printer-specific details. Most of the settings available in this file relate to network security, i.e. which systems can access CUPS network capabilities, how printers are advertised on the local network, what management features are offered, and so on.
  • **printers.conf** is where you will 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 should not be modified by hand.

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 (these 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 is commonly known as the print queue.

    ![[/Untitled 1 47.png|Untitled 1 47.png]]

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.

  • To view what log files exist, type: **$ sudo ls -l /var/log/cups**

    ![[/Untitled 2 36.png|Untitled 2 36.png]]

Filters, Printer Drivers, and Backends

  • CUPS uses **filters** to convert job file formats to printable formats. 
  • Printer drivers contain descriptions for currently connected and configured printers, and are usually stored under **/etc/cups/ppd/**.
  • The print data is then sent to the printer through a filter, and via a backend that helps to locate devices connected to the system.
  • So, in short, when you execute a print command, the scheduler validates the command 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**

Printing Operations

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. You can use these commands 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 **lpr** accept command line options that help you perform all operations that the GUI can accomplish. **lp** is typically used with a file name as an argument.
  • **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.
==Command== ==Usage==
**lp <filename>** To print the file to default printer
**lp -d printer <filename>** To print to a specific printer (useful if multiple printers are available)
**program | lpecho string | lp** To print the output of a program
**lp -n number <filename>** To print multiple copies
**lpoptions -d printer** To set the default printer
**lpq -a** To show the queue status
**lpadmin** To configure printer queues

Managing Print Jobs

  • 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.
==Command== ==Usage==
**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** 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 is purely a text format that contains the data fed to a PostScript interpreter.
  • Features of PostScript are:
    • It can be used on any printer that is PostScript-compatible; i.e. any modern printer
    • Any program that understands the PostScript specification can print to it
    • Information about page appearance, etc. is embedded in the page.
  • 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.

Working with enscript

  • enscript is a tool that is used to convert a text file to PostScript and other formats.

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

    • For example, 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.

==Command== ==Usage==
**enscript -p psfile.ps textfile.txt** Convert a text file to PostScript (saved to psfile.ps)
**enscript -n -p psfile.ps textfile.txt** Convert a text file to n columns where n=1-9 (saved in psfile.ps)
**enscript textfile.txt** Print a text file directly to the default printer

Converting between PostScript and PDF

  • 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.
  • Another possibility is to use the very powerful **convert** program, which is part of the **ImageMagick** package.
==Command== ==Usage==
**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 input.pdf output.ps** Converts input.pdf to output.ps
**convert input.ps output.pdf** Converts input.ps to output.pdf
**convert input.pdf output.ps** Converts input.pdf to output.ps

Manipulating PDFs

  • 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 metadata
  • Exporting bookmarks to a text file
  • Filling out PDF forms.

In order to accomplish these tasks there are several programs available:

  • qpdf
  • pdftk
  • ghostscript.

**qpdf** is widely available on Linux distributions and is very full-featured. 

**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. 

**Ghostscript** (often invoked using **gs**) is widely available and well-maintained.

Using **qpdf**

Command Usage
**qpdf --empty --pages 1.pdf 2.pdf -- 12.pdf** Merge the two documents 1.pdf and 2.pdf. 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. The output will be saved to new.pdf.
**qpdf --rotate=+90:1 1.pdf 1r.pdfqpdf --rotate=+90:1-z 1.pdf 1r-all.pdf** Rotate page 1 of 1.pdf 90 degrees clockwise and save to 1r.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.
**qpdf --decrypt --password=mypw private.pdf file-decrypted.pdf** Decrypt private.pdf with output as file-decrypted.pdf.

Using **pdftk**

==Command== ==Usage==
**pdftk 1.pdf 2.pdf cat output 12.pdf** Merge the two documents 1.pdf and 2.pdf. 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. 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 result in new.pdf.
**$ pdftk public.pdf output private.pdf user_pw PROMPT** apply a password to it using the **user_pw** option

Using **Ghostscript**

  • Ghostscript is widely available as an interpreter for the Postscript and PDF languages. The executable program associated with it is abbreviated to **gs**.
==Command== ==Usage==
$ gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite  -sOutputFile=all.pdf file1.pdf file2.pdf file3.pdf Combine three PDF files into one
$ gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dDOPDFMARKS=false -dFirstPage=10 -dLastPage=20\
sOutputFile=split.pdf file.pdf
Split pages 10 to 20 out of a PDF file

Using Additional Tools

  • **pdfinfo** It can extract information about PDF files, especially when the files are very large or when a graphical interface is not available.
  • **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 is a simple application that provides a graphical interface for modifying PDF documents. Using this tool, you can reorder, rotate, and remove pages; export images from a document; edit the title, subject, and author; add keywords; and combine documents using drag-and-drop action.

![[/Untitled 3 24.png|Untitled 3 24.png]]

  • Lab 17.1: Creating PostScript and PDF from Text Files

    1. Check to see if the enscript package has been installed on your system, and if not, install it.
    2. Using enscript, convert the text file /var/dmesg to PostScript format and name the result /tmp/dmesg.ps. As an alternative, you can use any large text file on your system. Make sure you can read the PostScript file (for example with evince) and compare to the original file. NOTE__: On some systems, evince may have problems with the PostScript file, but the PDF file you produce from it will be fine for viewing.
    3. Convert the PostScript document to PDF format, using ps2pdf. Make sure you can read the resulting PDF file. Does it look identical to the PostScript version?
    4. Is there a way you can go straight to the PDF file without producing a PostScript file on the disk along the way?
    5. Using pdfinfo, determine what is the PDF version used to encode the file, the number of pages, the page size, and other metadata about the file. If you do not have pdfinfo you probably need to install the poppler-utils package.
    • Solution

      1.
      \#Try
      which enscript         
      \#OUTPUT
      /usr/bin/enscript 
      
      \#If you do not get a positive result, install with whichever command is appropriate for your Linux distribution:
      apt-get install enscript
      yum install enscript
      zypper install enscript 
      
      2.
      enscript -p /tmp/dmesg.ps /var/log/dmesg
      evince /tmp/dmesg.ps 
      
      3.
      ps2pdf /tmp/dmesg.ps
      ls -lh /var/log/dmesg /tmp/dmesg.ps /tmp/dmesg.pdf
      \#OUTPUT
      -rw-rw-r-- 1 coop coop 28K Apr 22 13:00 /tmp/dmesg.pdf
      -rw-rw-r-- 1 coop coop 80K Apr 22 12:59 /tmp/dmesg.ps
      -rw-r--r-- 1 root root 53K Apr 22 11:48 /var/log/dmesg
      
      evince /tmp/dmesg.ps /tmp/dmesg.pdf 
      
      \#Note the difference in sizes. PostScript files tend to be large, while PDF is a compressed format.
      
      4.
      \#You may want to scan the man pages for enscript and ps2pdf to figure out how to use standard input or standard output instead of files.
      student:/tmp> enscript -p - /var/log/dmesg  | ps2pdf -  dmesg_direct.pdf         
      \#OUTPUT
      [ 15 pages * 1 copy ] left in -
      85 lines were wrapped
      
      
      student:/tmp> ls -l dmesg*pdf         
      \#OUTPUT
      -rw-rw-r-- 1 coop coop 28177 Apr 22 13:20 dmesg_direct.pdf
      -rw-rw-r-- 1 coop coop 28177 Apr 22 13:00 dmesg.pdf 
      
      
      student:/tmp> pdfinfo dmesg.pdf         
      \#OUTPUT
      Title:          Enscript Output
      Author:         Theodore Cleaver
      Creator:        GNU Enscript 1.6.6
      Producer:       GPL Ghostscript 9.07
      CreationDate:   Wed Apr 22 13:00:26 2015
      ModDate:        Wed Apr 22 13:00:26 2015
      Tagged:         no
      Form:           none
      Pages:          15
      Encrypted:      no
      Page size:      612 x 792 pts (letter)
      Page rot:       0
      File size:      28177 bytes
  • Lab 17.2: Combining PDFs

    You can convert two text files (you can create them or use ones that already exist since this is non-destructive) into PDFs, or you can use two pre-existing ones. Combine them into one PDF, and view the result.

    If pdftk is not installed, you can try to install. However, if you are on a system for which it is not available (such as RHEL7/CentOS7 or OpenSUSE), you will have to use qpdf or gs.

    • Solution

      \#First we can create two PDFs to play with, using enscript and then ps2pdf:
      cd /var/log
      enscript -p dmesg.ps /var/log/dmesg
      enscript -p boot.ps /var/log/boot.log
      ps2dpf dmesg.ps
      ps2dpf boot.ps 
      
      \#Of course you may use two pre-existing PDF files and substitute their names below.
      
      \#Method 1: Using qpdf:
      qpdf --empty --pages dmesg.pdf boot.pdf -- method1.pdf
      
      \#Method 2: Using pdftk:
      pdftk dmesg.pdf boot.pdf cat output method2.pdf
      
      \#Method 3: Using gs:
      gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=method3.pdf dmesg.pdf boot.pdf
      
      \#Now view them:
      ls -l method1.pdf method2.pdf
      evince method1.pdf method2.pdf
      
      \#How do the files compare?

Knowledge Check (Verified Certificate track only)

![[Screenshot_from_2022-07-05_10-18-45.png]]

Summary

  • CUPS provides two command-line interfaces: the System V and BSD.
  • The CUPS interface is available at http://localhost:631.
  • **lp** and **lpr** are used to submit a document to CUPS directly from the command line.
  • **lpoptions** can be used to set printer options and defaults.
  • PostScript effectively manages scaling of fonts and vector graphics to provide quality prints.
  • **enscript** is used to convert a text file to PostScript and other formats.
  • Portable Document Format (PDF) is the standard format used to exchange documents while ensuring a certain level of consistency in the way the documents are viewed.
  • **pdftk** joins and splits PDFs; pulls single pages from a file; encrypts and decrypts PDF files; adds, updates, and exports a PDF’s metadata; exports bookmarks to a text file; adds or removes attachments to a PDF; fixes a damaged PDF; and fills out PDF forms.
  • **pdfinfo** can extract information about PDF documents.
  • **flpsed** can add data to a PostScript document.
  • **pdfmod** is a simple application with a graphical interface that you can use to modify PDF documents.