{"id":16708,"date":"2021-05-18T11:26:32","date_gmt":"2021-05-18T15:26:32","guid":{"rendered":"https:\/\/scruss.com\/blog\/?p=16708"},"modified":"2021-05-18T11:26:35","modified_gmt":"2021-05-18T15:26:35","slug":"cp2up-sh-fits-the-important-part-of-canada-post-print-labels-two-per-sheet","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2021\/05\/18\/cp2up-sh-fits-the-important-part-of-canada-post-print-labels-two-per-sheet\/","title":{"rendered":"cp2up.sh \u00e2\u20ac\u201d fits the important part of Canada Post print labels two per sheet"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"791\" src=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/labels-2up-1024x791.jpg\" alt=\"blurred (for privacy) 2-up landscape page of Canada Post Tracked Package (to USA) shipping labels made by this script\" class=\"wp-image-16710\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/labels-2up-1024x791.jpg 1024w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/labels-2up-320x247.jpg 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/labels-2up-160x124.jpg 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/labels-2up-768x593.jpg 768w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/labels-2up.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>no you will not read my 2-up shipping labels<\/figcaption><\/figure>\n\n\n\n<p>If you need to ship things, you&#8217;re probably not too keen on queuing at the post office right now. Canada Post&#8217;s <a href=\"https:\/\/www.canadapost-postescanada.ca\/cpc\/en\/personal\/sending\/parcels\/ship-online.page\">Ship Online<\/a> service is pretty handy if you have a printer. The PDFs it produces are okay to print on plain paper, but if you&#8217;re using full-sheet labels like <a href=\"https:\/\/www.avery.ca\/avery-full-sheet-shipping-labels-5165-template-n.html\">Avery 5165<\/a> you&#8217;re going to waste half a sheet of expensive labels.<\/p>\n\n\n\n<p>If you&#8217;ve got two parcels to mail, this shell script will extract the right side of each page and create a single 2-up PDF with both your labels on the same page. You will need:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><a href=\"https:\/\/linux.die.net\/man\/1\/pdftoppm\">pdftoppm<\/a><\/em> from <a href=\"https:\/\/www.glyphandcog.com\/opensource.html\">Poppler<\/a>;<\/li><li><a href=\"http:\/\/netpbm.sourceforge.net\/doc\/pnmcat.html\"><em>pnmcat<\/em><\/a> and <a href=\"http:\/\/netpbm.sourceforge.net\/doc\/pnmtopng.html\"><em>pnmtopng<\/em><\/a> from <a href=\"http:\/\/netpbm.sourceforge.net\/\">netpbm<\/a>;<\/li><li><a href=\"https:\/\/gitlab.mister-muffin.de\/josch\/img2pdf\"><em>img2pdf<\/em><\/a><\/li><\/ul>\n\n\n\n<p>On my Ubuntu system, you can get good-enough<a href=\"#note1\">\u00c2\u00b9<\/a> versions by doing this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install poppler-utils netpbm img2pdf<\/pre>\n\n\n\n<p>The code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n# cp2up.sh - fits the important part of Canada Post print labels 2 per sheet\n# scruss, 2021-05 - CC-BY-SA\n# hard-coded input name (document.pdf)\n# hard-coded output name (labels-2up.pdf)\n# accepts exactly two labels (sorry)\n\ndpi=600\nwidth_in=11\nheight_in=8.5\n# png intermediate format uses pixels per metre\ndpm=$(echo &quot;scale=3; $dpi * 1000 \/ 25.4&quot; | bc)\n# calculated pixel sizes, truncated to integer\nhalf_width_px=$(echo &quot;$width_in * $dpi \/ 2&quot; | bc | sed &#039;s\/\\..*$\/\/&#039;)\nheight_px=$(echo &quot;$height_in * $dpi&quot; | bc | sed &#039;s\/\\..*$\/\/&#039;)\n\npdftoppm -mono -r &quot;$dpi&quot; -x &quot;$half_width_px&quot; -y 0 \\\n\t -W  &quot;$half_width_px&quot; -H &quot;$height_px&quot; document.pdf labels\npnmcat -lr labels-1.pbm labels-2.pbm |\\\n    pnmtopng -compression 9 -phys &quot;$dpm&quot; &quot;$dpm&quot; 1 &gt; labels.png \\\n    &amp;&amp; rm labels-1.pbm labels-2.pbm\n# fix PDF time stamps\nnow=$(date --utc --iso-8601=seconds)\nimg2pdf -o labels-2up.pdf --creationdate &quot;$now&quot; --moddate &quot;$now&quot; \\\n\t--pagesize &quot;Letter^T&quot; labels.png \\\n    &amp;&amp; rm labels.png \n\n# saved from:\n# history | tail | awk &#039;{$1=&quot;&quot;; print}&#039; |\\ \n#           perl -pwle &#039;chomp;s\/^\\s+\/\/;&#039; &gt; cp2up.sh\n<\/pre><\/div>\n\n\n<p>It&#8217;s got a few hard-coded assumptions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>input name (<em>document.pdf<\/em>);<\/li><li>output name (<em>labels-2up.pdf<\/em>);<\/li><li>accepts exactly two labels (sorry).<\/li><\/ul>\n\n\n\n<p>Clever people could write code to work around these. <em>Really<\/em> clever people could modify this to feed a dedicated label printer.<\/p>\n\n\n\n<p>Yes, I could probably have done all this with one ImageMagick command. When ImageMagick&#8217;s command line syntax begins to make sense, however, it&#8217;s probably time to retire to that remote mountain cabin and write that million-word thesis on a manual typewriter. Also, ImageMagick&#8217;s PDF generation is best described as <em>pish<\/em>.<\/p>\n\n\n\n<p>One of the issues that this script avoids is aliasing in the bar-codes. For reasons known only to the anonymous PDF rendering library used by Canada Post, their shipping bar-codes are stored as smallish (780 \u00c3\u2014 54 px) bitmaps that are scaled up to a 59 \u00c3\u2014 19 mm print size. Most PDF viewers (and Adobe Viewer is one of these) will anti-alias scaled images, making them slightly soft. If you&#8217;re really unlucky, your printer driver will output these as fuzzy lines that no bar-code scanner could ever read. Rendering them to high resolution mono images may still render the edges a little roughly, but they&#8217;ll be <em>crisply<\/em> rough, and scanners don&#8217;t seem to mind that.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/cp-006.png\" alt=\"split image of simulated printed barcode: top image is five indistinct black-grey bars merging into a white background, bottom image is the same vertical lines, rendered crisply but showing some slightly rough edges\" class=\"wp-image-16711\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/cp-006.png 640w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/cp-006-320x240.png 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2021\/05\/cp-006-160x120.png 160w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption>fuzzy vs crisply rough: scaled image (top) vs direct-rendered (bottom), at simulated 600 dpi laser print resolution<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p id=\"note1\">\u00c2\u00b9: Debian\/Ubuntu&#8217;s <em>netpbm<\/em> package is roughly 20 years out of date for <a href=\"http:\/\/netpbm.svn.code.sourceforge.net\/p\/netpbm\/code\/trunk\/doc\/copyright_summary\">reasons that only a very few nerds care about<\/a>, and the much better package is blocked by Debian&#8217;s baroque and <a href=\"https:\/\/bugs.debian.org\/cgi-bin\/bugreport.cgi?bug=380172\">gatekeepery<\/a> packaging protocol. I usually build it from <a href=\"http:\/\/netpbm.sourceforge.net\/getting_netpbm.php\">source<\/a> for those times I need the new features.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you need to ship things, you&#8217;re probably not too keen on queuing at the post office right now. Canada Post&#8217;s Ship Online service is pretty handy if you have a printer. The PDFs it produces are okay to print on plain paper, but if you&#8217;re using full-sheet labels like Avery 5165 you&#8217;re going to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"cp2up.sh \u00e2\u20ac\u201d fits the important part of Canada Post print labels two per sheet on Linux","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[3272,3273,270,765,1679,1372],"class_list":["post-16708","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-canada_post","tag-labels","tag-linux","tag-pdf","tag-print","tag-shell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-4lu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=16708"}],"version-history":[{"count":2,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16708\/revisions"}],"predecessor-version":[{"id":16712,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16708\/revisions\/16712"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=16708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=16708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=16708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}