PhilterPaper / PDF-Table

Official repository for PDF::Table in Perl
https://www.catskilltech.com/FreeSW/product/PDF%2DTable/title/PDF%3A%3ATable/freeSW_full
Other
10 stars 15 forks source link

Unexpected "first word in paragraph (XXXXXX) doesn't fit into empty line!" #72

Closed mdeweerd closed 2 years ago

mdeweerd commented 3 years ago

I got the following error, this is the test case (also shown further below): tc.zip. It looks like the table width is not computed correctly internally (the autoexpand value is lower than the width required to make it work).

The source code says "This should never happen"

        !!! Error !!! first word in paragraph (XXXXXX) doesn't fit into empty line! at /usr/local/share/perl5/site_perl/5.32/PDF/Table.pm line 1726.
        PDF::Table::text_block(PDF::Table=HASH(0x800003a58), PDF::API2::Content::Text=HASH(0x801a11f28), "XXX XXXXXX", "x", 117.225850393701, "y", 731.842755905512, "w", ...) called at /usr/local/share/perl5/site_perl/5.32/PDF/Table.pm line 1084
        PDF::Table::table(PDF::Table=HASH(0x800003a58), PDF::API2=HASH(0x80007e990), PDF::API2::Page=HASH(0x8011b7f40), ARRAY(0x801a04178), "x", 56.6929133858268, "w", 643.464566929134, ...) called at ./tc.pl line 44

This is the test case code in clear (same as attached zip):

#!/usr/bin/perl
# e warnings;
use strict;
use diagnostics;

use PDF::API2;
use PDF::Table;
use JSON::PP;

use constant mm         => 25.4 / 72;
use constant in         => 1 / 72;
use constant pt         => 1;

my $pdftable = new PDF::Table;
my $pdf      = new PDF::API2( -file => "header_repeat_with_cell_props.pdf" );
my $page     = $pdf->page();
my $pageHeight = 297 / mm;
my $pageWidth  = 210 / mm;
$pdf->mediabox( $pageWidth, $pageHeight );           # Default= portrait
my $font  = $pdf->corefont( 'Helvetica',      -encoding => "utf-8" );
my $fontb = $pdf->corefont( 'Helvetica-Bold', -encoding => "utf-8" );

my $data;
$data='[[["XXX","XXX XXXXXX","XéXéXXXXX","XXXèXX","XXXXX","XXXXXX","XéXXXXXX X°","XXXX","XXXXX","XXXXX X¦","XXXXX XX XXXX (X)","XXXXXXXX XXXXXXXX (XXX)","XXXXX X (XX)",""],["XXXX_X_XX","XXX","12345","XXXX","XXXXX","XX","1","08-10-21","10:28:11","01","27.5","-526","-20",""],["XXXX_X_X","XXX","12345","XXXX","XXXXX","X","1","08-10-21","10:29:08","01","27.5","-526","-24",""]]]';;
my $widths='[[null,null,null,null,null,null,{"justify":"right","max_w":45.3543307086614,"min_w":45.3543307086614},{"justify":"center","max_w":51.0236220472441,"min_w":51.0236220472441},{"justify":"center","max_w":48.1889763779528,"min_w":48.1889763779528},{"justify":"right","max_w":34.0157480314961,"min_w":34.0157480314961},{"justify":"right","max_w":53.8582677165354,"min_w":53.8582677165354},{"justify":"right","max_w":73.7007874015748,"min_w":73.7007874015748},{"justify":"right","max_w":45.3543307086614,"min_w":45.3543307086614},null]]';
my $props='[]';

my $r=decodeJSON($data);
my @resultdata=@{$r};
$r=decodeJSON($widths);
my @columnprops=@{$r};
my $cell_props=decodeJSON($props);
my $tablewidth;
$tablewidth=429.448818897638; # Fails
$tablewidth=227/mm; # Fails
#$tablewidth=228/mm; # Passes
#print $tablewidth."\n";

