[aklug] Re: Printing Return Address Labels the unix Way

From: Mike Bishop <mbishop@mtaonline.net>
Date: Wed Dec 19 2007 - 14:00:20 AKST

On Wed, Dec 19, 2007 at 12:12:48AM -0900, Fielder George Dowding wrote:
<snip>

> Yes! By all means Mike, I sure would like to see an awk script that uses
> roff. I don't use awk since I have switched to perl, but I find it
> enlightening to see how others have solved similar problems in the UNIX
> fashion.
>
> At Mable T. Caverly Senior Center, I have been using perl to write a
> PostScript file to print address labels from a comma delimited (actually
> it is a bang delimited) file produced from the OpenOffice Calc
> spreadsheet that holds the address data. Very ugly, but much better than
> struggling with OOWriter and mail merge to try to print labels. I
> suppose I should clean up the code, make it generic, and post it (some
> time after Christmas and the new year).
>

     Damn, now I have to document it! Okay, script below.

     Mr. Dowding,

     The "technique" used could easily be adapted to perl and csv
     addresses, bar codes etc., if warranted.

---%< -----

#!/bin/bash
#
# lblprint
#
# label printer mrb Apr 3 2001
# stdout is troff, pipe to groff (with the -U flag?)
# or straight to lpr for automagick conversion
#
# Unresolved bug: first label only is incorrectly positioned on large
# two-up Avery labels. This may be a ghostscript error. Use -s 1
#
# Fixed, I think, by using .sp 48p instead of blank lines for
# avery.5163 labels--mrb Feb 27 2002
#
# Defining fonts within labels larger than the labels, or similar idiocies
# will screw things up, of course.
#
# Use with other macro packages may cause suprises.
#
# Script "reverse engineered" (?) from jetlbl(1), a utility in the v.2
# (commercial) version of Jetroff, a DWB 2.0 troff to HP postprocessor
# by Rich Richardson (no longer available).
#
# Note: This version works well with a Kyocera FS-1010 but may need
# adjustment for printers with different page margins.
#
# Design:
# Prototype_file defines the label spec
# The awk section first generates a groff macro according to the
# protype spec, then calls that macro for each address label
# after moving to the correct page position (left to right, top to
# bottomt). Page ejects are forced if the next label will not
# fit on the page. This can cause a final blank page, but I am too
# lazy to fix!
#
# Protype file "avery.5160": Example 1 small address labels
# \"address.lbl : Standard three line label - Avery 5160 labels 1x2.675
# .ps 10 \# 10 point
# .vs 12 \# 12 pt vertical spacing
# .fp 1 KR \# Normal font
# .fp 3 KB \# Bold font
# .nf \# no fill
# .sp 5 \# space 5 lines
# .in 0.2i \# indent 0.2inches
# \f3\\*1\f1 \# <font 3>Line 1<font 1>
# \\*2 \# line 2
# \\*3
# \\*4 \# line 4 ....
# .in -0.2i \# reverse indent (housekeeping)
# .fi \# fill (housekeeping)
#
# Usage: lblprint -h1.0 -w2.675 -p avery.5160 label-file | groff > foo.ps
#
# Protoype file "avery.5163": Example 2 with return address with rule underneath
#
# \"shipping.lbl : Shipping/large envelope labels/2up - Avery 5163 labels 2x4
# .fp 1 KR
# .fp 2 KI
# .fp 3 KB
# .nf
# .sp 48p
# .in 0.2i
# .ps 9 \# 9 point for return address
# .vs 11p
# \f3WACO\fP
# PO Box 1027
# Willow, \s-1AK 99688-1027\s0
# \l'3.8i\(ru'
# .sp 0.5v
# .in 0.5i
# .ps 12
# .vs 14
# \f3\\*1\f1
# \\*2
# \\*3
# \\*4
# \\*5
# .in -1.25i
# .fi
#
# Usage: lblprint -p avery.5163 label-file | groff > foo.ps
#
# Note: preview foo.ps with gv, evince, or other ps reader/viewer
#
# Address file "label-file":
# Governor Sarah Palin
# 877 Commercial Drive
# Wasilla, AK 99654
#
# Senator Ted Stevens
# 522 Hart Senate Building
# Washington, DC 20510
#
# Senator Lisa Murkowski
# 709 Hart Senate Building
# Washington, DC 20510
#
# Representative Don Young
# 2111 Rayburn HOB
# Washington, DC 20510
#

die()
{
        echo $1 >&2
        exit ${2-9}
}

#set -xv

USAGE="lblprint -w <lbl_width> -h <height> -s <skip_labels> \
-p <proto_file> address_file \
(2x4 labels default)"

#defaults 2x4 shipping labels
wid=4.0
len=2.0
skip=0

while getopts h:w:s:p: c
do
        case $c in
        h) len=$OPTARG;;
        w) wid=$OPTARG;;
        s) skip=$OPTARG;;
        p) proto=$OPTARG;;
        \?) echo $USAGE&&exit 1;;
        esac
done
shift $(($OPTIND -1 ))

add_file=$1

[ -z $proto ]&&die "No label prototype file specified"
[ ! -f $proto ]&&die "Cannot read prototype file $proto"

[ -z $add_file ]&&die "No address file specified"
[ ! -f $add_file ]&&die "Cannot read address file $add_file"

# note: (I forget to add size spec sometimes ...)
echo "Note: lblprint running with -h=$len and -w=$wid" >&2

# okay, looks good

awk 'BEGIN { RS=""; FS="\n";
margin=0.25
p_length=10.75 # actually 11, but avoids tripping form feeds
p_width=8.5
Last_NF=0
}
# initalize
NR == 1 {
                across = int(p_width / wid) # labels across
                down = int(p_length / len) # labels down
                # print line length and the label macro
                print ".ll "wid"i"
                print ".de LB"
                print ".sp |\\\\n(vpu"
                print ".in 0"
                system("cat " proto)
                print ".br"
                print ".."
}

{

        lbl_num = NR + (skip - 1) # so that % math works correctly

        # print the label variables
        { for (i = 1; i <= NF; i++) print ".ds "i" \""$i }

        # clear any unused strings
         if (Last_NF > NF) { for (j = NF+1; j <= Last_NF; j++) print ".rm "j }

        # print the page offset for this label
        print ".po "margin + (wid * (lbl_num % across))"i"

        # print the vertical position for this label
        print ".nr vp "len * ( int(lbl_num/across) % down)"i"

        # print call to label macro
        print ".LB"

        # new page if we have filled a page
        if ( (lbl_num + 1) % (across * down) == 0 ) { print ".bp" }

        Last_NF = NF

}' len=$len wid=$wid skip=$skip proto=$proto $add_file

--%< -----

Rgds,

--
Mike Bishop 
Willow, Alaska
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed Dec 19 14:01:02 2007

This archive was generated by hypermail 2.1.8 : Wed Dec 19 2007 - 14:01:02 AKST