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

Thanks, Corion! I define the pdf object like this:
my $pdf = PDF::API2::Simple->new( header => \&header, footer =>\&footer, margin_left => 15, margin_top => 15, margin_right => 15, margin_bottom => 45 );
and I want to pass a string parameter, which will appear in my header, depending on page.

Replies are listed 'Best First'.
Re^3: PDF::API2::Simple - pass parameter to header
by Corion (Patriarch) on Mar 11, 2010 at 15:38 UTC

    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 );
      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 }