my $curY=742.677401574803;
my $final_page;
my $number_of_pages;

    ( $final_page, $number_of_pages, $curY ) = $pdftable->table(

                # required params
                $pdf,
                $page,
                @resultdata,
                x       => 20 / mm,                 # Upper left X of table
                w       => $tablewidth,             # Width of table
                start_y => $curY,                   # Upper left of table842,595
                next_y  => $pageHeight - 35 / mm,   #
                start_h => $curY - 20 / mm,    # Height of table on initial page
                next_h  => $pageHeight -
                  ( 20 + 35 ) / mm,            # Height of table on next pages
                padding => 1 / mm,

                # padding_top => 10, # overrides padding
                # padding_left => 10, # overrides padding
                # padding_right => 10, # overrides padding
                # padding_bottom => 10, # overrides padding
                border             => 0.1 / mm,    # Width of border
                horizontal_borders => 0.1 / mm,    # Width of vertical border
                vertical_borders   => 0.1 / mm,    # Width of vertical border

    # border => , # Width of border
    # horizontal_borders => , # Width of vertical border
    # vertical_borders => , # Width of vertical border
    # border_color => 'red', # Color of border
    # font => $pdf->corefont("Helvetica", -encoding => "utf8"),  #can be any PDF::API2::Resource::* type of font Default: 'Times' with UTF8 encoding
                font => $font,

                # font_color => '#333333', - Font color for all rows
                # font_color_odd => 'purple', - Font color for odd rows
                # font_color_even - Font color for even rows
                background_color_odd =>
                  "#EEEEEE",    #- Background color for odd rows
                background_color_even =>
                  "#FFFFFF",    #- Background color for even rows

       #new_page_func  => &newPage2, # Reference to code that returns PDF:API2::Page

                #max_word_length    => 20,    # Split words longer than this.
                # some optional params
                font_size => 9 / pt,

                # column properties
                column_props => @columnprops,

                # Header properties
                header_props => {
                    bg_color   => "silver",
                    font       => $fontb,
                    font_size  => 8,
                    font_color => "#000000",
                    repeat     => 1,
                },

                #header_props => {font=>$font,font_size=>9},
                cell_props => $cell_props
            );

$pdf->saveas();

sub decodeJSON {
    my $str=shift;
    my $json = JSON::PP->new->allow_nonref;
    return $json->decode($str);    
}
PhilterPaper commented 3 years ago

I have been able to replicate this with the latest (official) release of PDF::Table, for both PDF::API2 and PDF::Builder. It doesn't happen with my current working copy of PDF::Table (code in-progress), so hopefully it can be fixed soon.

I see you are using corefont with utf-8 encoding. You should be aware that only ttfont can properly handle multibyte encodings; corefont, psfont, etc. must stick to single byte encodings. This applies to both PDF::API2 and PDF::Builder.

mdeweerd commented 3 years ago

Thanks for the feedback regarding corefont - I haven't run into representation issues yet.

Happy to see that the testcase allowed you to reproduce the problem and confirm that it's apparently already fixed in your working copy ;-).

abeverley commented 3 years ago

Hi @PhilterPaper - thanks for your work on this. I'm also seeing the same issue and I wondered whether you could share the fix you have, so that I can see whether it fixes my specific problem?

PhilterPaper commented 3 years ago

Unfortunately, it's an extensive rewrite of Table.pm (for the column width issue), so there isn't some little set of changes you could drop in to patch it. I was going to get extensive changes to PDF::Builder out of the way first, but if people are getting blocked by PDF::Table problems, I will have to consider finishing the column width fixes (and one or two other minor bugs) and pushing out PDF::Table first.

abeverley commented 3 years ago

Thanks for the quick reply - much appreciated. Would some sponsorship help you to clear issues?

PhilterPaper commented 2 years ago

This appears to be fixed in the upcoming 1.003 release (out within a few days). Closing.

abeverley commented 2 years ago

Brilliant, thanks @PhilterPaper - confirmed working. Thanks very much for fixing this long-standing problem of mine.

PhilterPaper commented 2 years ago

No problem! Although it did give me a scare to see a fresh entry in this bug report... I feared that maybe the fix had regressed during the build process. Much relieved to see it was only a "Thank You" (and You're Welcome!).