http://www.perlmonks.org?node_id=531482

madizen has asked for the wisdom of the Perl Monks concerning the following question:

I have some legacy code for a public library app to generate codabar barcodes to PDF for on-demand inventory labels. The old code made a call to $pdf->barcode() but that method is deprecated. Old code in question:
sub makebarcode { # given a string, return encoded codabar as image my $code = shift(@_); my $img = $pdf->barcode( -type => $type, -font => $pdffont, -code => $code, -quzn => $quzn, -umzn => $umzn, -lmzn => $lmzn, # fontsize at bottom -zone => $zone, -ofwt => $owft, -text => $label, -fnsz => $fnsz, -spcr => $spcr, ); return $img; }
Codabar functionality in PDF::API2 has moved to PDF::API2::Resource::XObject::Form::BarCode::codabar but I can't figure out how to give it these same args (input string, all those zoning params, etc. Admittedly this is probably due to a density issue in my own gray matter. I tried PDF::Report->drawBarcode which takes similar args, but that module's use of PDF::API2 also seems to be obsolete. Can someone please offer a translation for the new PDF::API2?

Replies are listed 'Best First'.
Re: translation request PDF::API old barcode call to new PDF::API
by idsfa (Vicar) on Feb 20, 2006 at 19:31 UTC

    To say that the documentation on this package is terrible would falsely imply that it existed at all ... A meditation or two have tried to gather some info on this package, but neither is this deep in the PDF::API2 tree.

    That said, I'll dig in a little. The pod contains a promising function, and diving into the guts shows that it will take all of your options except -type and -text. I don't know if you will need them to get the output you are looking for. Maybe the following will help:

    use PDF::API2; my $pdf = PDF::API2->new(-file => 'barcode.pdf'); my $bc = $pdf->xo_codabar( -font => $pdffont, -code => $code, -quzn => $quzn, -umzn => $umzn, -lmzn => $lmzn, # fontsize at bottom -zone => $zone, -ofwt => $owft, -fnsz => $fnsz, -spcr => $spcr, ); $pdf->save();

    Updated: So naturally, another example appears the next day. Doesn't directly help with this problem, but I'm adding the link here just to keep track of it.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
      Documentation? er... what's that? ;) The -type arg is obviously not needed anymore and I think I can add back the -text label (it is important) just by sending that to the pdf document separately from the barcode, as I have working examples of sending text to the document. However... while pdf->xo_codabar() now returns something, I need to place it on the page at a specific location. This used to be like this:
      # encode string $code as codabar image $bar my $bar = makebarcode ($code); # Working! Thank you! # Place it at x,y page coordinates with scale and frame options. # Doesn't work. $gfx->barcode($bar, $x, $y, $scale, $frame); # Don't work neither. $gfx->xo_codabar($bar, $x, $y, $scale, $frame)
      Now I get: Can't locate object method "what_I_tried" via package "PDF::API2::Content" Well, that's progress anyway. Just acquiring the barcode image was 80% of my battle. I will mull the meditations offered, but if you or anyone can just throw me the required "output $bar at coordinates" incantation, I have the rest of my legacy issues already wrapped. I found and tried:
      $gfx->image($bar, $x, $y, $scale);
      But $bar is not an image? (Anyway I get a blank document, which is better than an error message). Found it!
      # Works! $gfx->formimage($bar, $x, $y, $scale);
        Hey there.. Do you have a full example of this code? I am trying to do almost the exact same thing. What I need to do is create and print individual barcodes that will print onto Avery labels. Thanks, ~Donavon