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

Here is a sample of what I am going nuts trying to do, I would like to have the first column "Content 1" to be a link.
If I run with the code you mentioned here it will generate an error, I am still lost.

#!/usr/bin/perl use DBI; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use strict; use PDF::API2; use PDF::API2::Annotation; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "M:\\pdf\\table-1.pdf"); $pdf->preferences( -thumbs => 1, ); my $page = $pdf->page; $page->gfx; $page->mediabox(792,612); # some data to layout my $some_data =[ ["Header 1", "Header 2", "Header 3", "Header 4", "Header 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ["Content 1", "Content 2", "Content 3", "Content 4", "Content 5" ], ]; my $left_edge_of_table = 50; # build the table layout $pdftable->table( # required params $pdf, $page, $some_data, x => $left_edge_of_table, w => 495, start_y => 750, next_y => 700, start_h => 300, next_h => 500, # some optional params padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", #cell background color for +even rows new_page_func => sub { my $url = qq{ http://www.somewhere.com}; my $next_page = $pdf->page(); $next_page->mediabox(792,612); #my $annot = $next_page->annotation(); # an annotation is a c +omponent of a page #$annot->url($url,-rect =>[100,50,310,30],-border =>[1,1,1]); #return $annot; return $next_page; }, ), # do other stuff with $pdf $pdf->saveas();

Replies are listed 'Best First'.
Re^3: PDF, annotation method using Perl
by almut (Canon) on Apr 02, 2009 at 02:45 UTC
    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.

      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 :)