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

Hi, I want to use perl to add a 2D Datamatrix on the first page of existing PDF files. I've been reading about Barcode::DataMatrix and several PDF modules, but I'm not sure on how to get this done.

Any advise or code samples would be greatly appreciated. Thanks, Kurt

Replies are listed 'Best First'.
Re: Placing 2D Datamatrix on PDF
by kcott (Archbishop) on Jan 17, 2014 at 19:27 UTC

    The module PDF::API2::Simple provides a "Simplistic wrapper for the excellent PDF::API2 modules". This is probably the easiest to use with these methods:

    • open()"This method opens an existing PDF for editing. ..."
    • image()"Renders an image onto the PDF. ..."

    See also: PDF::API2

    -- Ken

Re: Placing 2D Datamatrix on PDF
by Anonymous Monk on Jan 17, 2014 at 19:11 UTC

    This basically seems to work, sweeten to taste:

    use Barcode::DataMatrix; use PDF::API2; my $x = 100; my $y = 200; my $sz = 10; my $mat = Barcode::DataMatrix->new()->barcode('Hello World'); my $pdf = PDF::API2->open('original.pdf'); my $page = $pdf->openpage(1); my $content = $page->gfx(); for my $i (0 .. $#$mat) { for my $j (0 .. $#{$mat->[$i]}) { if ($mat->[$i][$j]) { $content->rect($x+$sz*$j, $y-$sz*$i, $sz, $sz); } } } $content->fill(); $pdf->saveas('modified.pdf');
      Thank you very much. I'll request the installation of the modules and test this out asap. Regards, Kurt
      Tested this and it works great ! Thanks, Kurt
      Hi, I tried the code. After including the necessary packages, I get the below error.
      C:\>perl c:\PerlScripts\test.pl
      String found where operator expected at C:/Strawberry/perl/site/lib/Barcode/Data
      Matrix.pm line 9, near "has "encoding_mode""
              (Do you need to predeclare has?)
      String found where operator expected at C:/Strawberry/perl/site/lib/Barcode/Data
      Matrix.pm line 17, near "has "process_tilde""
              (Do you need to predeclare has?)
      syntax error at C:/Strawberry/perl/site/lib/Barcode/DataMatrix.pm line 9, near "
      has "encoding_mode""
      BEGIN not safe after errors--compilation aborted at C:/Strawberry/perl/site/lib/
      Barcode/DataMatrix.pm line 142.
      Compilation failed in require at c:\PerlScripts\test.pl line 1.
      BEGIN failed--compilation aborted at c:\PerlScripts\test.pl line 1.
      
      C:\>
      
      Please help me out with this. Thanks in advance
        How did you "include the necessary packages"? It seems Barcode::DataMatrix needs Any::Moose, but it's not working correctly.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