Hand Crafted
Send documents to a printer and automate many tasks from the command line.
Send documents to a printer and automate many tasks from the command line.
CUPS command-line commands let you quickly print documents remotely to a printer of your choice, print shell script results without user intervention, move print jobs to another printer, cancel jobs, and more.
In the first part of this series, "CUPS Administration," published in Raspberry Pi Geek last issue [1], I showed you how to create and manage printers and queues. In this article, although I distinguish between printers and queues, from the perspective of the command line, these are usually the same thing because multiple queues can be set up for a physical printer. This ability proves especially useful if your printers have various add-ons (e.g., additional paper trays filled with different kinds of paper).
CUPS provides two print commands, lp
(System V) and lpr
(BSD), both of which you can use on the Raspberry Pi. The CUPS developers have expanded these commands with their own options. Table 1 shows the most important commands for everyday work.
Table 1
lp and lpr: Print Commands
Action | lp Command | lpr Command |
---|---|---|
Print a file |
lp <file> |
lpr <file> |
Print destination |
lp -d <printer> <file> |
lpr -P <printer> <file> |
Number of copies (max = 100) |
lp -n<number> |
lpr -# <number> |
Print without filtering |
lp -o raw |
lpr -o raw |
Pages to print (comma-delimited list and ranges) |
lp -p <pages> |
– |
Thanks to the -o
option, both commands open up possibilities to further refine the printing command, such as choosing the paper size, toggling between single-side and duplex printing, grouping pages on one sheet, and much more (Table 2).
Table 2
lp and lpr -o Options
Action | Input | Notes |
---|---|---|
Paper size |
-o media=<paper format> |
a3, a4, a5, etc. |
Landscape orientation |
-o landscape |
– |
Single-sided printing |
-o sides = one-sided |
– |
Duplex (long edge) |
-o sides=two-sided-long-edge |
– |
Duplex (short edge) |
-o sides=two-sided-short-edge |
– |
Customize print data to fit page size |
-o fit-to-page |
– |
Group pages on one sheet |
-o number-up=<number> |
Possible values = 2/4/6/9/16 |
Print plain text (file / from pipe) |
||
Left margin |
-o page-left =<points> |
1 point = 1/72 inch (0.35mm) |
Right margin |
-o page-right=<points> |
– |
Top margin |
-o page-top=<points> |
– |
Page bottom edge |
-o page-bottom=<points> |
– |
Characters per inch |
-o cpi=<points> |
Typical values = 10 or 12 |
Lines per inch |
-o lpi =<points> |
Typical value = 6 |
Many printing options can be combined. For the examples in Listing 1, I always used the fs2000
printer described in the first part of the series as the target. The <file>
can be text, PostScript, or PDF. Some options only process pure text, which you feed in as a text file or pipe to the print command. In Listing 1 you can see in detail:
Listing 1
Print Examples
01 $ lpr -o number-up=2 -Pfs2000 <print_file> 02 $ lp -o landscape <print_file> 03 $ lpr -o fit-to-page -Pfs2000 <print_file> 04 $ lpr -o cpi=8 <text_file> 05 $ lpr -o lpi=3 <text_file> 06 $ lpr -o page-left=72 -o page-top=144 -Pfs2000 <text_file>
To exploit the printing speed of a device fully, you need to send it a pre-formatted print or text file without filtering:
$ lpr -o raw -Pfs2000 <print_file>
In this mode, the Kyocera FS-2000 used in the test almost reached the number of pages per minute stated in the product manual. You must understand the character-specific settings of the printer to do this, however, and change the character set of the text file accordingly – in this case, the recode
character set is used on MS-DOS.
The specification of the print data is not necessary if the data is piped to the print command. In the following example, I send shell output to the fs2000
printer, fitting four screen pages on a printed page:
$ cat <text_file> | lp -o number-up=4 -d fs2000
The two examples in Figure 1 display the different feedback messages from lp
and lpr
. When using lp
, you receive the job number of the applicable queue, which you can use for downstream processing of the print job.
Command-line commands also let you enumerate, move, stop, and delete print jobs. Several commands yield similar actions, because the CUPS project adopted many things simply for compatibility.
You can use the commands lpstat
and lpq
to discover which jobs are currently ready for printing. If you add the -o
option, lpstat
lists print jobs; if you do not define a specific printer, this command lists jobs from all queues.
The lpq
command uses the -a
option. If you want to restrict the output to a single printer, add -P<printer>
to the command. Figure 2 shows some appropriate queries. The first two commands display jobs for all printers. In the third query, lpq
informs you that the printer is not currently ready – but this is not true. The last query shows a queue without content; the printer is ready for jobs.
In any case, you can discover the job numbers of the print jobs, which you might need if you want to move or delete them. If a printer fails and you have a choice of several devices, you can move queued jobs to another device with lpmove
(Figure 3):
$ lpmove <jobno> <printer>
The commands cancel
and lprm
let you to delete individual print jobs by referring to their job number. If so desired, cancel -a <printer>
cancels all jobs that are currently waiting for a printer (Figure 4).
Pages: 4
Price $15.99
(incl. VAT)