Using Perl to print Avery labels.

277

This blog will discuss how to use the Perl module PostScript::MailLabels to print Avery labels.

The basic design is to have your user(s) run the following BASH shell script, which will then call a Perl script that builds a postscript file, then the BASH script will send the postscript file to the printer.

Here is the shell script your user(s) will run.

 #!/bin/bash

#####################################

#This shell script is used to print address labels.#
####################################

#Must export default printer environment variable
#in order for lpr to function correctly.  The rest
#will be executed from a Perl script.

#declare variables
FILEPATH=”/home/user1/”
DATE=`date +%m%d%y`
MYPERL=`which perl`
SCRIPT=${FILEPATH}’print_addresses.pl’

#get name of file user wants to process
echo “What printer do you want to print to?”
read PRINTER
#echo “Going to process: $DEFAULTPRINTER”

#set default printer to DEFAULTPRINTER
export PRINTER=$PRINTER

#call perl script which builds a postscript
#file sends it to user’s printer.
perl $SCRIPT

#send postscript file to the printer
`cat ${FILEPATH}$DATE | lpr -P $PRINTER`

exit 0

 The BASH script basically asks the user what printer they want to print to,  set’s the user’s DEFAULT printer, then calls your Perl script using the modulePostScript::MailLabels to build a postscript file, then the BASH script  sends it to the user’s printer.

Here is the Perl script which does most of the “heavy lifting.”

#!/usr/bin/perl -w

##########################################

#This script will open a file containing a list of                  #
##addresses, use the addresses to build a postscript file, #
##and send that file to a printer.                                           #
##########################################

#import required modules
use strict;
use PostScript::MailLabels;

#declare local variables
my $labels;
my @addresses;
my $filename;
my $filepath = ‘/home/user1/’;
my $date = `date +%m%d%y`;

#get name of file from user
print “Enter name of file you would like to process:  “;
$filename = <>;

##inform user of progress
print “Creating postscript file …nn”;

$labels = PostScript::MailLabels->new;

#create file handle for reading
open(NEWADDRESSES, “$filepath$filename”) or die(“Unable to open file: $!”);

#read file one line at a time
while () {

    #delete first line containing name;street1;street2;citystzip$
    next if /^name;/;
n
    $labels->labelsetup(
        Avery       => $labels->averycode(5961),
        PaperSize   => ‘letter’,
        postnet     => ‘no’
    );

    $labels->definelabel(‘clear’);

    $labels->definelabel(0,’fname’,’lname’);
    $labels->definelabel(1,’street’);
    $labels->definelabel(2,’city’,’state’,’zip’);

    #Match lastname, firstname, and address.
    my $pattern = ‘^(.+),s+(.+);(.+);(.*);(.+),s+(w{2})s+(d{5}|d{5}-d{4})’;

    $_ =~ /$pattern/;

    my $lname = $1;
    my $fname = $2;
    my $address1 = $3;
    my $address2 = $4;
    my $city = $5;
    my $state = $6;
    my $zip = $7;

    my @record;

    push @record, $fname;
    push @record, $lname;
    if ( $address2 !~ /^$/ ) {
        push @record, “$address1, $address2”;
    } else {
        push @record, $address1;
    }
    push @record, “$city,”;
    push @record, $state;
    push @record, $zip;

    #print $labels->makelabels( $addresses );
    push (@addresses, @record);

#close while loop
}

#open file handle for writing
open(PRINTADDRESSES, “>$filepath$date”) or die(“Unable to write file: $!”);
 #write postscript file
print PRINTADDRESSES $labels->makelabels( @addresses );

#close filehandle
close(PRINTADDRESSES);

#close filehandle
close(NEWADDRESSES);

#notify user of file completion and get printer name
print “Postscript file named $date has been created successfully.n”;

#exit cleanly
exit 0;

The Perl script basically asks the user what’s the name of the file they want to print, reads in the file (; delimited in this case), outputs a postscript file.

Obviously you will have to make some changes in order to get these scripts to work in YOUR environment!

Note: This is for Avery labels with Avery code 5961.  Please review  PostScript::MailLabels’s documentation on CPAN to see if the module supports your particular labels or not.  You will also probably have to tweak the  “labelsetup” and “definelabel” parts to fit your needs.  Also note that the file this script reads is in the format of name;strete1;street2;citystzip$ semicolon delimited with a trailing $.  You will have to tweak the regex if your file is in a differet format.

Disclaimer:  This blog entry comes with NO expressed warranty, guarantee, support, or maintenance of any kind!  Use at your own risk!  

Good luck and happy printing!