Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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 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.
% perl addresses.pl > addresses.ps % ps2pdf addresses.ps
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 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.

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 <bdfoy@cpan.org>

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


In reply to Perl and Address labels, oh yeah! by brian_d_foy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-28 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found