Skip to content

Instantly share code, notes, and snippets.

@jackl0phty
Last active October 13, 2015 19:58
Show Gist options
  • Save jackl0phty/4247908 to your computer and use it in GitHub Desktop.
Save jackl0phty/4247908 to your computer and use it in GitHub Desktop.
Perl script to Print Avery Labels.
#!/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. #
###############################################################################
# Licensed under the Apache License, Version 2.0 (the "License"), #
# For any questions regarding the license of this software, please refer to #
# the actual license at http://www.apache.org/licenses/LICENSE-2.0.txt. #
###############################################################################
# DISCLAIMER OF WARRENTY #
# BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR #
# THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN #
# OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES #
# PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED #
# OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO #
# THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE #
# SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, #
# REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR #
# AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY #
# MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, #
# BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, #
# OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE #
# SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED #
# INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIESOR A FAILURE OF THE #
# SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER #
# PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. #
###############################################################################
# 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 ...\n\n";
$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;/;
$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;
# Uncomment line below to debug.
#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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment