Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

PDF::Table last page

by PierreForget (Novice)
on Apr 16, 2018 at 01:41 UTC ( [id://1212956]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I made a PDF document on one of my servers with a main Table pages for filling tables with data, and made another page at the end to put all the summaries.

I did that with:

main_table($pdf, $some_data);

main_table2($pdf, $some_data); #using newpage2() and using footer2() to fill the summaries

and not using the some_data on the last page

It's working on this server with PDF::API2::Page version 2.023 and with PDF::Table version 0.9.7

You can see the dummy result here:

http://www.st-donat.com/pdf/formulaireassurance2.pdf

On another server with PDF::API2::Page version 2.033 and with PDF::Table version 0.10.0

It simply doesn't work and I get the message: Error: Invalid data received

From the code, I can see:

croak "Error: Invalid data received." unless ((ref($data) eq 'ARRAY') && scalar(@$data));

Which means that the $data is either not an array or not a scalar

If I put a stop in the code to get the value of $data, I get:

ARRAY(0x241c448)

Here is the part of the code that causes the problem (part of the main_table2 code):

$pdftable->table( # required params : détermine les rangées et colonnes du table +au $pdf, $page, $data, x => 80, w => 460, start_y => 490, # 550 next_y => 490, start_h => 120, next_h => 120, # some optional params padding => 5, padding_right => 5, font => $pdf->corefont("Helvetica", -encoding => "ut +f8"), font_size => 9, background_color_odd => "#EEEEEE", background_color_even => "#FFFFFF", #cell background color for + even rows header_props => $hdr_props, column_props => $col_props, new_page_func => \&newpage, # as defined above cell_render_hook => sub { # this is our newly introduced ca +llback function my ($page, $row, $col, $x, $y, $w, $h) = @_; }, );

Any way I can get a better way of seeing the variables to find the problem?

Thanks for the help (I use Perlmonks a lot to debug my Perl scripts and it's been very useful, but this one is a bugger...)

Replies are listed 'Best First'.
Re: PDF::Table last page
by huck (Prior) on Apr 16, 2018 at 02:41 UTC

    You have neglected to show us sample data tho. scalar(@$data) also would fail if $data was an EMPTY array.

    (ref($data) eq 'ARRAY') per http://perldoc.perl.org/functions/ref.html would fail if $data is a blessed object. If the referenced object has been blessed into a package, then that package name is returned instead.

      Hi,

      Thanks for the reply.

      I found a simple solution that works for me and it's quite funny.

      Instead of

      main_table($pdf, $some_data); main_table2($pdf, $some_data);

      I put

      main_table($pdf, $some_data); main_table2($pdf, $data2);

      Where the $data2 is an empty variable.

      In theory, it shouldn't work, because the $data2 array is empty. But it does exactly what I need!

      My culprit was probably because I repeated $some_data twice.

      Thanks to everybody

Re: PDF::Table last page
by vr (Curate) on Apr 16, 2018 at 16:48 UTC

    It's good you have solution that finally works, but we don't know what 'main_table', 'main_table2' (and 'newpage2', 'footer2'), that you keep mentioning, are about, and how they would react to empty array refs or undefined scalars ('empty variable' - what's that?).

    But, looking at source of PDF::Table, passing an empty array ref, as data, to its table method is, indeed, fatal error in 0.10.0, though it wasn't so in 0.9.7. Not sure what you expect to be placed, as a table, on that 'last page', if you pass no data. I haven't used this module before, but from documentation,

    my ($final_page, $number_of_pages, $final_y) = table($pdf, $page, $dat +a, %settings)

    perhaps means that table fills as many pages as required, automatically.

    -------

    A side-note: PDF::Table's POD, and then your code, pass an '-encoding' parameter to corefont method of PDF::API2. It's not a meaningful parameter and is just ignored. Perhaps '-encode' was supposed instead, but you shouldn't use it.

    Looking at source, it only makes sense if it is some single-byte encoding for a single-byte encoded font. Passing 'utf8' results in indices 129..255 in Encoding dictionary to stand for the same 'uniFFFD' name. Consider:

    use strict;
    use warnings;
    use utf8;
    use PDF::API2;
    
    my $pdf  = PDF::API2-> new;
    my $page = $pdf-> page;
    my $font = $pdf-> corefont( 'Helvetica', '-encode' => 'utf8' );
    my $text = $page-> text;
    
    $text-> font( $font, 20 );
    $text-> translate( 200, 700 );
    $text-> text( 'àáâã' );
    
    $pdf-> saveas( 'test.pdf' );
    

    Oops, broken PDF file with '.notdef' glyphs rendered. Just remove the '-encode' parameter, and file becomes OK.

    For 'core' fonts, single-byte encoding other than 'latin1' would be useless. For arbitrary unicode texts and TTFs which support required codepoints (i.e. ttfont method is called to create a double-byte encoded font), it looks like '-encode' parameter is ignored anyway. The PDF::API2's psfont method, and PostScript Type1 custom-encoded fonts are very retro, and of historical interest only.

    TL;DR -- don't use '-encode' (nor '-encoding'), its obsolete.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1212956]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found