We have a laser printer at house. This Hewlett Packard LaserJet Pro CP1525nw Color Printer is an older mannequin, but it surely has been a terrific workhorse that prints reliably and in colour. I put it on our home network just a few years in the past utilizing our Raspberry Pi as a print server.
The LaserJet has been a terrific addition to my house workplace. Since I launched my company final 12 months, I’ve relied on this little laser printer to print handouts and different supplies for consumer conferences, workshops, and coaching classes.
My solely gripe with this printer is that it prints single-sided solely. If you need to print double-sided, you have to arrange a customized print job to do it your self. That’s inconvenient and requires handbook steps. In LibreOffice, I have to particularly arrange the print job to print the odd-numbered pages first, then reload the paper earlier than printing the even-numbered pages on the opposite aspect—however in reverse order.
If I have to print a PDF that somebody has despatched me, the method is similar. For a four-page doc, I first have to print pages 1 and three, then reload the paper and print pages 2 and four in reverse order. In the GNOME print dialog, you have to choose “Page Setup” to print odd pages and even pages.
Regardless of how I print, the general course of is to print the odd-numbered pages, reload the stack of printed pages into the paper tray, then print the even-numbered pages in reverse order. If I am printing a four-page doc, printing the even-numbered pages in reverse order means web page four prints on the again of web page three and web page 2 prints on the again of web page 1. Imagine my frustration in these few cases after I forgot to pick out the choice to print in reverse order when printing the even-numbered pages and ruined an extended print job.
Similarly, it is simple to neglect cope with paperwork which have an odd variety of pages. In a five-page doc, you first print pages 1, three, and 5. But whenever you reload the printed pages into the printer, you do not need web page 5. Instead, you solely need to load pages 1 and three. Otherwise, web page four will print on the again of web page 5, web page 2 will print on the again of web page three, and nothing will get printed on the again of web page 1.
To make issues simpler and extra dependable, I wrote a easy Bash script that automates printing duplex. This is principally a wrapper to print odd-numbered pages, remind me to reload the pages (and take away the final web page if wanted), then print the even-numbered pages.
Whenever I have to print a doc as duplex, I first convert the doc to PDF. This could be very simple to do. In LibreOffice, there is a toolbar icon to export instantly as PDF. You may also navigate underneath File— Export As—Export as PDF to do the identical. Or in every other utility, there’s often a Save to PDF characteristic. When doubtful, GNOME helps printing to a PDF file as a substitute of a printer.
How it really works
Once I’ve saved to PDF, I let my Bash script do the remaining. This actually simply automates the lpr instructions to make printing simpler. It prints odd pages first, prompts me to reload the paper, then prints the even pages. If the doc has an odd variety of pages, it additionally jogs my memory to take away the final web page after I reload the printed pages. It’s fairly easy.
The solely “programming” a part of the script is figuring out the web page depend, and determining if that is a fair or odd quantity. Both of these are simple to do.
To decide the web page depend, I take advantage of the pdfinfo command. This generates helpful information a few PDF doc. Here’s some pattern output:
$ pdfinfo All coaching - catalog.pdf
Creator: Writer
Producer: LibreOffice 6.three
CreationDate: Fri Oct 18 16:06:07 2019 CDT
Tagged: no
ConsumerProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 11
Encrypted: no
Page measurement: 612 x 792 pts (letter)
Page rot: zero
File measurement: 65623 bytes
Optimized: no
PDF model: 1.5
That output could be very simple to parse. To get the web page depend, I take advantage of an AWK one-line script to search for Pages: and print the second discipline.
pages=$( pdfinfo "$1" | awk '/^Pages:/ ' )
To determine if that is an odd and even quantity, I take advantage of the modulo (%) arithmetic operator to divide by two and inform me the rest. The modulo of two will at all times be zero for a fair quantity, and one for an odd quantity. I take advantage of this straightforward check to find out if the doc has an odd variety of pages, so I am going to have to take away the final web page earlier than printing the remainder of the doc:
if [ $(( $pages % 2 )) -ne 0 ] ; then
With that, writing the print-duplex.sh Bash script is a straightforward matter of calling lpr with the right choices to ship output to my printer (lpr -P “HP_LaserJet_CP1525nw”), to print odd-numbered pages (-o page-set=odd) or even-numbered pages (-o page-set=even), and to print in reverse order (-o outputorder=reverse).
Bash script
#!/bin/sh
# print-duplex.sh
# easy wrapper to print duplexcat<<EOF
$1 ($pages pages)
-------------------------------------------------------------------------------
Printing odd pages first
Please look ahead to job to complete printing...
-------------------------------------------------------------------------------
EOFlpr -P "HP_LaserJet_CP1525nw" -o page-set=odd "$1"
sleep $pagescat<<EOF
===============================================================================
Put paper again into the printer in EXACT OUTPUT ORDER (face down in tray)
then press ENTER
===============================================================================
EOFpages=$( pdfinfo "$1" | awk '/^Pages:/ ' )
if [ $(( $pages % 2 )) -ne 0 ] ; then
echo '!! Remove the final web page - this doc has an odd variety of pages'
fiecho -n '>'
learn xcat<<EOF
-------------------------------------------------------------------------------
Printing even pages
Please look ahead to job to complete printing...
-------------------------------------------------------------------------------
EOFlpr -P "HP_LaserJet_CP1525nw" -o page-set=even -o outputorder=reverse "$1"