in reply to Re^2: PDF::API2::Simple - pass parameter to header
in thread PDF::API2::Simple - pass parameter to header

I'm not sure whether your callback ever gets passed the current page number. The best is to wrap the header subroutine with your own header:

sub my_header { # ... do your own stuff: print "Got parameters @_\n"; # Call old header routine to output other stuff header(); }; my $pdf = PDF::API2::Simple->new( header => \&my_header, footer =>\&footer, margin_left => 15, margin_top => 15, margin_right => 15, margin_bottom => 45 );

Replies are listed 'Best First'.
Re^4: PDF::API2::Simple - pass parameter to header
by constantin.iliescu (Initiate) on Mar 11, 2010 at 15:49 UTC
    Still can't pass the parameter. I'm trying this:
    my $pdf = PDF::API2::Simple->new( header => \&my_header($CategoryName), footer => \&footer, margin_left => 15, margin_top => 15, margin_right => 15, margin_bottom => 45 ); sub my_header { my $pdf = shift; my $cat = shift; print $cat; header($pdf); } sub header { my $pdf = shift; # do my stuff }
    Erorr: Not a CODE reference at...

      No. That can't work. That's also not what I showed. You will need to supply that parameter yourself, outside of the "parameters". A subroutine reference never takes parameters. Maybe something like the following:

      my $pdf = PDF::API2::Simple->new( header => \&my_header($CategoryName), footer => \&footer, margin_left => 15, margin_top => 15, margin_right => 15, margin_bottom => 45 ); sub my_header { my $pdf = shift; my $cat = $CategoryName; print $cat; header($pdf); } sub header { my $pdf = shift; # do my stuff }

      ... or, alternatively, if your $CategoryName changes several times before your (different) PDF objects output themselves, you can create the subroutines anonymously on the fly:

      my $pdf = PDF::API2::Simple->new( header => sub { my_header($CategoryName), }, footer => \&footer, margin_left => 15, margin_top => 15, margin_right => 15, margin_bottom => 45 ); sub header { my $pdf = shift; # do my stuff }
        Thanks, Corion! I tried your suggestions. The first one can't work because the category name is changes in a loop I make after defining the pdf object. The second one gave the same 'not a code reference...' error. I placed a variable outside the fuctions which I set in my first function and use in the header function. Thanks you very much!