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

Before I ask the question, can I explain the ultimate goal in case there is an easier alternative.

I'm trying to create a PDF page of labels. Not the normal lines of text print. Rather, each label has a QR code on the right, (generated via GD) and a line of text along the bottom.

There is also a generic image to the right of QR code(An arrow with "Find your photos here") The label format is 3 wide x 8 deep

I am using PDF::AP12, and - by experimenting - have managed to =more or less= lay the lines of text where the bottom of labels will be.

This morning, I thought I would double check the layout, so created a Photoshop graphic (A4 @72dpi). IN THEORY, I can load the graphic, and then print text on top. (This would also be handy to learn for when I import the QR graphics. However ...

With text, I used:

$text = $page->text(); # Adds some text to page $text->font($font, 8); $across=12; $up=36; for ($x=0; $x<3; $x++){ # Across in 183dpi steps $new_across=$across+(183*$x); for ($y=0; $y<8; $y++){ # Up in 96dpi steps $new_up=$up+(98*$y); $text->translate($new_across, $new_up); $text->text('http://www. ###/abcde/122345'); $pdf->save

But now (lifted from CPAN docs) I have: $jpg=$pdf->image_jpeg('Path\To\label_layout.jpg');

What the heck do I do with the $jpg variable to add it to the PDF file??? I tried $jpg->translate(0,0) ... but that threw an error. So how do I position the graphic as well?

Replies are listed 'Best First'.
Re: Add images to PDF using pdf::ap12
by vinoth.ree (Monsignor) on Feb 19, 2015 at 13:34 UTC
      Thanks for your reply. I studied the code in your link, and discovered I was just missing two lines. Works fine now, so again many thanks for your help and fast response.
Re: Add images to PDF using pdf::ap12
by gpapkala (Acolyte) on Feb 19, 2015 at 14:10 UTC
    I am not sure if this would be of any help to you, but some time ago I wrote a module PDF::TableX which is built on top of PDF::API2 and simplifies generation of tabular pdfs. It can be relatively easy extended to work with images, see the code below:
    #!/usr/bin/env perl -w # this extends the regular text cell with image capabilities package ImageContent; use Moose::Role; use PDF::API2; sub draw_content { my ($self, $x, $y, $gfx, $txt) = @_; $y -= ($self->padding->[0] + 200); $x += $self->padding->[3]; if ($y < $self->margin->[2]) { return (200, $self->height, 1); } else { my $image_object = $gfx->{' api'}->image_jpeg( $self->content +); $gfx->image($image_object, $x, $y, 200, 200); $self->height(200+$self->padding->[0]+$self->padding->[2]); return (200, $self->height, 0); } } sub _get_min_width { 200 + $_[0]->padding->[1] + $_[0]->padding->[3] } sub _get_reg_width { 200 + $_[0]->padding->[1] + $_[0]->padding->[3] } sub _get_height { 200 + $_[0]->padding->[0] + $_[0]->padding->[2] } # the actual script to build the page use Moose::Util qw( apply_all_roles does_role); use PDF::TableX; use PDF::API2; my $contents = [ ['http://something/something', 'img.jpg'], ['http://something/something', 'img1.jpg'], ['http://something/something', 'img2.jpg'], ['http://something/something', 'img3.jpg'], ]; my $table = PDF::TableX->new(2,4); my $pdf = PDF::API2->new(); $pdf->mediabox('a4'); my $page = $pdf->page; $table ->padding(0) ->border_width(0) ->text_align('center'); $table->[0]->border_width(0); foreach my $i (0..3) { foreach my $j (0..1) { if ($j == 1) { apply_all_roles( $table->[$i][$j], 'ImageConten +t'); } $table->[$i][$j]->content($contents->[$i][$j]); } } $table->draw($pdf, $page); $pdf->saveas('output.pdf');
    or the documentation PDF::TableX
      Thanks for the time in posting your code, but that's a little "heavy" for me. I ended up using a couple of for ->next loops
      use PDF::API2; print "content-type: text/html\n\n"; use CGI::Carp qw( fatalsToBrowser ); $pdf = PDF::API2->new(-file => 'new.pdf'); # prepares filespace # 10 pages without above graphic = <3 seconds for ($doc=0; $doc<10; $doc++){ # Repeat for each new loop $page = $pdf->page(); # Adds new page to document. $page->mediabox('A4'); # Sets page size to A4 $font = $pdf->corefont('Verdana'); # Adds the Verdana font $text = $page->text(); # Adds some text to page $across=30; $up=48; $z=0; $qr=$pdf->image_png('test.png'); # Test pathname $arrow=$pdf->image_png('C:\...\arrows.png'); # Test pathname $gfx = $page->gfx; for ($x=0; $x<3; $x++){ # Across in 180dpi steps $new_across=$across+(180*$x); for ($y=0; $y<8; $y++){ # Up in 96dpi steps $new_up=$up+(96*$y); $text->font($font, 8); $text->translate($new_across, $new_up); $text->text('http://www.###/abcde/122345'); # Test URL $text->font($font, 9); $text->translate($new_across+11, $new_up+56); $text->text('Find Your'); $text->translate($new_across+16, $new_up+45); $text->text('Photos'); $text->font($font, 11); $text->translate($new_across+25, $new_up+34); $text->text('HERE'); $gfx->image($qr, ($new_across+90), ($new_up+10)); $gfx->image($arrow, ($new_across-4), ($new_up+12)); $z++; } } #$file=$pdf->image_png('C:\...label_layout.png'); # This is onl +y a test pathname #$gfx = $page->gfx; #$gfx->image($file, 0, 0); } # End of Page loop $pdf->save(); # Saves the file print "OK"; exit;
      I will now add some code from GD library to create 24 QR code images, and then load them into page 1 by 1. Then discard them, and prepare the next batch for next page and so on.

        So one more piece of code from myself. I wrote very small module that extends the PDF::API2 with QRCodes capabilities. Unfotunatelly although I sent this to current package maintener it never appeard in the PDF::API2

        Anyway you may have a look at https://github.com/grzegorzpapkala/PDF-API2-Resource-XObject-Form-QRCode. It relays on Text::QRCode. This way you can generate the QRcodes without extra GD calls. Maybe it would be easier this way.