in reply to Re^2: PDF, annotation method using Perl
in thread PDF, annotation method using Perl

it will generate an error

What is the error?  It works for me (if I uncomment the respective lines), i.e. it creates an annotation link to www.somewhere.com at the specified position on page 2.

OTOH, as I'm reading your ...have the first column "Content 1" to be a link, you would like to have every cell of column 1 be a link to some URL (not just one link per page).  Unfortunately, PDF::Table does not make this easy. What you would want is a configurable callback/hook function that's being called when a cell is rendered, so you can easily get at its current coordinates, etc. As it is, there is no such thing...  But you could patch PDF/Table.pm like this

} # line 615 ### insert this if (ref $arg{cell_render_hook} eq 'CODE') { $arg{cell_render_hook}->( $page, $rows_counter, $j, $cur_x, $cur_y-$row_h, $calc_column_widths->[$j], $row_h ); } ### $cur_x += $calc_column_widths->[$j]; # orig. line 616 }#End of for(my $j.... # orig. line 617

This would add a new option 'cell_render_hook', which you set to a function that gets called whenever a cell has been rendered. The function is being passed the following arguments:

page-obj, row-index, column-index, x, y, width, height

Your code would then look something like this:

use strict; use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "table-1.pdf"); $pdf->preferences( -thumbs => 1, ); sub newpage { my $page = $pdf->page; $page->mediabox(792,612); return $page; } my $page = newpage(); # some data to layout my $some_data =[ # same data as in your code... (not duplicated for brevity) ]; # build the table layout $pdftable->table( # required params $pdf, $page, $some_data, x => 70, w => 650, start_y => 550, next_y => 550, start_h => 500, next_h => 500, # some optional params padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", #cell background color for e +ven rows new_page_func => \&newpage, # as defined above cell_render_hook => sub { # this is our newly introduced callba +ck function my ($page, $row, $col, $x, $y, $w, $h) = @_; if ($col == 0) { # do nothing except for first column my $url = "http://www.somewhere.com/my.php?row=$row"; my $annot = $page->annotation(); $annot->url( $url, -rect => [$x, $y, $x+$w, $y+$h], -border => [1,1,1] ); } }, ), # do other stuff with $pdf $pdf->saveas();

which would produce this PDF.

Replies are listed 'Best First'.
Re^4: PDF, annotation method using Perl
by Anonymous Monk on Apr 02, 2009 at 12:27 UTC
    I don't know how but I can't get the links for all the cells on the first row to work, your PDF looks good, but I just ran the code above and I get no hyperlink at all.

      Have you patched your PDF/Table.pm as I described?  Without the patch, PDF::Table doesn't have an option "cell_render_hook", so it of course won't work...

      perl -MPDF::Table -e"print qq($INC{'PDF/Table.pm'}\n)"

      (Windows syntax) should tell you where Table.pm lives — just in case that's what kept you from editing it :)

        It does work, my bad I need more sleep and lots of more coffee. Thank you for the information very much. Going to the next phase. I think I should post the finished code here at some point it could be a nice reference about how use this module, it could help a lot of people looking for the same thing since documentation is nos really helpful. What do you think?
        Hi, nice work, how easy would be to add an extra row for customized title for each row, and another extra empty row on the bottom to displaying the current date?