Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Perl and Address labels, oh yeah!

by brian_d_foy (Abbot)
on Dec 10, 2004 at 08:51 UTC ( #413783=perlmeditation: print w/ replies, xml ) Need Help??

Cue the song from Yello...yeah, the one Ferris Bueller made famous in the end credits. Play it as you read this.

Just when I thought life couldn't get any cooler, Perl kicks Microsoft Word out of bed and I learn something new about Mac OS X's Preview. I owe it all to PostScript:: MailLabels.

I'm getting the latest issue of The Perl Review ready to go in the mail tomorrow, which means I need to put each issue in an envelope and then attach an address label to each envelope. No big whoop: my wife and I start when "The Daily Show" starts, and by the time it's over, we're done.

For the last issue (which was also the first print issue, so the first one that I needed to mail), I used Microsoft Word to create the labels. I'm not sure why I did that, but it probably had something to do with the back of the label box saying something about the Avery number and templates in Word. Fair enough. That mostly worked once I figured out how to tell it to import that data.

Now, usually, once I've done something, doing it again is a lot faster. I've made labels once with Word, so even though it took me a while the first time, it shouldn't take me so long this time. But, it does, or at least it was until I gave up. Word's data import for the labels is incredibly obtuse. I just want to put some lines on the labels. Put the first line first, the second second, and so on. Do Word really care if it is a name or company or zip code? Let's not even talk about international addresses right now!

Switch to Google. Well, switch to Perlmonks first, because I want to see if I've gotten any more hate mail from recent posts. Okay, read that and then back to Google for "perl address label". Boo-yah! Check CPAN for PostScript:: MailLabels. It's current, or recent, or at least not from 1998. Oh, and it does Avery labels. Manna from God or evil ruse?

It has two examples, each of which demonstrate nearly ever feature. And get this: it cares what line is what too. Ugh. It does have some cool features to do all sorts of processing and guessing, but I just want to put text on a page. It's set up to do American addresses with only one street address line and no company name.

My little test script is the tiniest of scripts. It even figures out all of the Avery label things for me. Try doing that, Python! (When I googled "python address labels", I found ads for labels with pythons on them).

use PostScript::MailLabels; $labels = PostScript::MailLabels->new; $labels->labelsetup( Avery => $labels->averycode(8160), PaperSize => 'letter', Font => 'Times-Roman', ); my $addresses = [ map { chomp; [ split /:/ ] } <DATA> ]; print $labels->makelabels( $addresses ); __END__ brian d:foy:5250 N. Broadway Suite 157:Chicago:IL:60640
I pipe that input into a file I call Up until today, when I've needed to look at a PostScript file on my Mac, I use ps2pdf to turn it into a PDF.
% perl > % ps2pdf
This time was no different. What I normally don't do it open every file in the directory with Preview, but I was stupid this time, so I did.
% open *

The Preview application turned into a new PDF file. Let's try that again: get rid of the PDF that ps2pdf made and open just the PostScript file. Yep, Preview converts PostScript to PDF. Cool. That's just a little bonus.

Gees, I wish I could post pictures here because the output is beautiful. It's beautiful in the way that makes me say "Gees, look at what computers can do nowadays!" See my beautiful label

Check that out. It even has the little zip encoder thing at the bottom. Direct mail here I come.

Notice the order of the fields in the input though. That's the order that it expects and I can't just add more fields and have them magically show up. I know: I tried. That's always the first thing a hacker tries. "What happens if I just do this?". Well, it didn't work.

I make my way through the docs. There is a lot of stuff to specify what means what and what goes where. It is pretty cool even of the interface hurts my eyes. It's not heinous, but it isn't sufficiently advanced either. I have to define the fields, then define where they go in the label. I won't explain the details so you can have the same fun I had figuring it out. If I wasn't so lazy, I'd refactor some of that stuff.

use PostScript::MailLabels; $labels = PostScript::MailLabels->new; $labels -> labelsetup( Avery => $labels->averycode(8160), PaperSize => 'letter', Font => 'Times-Roman', ); $labels->editcomponent('first', 'name', 'no', 0, 'Helvetica' ); $labels->editcomponent('second', 'name', 'no', 1, 'Helvetica' ); $labels->editcomponent('third', 'name', 'no', 2, 'Helvetica' ); $labels->editcomponent('fourth', 'name', 'no', 3, 'Helvetica' ); $labels->editcomponent('fifth', 'name', 'no', 4, 'Helvetica' ); $labels->definelabel('clear'); $labels->definelabel(0,'first'); $labels->definelabel(1,'second'); $labels->definelabel(2,'third'); $labels->definelabel(3,'fourth'); $labels->definelabel(4,'fifth'); my $addresses = [ map { chomp; [ split /:/ ] } <DATA> ]; print $labels->makelabels( $addresses ); __END__ brian d foy:5250 N. Broadway Suite 157:Chicago IL 60640

Not so pretty, but who cares. I can now simply put lines of text on the page. I took off the nifty barcode because I need the space for some addresses with five lines. Take a look at the less beautiful but still functional label.

What you can't see, however, is that the margins are a bit off. Go from printer to printer and you'll find that they each have their own idea of where a page actually starts. That text is flush left and right against the top of the label. It's almost off of the label, and by the time the printer gets to the last line of labels (Avery 8160 has 10 rows of 3 labels), the alignment is really off.

No problem. Before I add some whitespace myself, I read through the docs again. I knew there was a way to adjust things, but as in all documentation reading, I don't really pay attention to things I don't immediately need. Still, somewhere in the doc soup, there was something about margins. A ha! I can shift the whole thing in either degree of freedom (alas, no rotating). I shift it to the right 1/16th inch and down 1/16th inch.

use PostScript::MailLabels; $labels = PostScript::MailLabels->new; $labels -> labelsetup( Avery => $labels->averycode(8160), PaperSize => 'letter', Font => 'Times-Roman', Y_Adjust => 1 / 16, X_Adjust => 1 / 16, ); #print Dumper( $labels->{LABELDEF} ); $labels->editcomponent('first', 'name', 'no', 0, 'Helvetica' ); $labels->editcomponent('second', 'name', 'no', 1, 'Helvetica' ); $labels->editcomponent('third', 'name', 'no', 2, 'Helvetica' ); $labels->editcomponent('fourth', 'name', 'no', 3, 'Helvetica' ); $labels->editcomponent('fifth', 'name', 'no', 4, 'Helvetica' ); $labels->definelabel('clear'); $labels->definelabel(0,'first'); $labels->definelabel(1,'second'); $labels->definelabel(2,'third'); $labels->definelabel(3,'fourth'); $labels->definelabel(4,'fifth'); my $addresses = [ map { chomp; [ split /:/ ] } <DATA> ]; print $labels->makelabels( $addresses ); __END__ brian d foy:5250 N. Broadway Suite 157:Chicago IL 60640

Perfecto! Load up the printer with the labels and print those bad boys. I wish I could show it to you, but then you'd get everyone's personal info. Follow the example and make your own mailing labels. It's time for Christmas cards, dontcha know.

brian d foy <>

2004-12-10 Janitored by Arunbear - added readmore tags, as per Monastery guidelines

Comment on Perl and Address labels, oh yeah!
Select or Download Code
Replies are listed 'Best First'.
Re: Perl and Address labels, oh yeah!
by dragonchild (Archbishop) on Dec 10, 2004 at 13:42 UTC
    It would be interesting to see how PostScript:: MailLabels matches up against PDF::Labels. I just answered a question about it yesterday, so I'm curious.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I looked at PDF::Labels too, but it didn't say the magic word "Avery". It looks like PDF::Labels just puts lines on the page (which is what I want), but on the other hand requires a bit more to set up the boundaries of each label.

      I think I would spend about the same amount of time using each one, but futsing with different things. I think PostScript::MailLabels is a good lower-level module, but if I was going to use it more than every couple of months I'd build a layer on top of it so I didn't have to look at it again.

      I still have the international labels to do, so maybe I'll give PDF::Labels a spin. I can steal the label layout data from PostScript::MailLabels or Microsoft Word (or my ruler).

      brian d foy <>
Re: Perl and Address labels, oh yeah!
by jimbojones (Friar) on Dec 10, 2004 at 14:40 UTC
    Too bad I didn't see this two years ago, before sending out the "Save the date" cards for our wedding

    My solution was to
    • Open the Excel address spreadsheet with Win32::OLE
    • Get the addresses off a certain sheet
    • Do some internationalization munging
    • Format and append to a Latex document
    • Call Latex and dvips
    The formating I wanted was slightly different from an Avery label, in that it had to 'fold over' inside a translucent envelope ... long story

    - j

      i wrote a quickie for my non-postscript printer. A template in RTF and a script that replaces the place holders in the RTF. has worked fine as a server application, supplying labels to several users on different systems. -- devin

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://413783]
Approved by johnnywang
Front-paged by htoug
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2015-11-25 08:51 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (672 votes), past polls