|Syntactic Confectionery Delight|
Perl and Address labels, oh yeah!by brian_d_foy (Abbot)
|on Dec 10, 2004 at 08:51 UTC||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).
I pipe that input into a file I call addresses.ps. 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.
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.
The Preview application turned addresses.ps 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.
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.
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 <firstname.lastname@example.org>