{"id":8579,"date":"2024-11-20T02:00:00","date_gmt":"2024-11-20T07:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=8579"},"modified":"2024-11-24T12:06:31","modified_gmt":"2024-11-24T17:06:31","slug":"print-double-sided-documents-with-this-bash-script","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=8579","title":{"rendered":"Print double-sided documents with this Bash script"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"8579\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>I have an old laser printer at home that I use occasionally. My Hewlett Packard LaserJet Pro CP1525nw Color Printer is a great workhorse that prints reliably and in color. While I don\u2019t use it all the time, this printer is useful when I need to print a proof of an article, or make a paper copy of an important document.<\/p>\n\n\n\n<p>My only limitation with this printer is that it can only print single-sided pages. If you want to print double-sided pages, you need to set up a custom print job to that yourself: you print the odd-numbered pages, then reload the pages into the printer and print the even-numbered pages:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"803\" height=\"587\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/11\/2sided_odd.png\" alt=\"Printing odd-numbered pages from LibreOffice\" class=\"wp-image-8580\"\/><figcaption class=\"wp-element-caption\">Printing odd-numbered pages from LibreOffice<\/figcaption><\/figure>\n\n\n\n<p>For example, to print a 4-page document, I have to print pages 1 and 3. Then I reload the pages face-down in the tray to print the even pages &#8212; but I need to print the even pages in reverse order: pages 4 and 2. It\u2019s not so bad for a short document, but it\u2019s a pain to get right when printing a longer document that might be 20 pages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"automating-the-steps\">Automating the steps<\/h2>\n\n\n\n<p>To make things easier, I wrote a short Bash script that automates the process of printing in \u201cduplex\u201d on a printer that only prints on one side at a time.<\/p>\n\n\n\n<p>Whenever I need to print a document in 2-sided mode, I first convert the file to PDF. This is easy to do if the file is not already in PDF format. For example, you can use the \u201cFile &gt; Export As &gt; Export as PDF\u201d feature in LibreOffice Writer, or you can do it right from the command line with this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ libreoffice --headless --convert-to pdf file.odt<\/code><\/pre>\n\n\n\n<p>Once I have the file in PDF format, my Bash script uses the <strong>lpr<\/strong> command with options to first print the odd-numbered pages in the document, then display a prompt so I know to reload the pages into the printer, then uses <strong>lpr<\/strong> a second time to print the even-numbered pages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"even-or-odd\">Even or odd?<\/h2>\n\n\n\n<p>The only tricky part is determining the page count, so I know if the document has an even number of pages (which is easy to print double-sided) or an odd number of pages (the last page doesn\u2019t need to be loaded back into the printer).<\/p>\n\n\n\n<p>To determine the page count, I use the <strong>pdfinfo<\/strong> command. This generates useful information about a PDF document, including the page count. For example, this document is 5 pages long:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pdfinfo wordle.pdf\nCreator:         Writer\nProducer:        LibreOffice 24.2\nCreationDate:    Sat Nov 16 16:20:22 2024 CST\nCustom Metadata: no\nMetadata Stream: no\nTagged:          yes\nUserProperties:  no\nSuspects:        no\nForm:            none\nJavaScript:      no\nPages:           5\nEncrypted:       no\nPage size:       612 x 792 pts (letter)\nPage rot:        0\nFile size:       65861 bytes\nOptimized:       no\nPDF version:     1.7<\/code><\/pre>\n\n\n\n<p>That output is very easy to parse with <strong>awk<\/strong>. To get the page count, I use <strong>awk<\/strong> to look for the <code>Pages:<\/code> line, and print the second (<code>$2<\/code>) field on the line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pdfinfo wordle.pdf | awk '\/^Pages:\/ {print $2}'\n5<\/code><\/pre>\n\n\n\n<p>To figure out if this is an even or odd number, I use the modulo (<code>%<\/code>) arithmetic operator to divide by 2 and print the remainder. For an odd number, the <em>modulo by 2<\/em> will always be 1; for an even number, it will always be zero. I can automate this in Bash using a few lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\npages=$( pdfinfo \"$1\" | awk '\/^Pages:\/ {print $2}' )\n\nif &#91; $(( pages % 2 )) -eq 1 ] ; then\n  echo \"odd\"\nelse\n  echo \"even\"\nfi<\/code><\/pre>\n\n\n\n<p>For my 5-page file, this prints \u201codd\u201d. Knowing if the page count is even or odd makes it easier to make a suitable prompt when I need to re-load pages into the printer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"writing-the-script\">Writing the script<\/h2>\n\n\n\n<p>Now I can put it all together. My script first prints the odd-numbered pages (1, 3, 5, ..) then waits for the printer to finish printing before displaying a prompt for me to re-load the pages into the printer. If the document has an odd number of pages, it also reminds me to remove the last page. With the pages loaded back into the printer, the script prints the even-numbered pages (2, 4, 6, ..)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\npages=$( pdfinfo \"$1\" | awk '\/^Pages:\/ {print $2}' )\n\necho \"Printing odd pages...\"\nlpr -P \"HP_LaserJet_CP1525nw\" -o page-set=odd \"$1\"\n\n# add a delay to give the printer time to finish:\nsleep $pages\n\necho \"Put the pages back into the printer IN THE EXACT ORDER\"\necho \"face-down in the tray.\"\n\nif &#91; $(( pages % 2 )) -eq 1 ] ; then\n  echo \"Remove the last page -- this document has an odd number of pages.\"\nfi\n\necho -n \"Print Enter when ready:\"\nread x\n\necho \"Printing even pages...\"\nlpr -P \"HP_LaserJet_CP1525nw\" -o page-set=even -o outputorder=reverse \"$1\"<\/code><\/pre>\n\n\n\n<p>Save this as a new script on your system, and make it executable with <code>chmod +x<\/code>. On my system, I called it <code>print-duplex<\/code>. For example, when printing a 5-page document, I see this output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ print-duplex wordle.pdf\nPrinting odd pages...\nPut the pages back into the printer IN THE EXACT ORDER\nface-down in the tray.\nRemove the last page -- this document has an odd number of pages.\nPrint Enter when ready:\nPrinting even pages...<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Use Bash to print 2-sided documents on a printer that can only print on one side.<\/p>\n","protected":false},"author":33,"featured_media":4314,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[149,5],"tags":[151,91],"class_list":["post-8579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","category-linux","tag-bash","tag-linux"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8579"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8579\/revisions"}],"predecessor-version":[{"id":8639,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8579\/revisions\/8639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4314"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}