“lp” and the automation of printing PDF’s

Apache FOP is actually really neat.  With a few jar files, an xslt file and a xml file it is possible to create PDF file which can then be viewed or printed on any PC without any loss of formatting.  It is so easy to write programs or scripts to create PDF files, so many you might want a more automated method for printing them.

I looked at my PDF viewer to see if it was possible to run it in a batch mode with a few parameters to automate the printing process of the PDF’s.    I had been hoping a well placed command line argument would do it but I couldn’t really find anything helpful.

Google was able to find some hints on how I could easily  do that on Linux (sorry windows users) without a normal PDF viewer.

The line printer command, lp, is used for the general printing of files.  It can also be used to print PDF files as well.  Like some Linux commands there are quite a few command line options.

LP – line printer

Option Description Example
-d <dest> The name of the printer to use. -d “HP-Color-LaserJet-CM1312-MFP”
-n <copies> How many copies of document. Three pages in this example. -n 3
-t <job name> Sets the job name. -t “birthday list”
-n <copies> How many copies of document. Three pages in this example. -n 3
-o <name=value> Pass common job options to line printer program.
-o number-up=<count> This will print multiple document pages to the printed page.

Two in this case.

-o number-up=2
-o <media=size> This will allow the document to be printed to different sizes of paper. The options are A4, letter and legal. -o media=A4
-o <landscape> This will rotate the document 90 degrees.  -o landscape
-o <landscape> Pass common job options to line printer program. -o number-up=2

For a full list of the command line options take a quick look at the man page for lp.

The simplest case is to simply name the printer and the file.

lp -d “HP-Color-LaserJet-CM1312-MFP” totals.pdf

Printing is usually a very dull command as it normally just works.  If for some reason you need to check up on it there are a few ways this can be done.  One of the most visible ways to see the printers would be to enter the following URL.

http://localhost:631/printers

list-of-printers

Note:  This assumes that cups is installed.  If for some reason cups is not installed just install using apt-get

sudo apt-get update && sudo apt-get install cups cups-client

This is probably more interesting when the number of printers is greater than one.  To see the contents, simply click on the printer to bring up the actual contents of the print queue.

print-jobs

The easiest way to remove any jobs from the print queue is from this screen.  Press the cancel job button and you will be prompted for the username and password, after which the job is deleted.

Of course, there are command line programs which could be used as well.

LPQ – line printer queue

This command will display a list of all the jobs that are currently waiting to print.

Option Description Example
-P <dest> The name of the printer to use. -P “HP-Color-LaserJet-CM1312-MFP”

If your printer is the default printer you don’t need to give name name of the printer to the command.  The default print queue will be displayed.

LPRM – cancel a print job

The lprm command will delete an entry from your print queue.

Option Description Example
-P <dest> The name of the printer to use. -P “HP-Color-LaserJet-CM1312-MFP”

 

Deleting print jobs are easily done from the command line as well.

gast@asus:~$ lpq -P HP-Color-LaserJet-CM1312-MFP
HP-Color-LaserJet-CM1312-MFP is ready
Rank Owner Job File(s) Total Size
1st dock 24 YearlyAverageCurrencyExchangeRa 1429504 bytes
gast@asus:~$ 
gast@asus:~$ lprm -P "HP-Color-LaserJet-CM1312-MFP" 24
gast@asus:~$ 
gast@asus:~$ lpq
HP-Color-LaserJet-CM1312-MFP is ready
no entries

 

References

http://www.tutorialspoint.com/unix_commands/lp.htm

http://www.tutorialspoint.com/unix_commands/lpq-cups.htm

http://www.tutorialspoint.com/unix_commands/lprm.htm

This entry was posted in Command line and tagged . Bookmark the permalink.