Skip to content

Commit

Permalink
til: improved the stamp scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
foosel committed Jan 12, 2024
1 parent da34751 commit 90a5192
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
---
title: "How to print Deutsche Post stamps via the command line on a Brother QL label printer"
date: 2024-01-11
lastmod: 2024-01-12
tags:
- brother_ql
- bash
- command line
- linux
---

*Update from 2024-01-12: I've updated the scripts to support both 50mm and 62mm wide labels, and added some more whitespace trimming to the basic stamp. The post has been adjusted accordingly.*

I recently acquired a [Brother QL-820NWB label printer](https://www.brother-usa.com/products/QL820NWB) to be able to quickly create labels for boxes and such, and ideally also print out Deutsche Post's "print yourself" stamps with it. The Deutsche Post stamp shop allows me to download PDFs targeting the 62mm wide endless labels for that printer, for the two types of stamps I'm interested in (stamp, and address label with stamp). But my attempts in printing those directly to the printer through Gnome's printer integration weren't successful, things were too small, the cutter didn't work etc.

I knew that printing to the printer via my local instance of [brother_ql_web](https://github.com/pklaus/brother_ql_web) works flawlessly, and that the library this is based on, [brother_ql](https://github.com/pklaus/brother_ql), has a command line interface. So I thought, why not just convert the PDFs to individual PNGs, and then print those?
Expand All @@ -31,14 +34,16 @@ export BROTHER_QL_MODEL=QL-820NWB

Then I created two shell scripts, one for printing stamps and one for printing labels.

The first one, `porto_print`, takes care of printing the stamps. Not much work here is needed beyond conversion to individual images via `pdftoppm` and ensuring the native pixel size with `imagemagick`:
The first one, `porto_print`, takes care of printing the stamps. It resizes, trims, adds a new border and then extends to the native width for the selected label size (50mm by default, or 62mm if requested) while keeping a right alignment:

```bash
#!/bin/bash

# Usage:
#
# porto_print <pdf>
# porto_print <pdf> [50|62]

#!/bin/bash

tmpdir=$(mktemp -d)

Expand All @@ -48,21 +53,33 @@ cleanup() {
trap cleanup EXIT

PDF=$1
LABEL=${2:-50}
PNG=$(basename "${PDF%.*}")

case $LABEL in
"50")
WIDTH="554"
;;
"62")
WIDTH="696"
;;
*)
echo "Unsupported label size: $LABEL"
exit -1
;;
esac

echo "Converting PDF to individual PNGs..."
pdftoppm "$PDF" "$tmpdir/$PNG" -png -r 600

for file in $(ls $tmpdir/*.png); do
echo "Printing $file..."
mogrify -resize 696x "$file"
brother_ql print -l 62 "$file"
echo "Printing $file..."
mogrify -background white -bordercolor white -resize 696x -trim -border 25x25 -gravity east -extent ${WIDTH}x284 "$file"
brother_ql print -l $LABEL "$file"
done
```

The second one, `porto_address_print`, is a bit more involved. The individual label PDFs provided by Deutsche Post contain a ton of whitespace, so I wanted to do some clean-up first to produce less waste, and also keep the option open for me to switch to the 50mm wide label stock at a later point (which should fit the labels just fine, it's just that Deutsche Post ONLY supports 62mm wide labels for some reason).

So, I extract the images, then for each image, resize it to a target width of 696px, trim all white space, add a border again of 25px, and then blow that up to a width of 696px again, keeping the aspect ration and making sure the source image stays to the left of the new image.[^1]
The second one, `porto_address_print`, does basically the same, just with slightly different parameters and left alignment:

``` bash
#!/bin/bash
Expand All @@ -79,35 +96,36 @@ cleanup() {
trap cleanup EXIT

PDF=$1
LABEL=${2:-50}
PNG=$(basename "${PDF%.*}")

GEOMETRY=696x839
LABEL=62

# For 50mm labels, these values should work:
# GEOMETRY=554x839
# LABEL=50
case $LABEL in
"50")
WIDTH="554"
;;
"62")
WIDTH="696"
;;
*)
echo "Unsupported label size: $LABEL"
exit -1
;;
esac

echo "Converting PDF to individual PNGs..."
pdftoppm "$PDF" "$tmpdir/$PNG" -png -r 600

for file in $(ls $tmpdir/*.png); do
echo "Printing $file..."
mogrify -resize 696x "$file"
mogrify -trim -background white "$file"
mogrify -bordercolor white -border 25x25 "$file"
mogrify -background white -gravity West -extent $GEOMETRY "$file"
brother_ql print -l $LABEL "$file"
echo "Printing $file..."
mogrify -bordercolor white -background white -resize 696x -trim -border 25x25 -gravity West -extent ${WIDTH}x839 "$file"
brother_ql print -l $LABEL "$file"
done
```

Both of these were placed under `~/.local/bin` and made executable. I can now call them both from anywhere on the command line, just passing the path to the PDF to print.

The result is one or more nicely printed stamps or address labels, ready to be stuck to an envelope[^2]:
The result is one or more nicely printed stamps or address labels, ready to be stuck to an envelope:

![Printed stamp and printed stamp with address label, freshly printed from example files through the two scripts](result.jpg)

Now, this should hopefully make it easier for me to print all those address labels for OctoPrint sticker shipments in the future ;) Next step: Automated QR code labels for the various boxes on my shelves ^^

[^1]: Were I to switch to 50mm wide labels, I'd just have to change the target width of the final step to 554px and adjust the label type. The corresponding values have already be added as comments in the script.
[^2]: Looking at this I'll definitely look into saving some more whitespace on the basic stamp too though.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 90a5192

Please sign in to comment.