Recently I wanted to print some PDF files containing sheet music. The tedious way to do that, would be to open them one by one in Evince and press the print button. Surely there must be a more efficient way to do that?
$ ls -l --human-readable *.pdf
-r--r--r-- 1 amedee amedee 217K apr 15 2020 'Arthur original.pdf'
-r--r--r-- 1 amedee amedee 197K apr 13 2020 'Canal en octobre.pdf'
-r--r--r-- 1 amedee amedee 14K apr 13 2020 DenAndro.pdf
-r--r--r-- 1 amedee amedee 42K apr 14 2020 'Doedel you do.pdf'
-r--r--r-- 1 amedee amedee 57K apr 13 2020 Flatworld.pdf
-r--r--r-- 1 amedee amedee 35K apr 16 2020 'Jump at the sun.pdf'
-r--r--r-- 1 amedee amedee 444K jun 19 2016 'Kadril Van Mechelen.pdf'
-r--r--r-- 1 amedee amedee 15K apr 13 2020 La-gavre.pdf
-r--r--r-- 1 amedee amedee 47K apr 13 2020 'Le petit déjeuner.pdf'
-r--r--r-- 1 amedee amedee 109K apr 13 2020 LesChaminoux__2016_04_24.cached.pdf
-r--r--r-- 1 amedee amedee 368K apr 13 2020 'Mazurka It.pdf'
-r--r--r-- 1 amedee amedee 591K apr 13 2020 'Narrendans uit Mater.pdf'
-r--r--r-- 1 amedee amedee 454K apr 13 2020 'Neverending jig.pdf'
-r--r--r-- 1 amedee amedee 1,1M apr 14 2020 'Red scissors.pdf'
-r--r--r-- 1 amedee amedee 35K apr 13 2020 Scottish-à-VirmouxSOL.pdf
-r--r--r-- 1 amedee amedee 76K apr 14 2020 'Tarantella Napolitana meest gespeelde versie.pdf'
-r--r--r-- 1 amedee amedee 198K apr 15 2020 'Zot kieken!.pdf'
There are 2 console commands for printing: lp
and lpr
. One comes from grandpa System V, the other from grandpa BSD, and both are included in CUPS. The nice thing about these commands is that they know how to interpret PostScript and PDF files. So this is going to be easy: just cd
into the directory with the PDF files and print them all:
$ lp *.pdf
lp: Error - No default destination.
Oops. A quick Google search of this error message tells me that I don’t have a default printer.
Configuring a default printer
First I use lpstat
to find all current printers:
$ lpstat -p -d
printer HP_OfficeJet_Pro_9010_NETWORK is idle. enabled since za 12 mrt 2022 00:00:28
printer HP_OfficeJet_Pro_9010_USB is idle. enabled since za 12 mrt 2022 00:00:17
no system default destination
I have a HP OfficeJet Pro 9012e printer, which Ubuntu recognizes as a 9010 series. Close enough. It’s connected over network and USB. I’m setting the network connection as default with lpoptions
:
$ lpoptions -d $(lpstat -p -d | head --lines=1 | cut --delimiter=' ' --fields=2)
copies=1 device-uri=hp:/net/HP_OfficeJet_Pro_9010_series?ip=192.168.1.9 finishings=3 job-cancel-after=10800 job-hold-until=no-hold job-priority=50 job-sheets=none,none marker-change-time=0 media=iso_a4_210x297mm number-up=1 output-bin=face-down print-color-mode=color printer-commands=none printer-info printer-is-accepting-jobs=true printer-is-shared=true printer-is-temporary=false printer-location printer-make-and-model='HP Officejet Pro 9010 Series, hpcups 3.22.2' printer-state=3 printer-state-change-time=1649175159 printer-state-reasons=none printer-type=4124 printer-uri-supported=ipp://localhost/printers/HP_OfficeJet_Pro_9010_NETWORK sides=one-sided
I can then use lpq
to verify that the default printer is ready:
$ lpq
HP_OfficeJet_Pro_9010_NETWORK is ready
no entries
Printing multiple files from console
I found that if I naively do lp *.pdf
, then only the last file will be printed. That’s unexpected, and I can’t be bothered to find out why. So I just use ls
and feed that to a while
-loop. It’s quick and dirty, and using find
+xargs
would probably be better if there are “special” characters, but that’s not the case here.
There’s one caveat: when the PDF files are printed one by one, then the first page will be at the bottom of the paper stack, so I need to print them in reverse order.
$ ls --reverse *.pdf | while read f; do lp "$f"; done
With that command I got 17 print jobs in the printer queue, one for each file.
Now that I know how to print from console, I’ll probably do that more often. The man page of lp
describes many useful printing options, like printing double sided:
$ lp -o media=a4 -o sides=two-sided-long-edge filename