tecnickcom / TCPDF

Official clone of PHP library to generate PDF documents and barcodes
https://tcpdf.org
Other
4.18k stars 1.51k forks source link

Warning: Trying to access array offset on value of type null in ...\tcpdf.php on line 2468 #564

Open BrainforgeUK opened 1 year ago

BrainforgeUK commented 1 year ago

Cannot generate output due to ... TCPDF ERROR: Some data has already been output, can't send PDF file

Solution I implemented using a class override ... ` public function getCellHeight($fontsize, $padding=TRUE) {

    $height = ($fontsize * $this->cell_height_ratio);

    if ($padding && !empty($this->cell_padding) {
        $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
    }
    return round($height, 6);
}

`

jausions commented 1 year ago

@BrainforgeUK Could you provide an example of code generating this error ?

BrainforgeUK commented 1 year ago

I don't know how it got into the state where $this->cell_padding = null

This is one of a number of occassions where earlier versions would silently ignore the error, and it is often benign anyway - maybe in this instance a value of 0 would be assumed.

PHP 7.4 and 8 are less forgiving and the unexpected / 'this will never happen' has to be coded for! - such as here.

kosmro commented 1 year ago

I can confirm that I had the same issue as of PHP 7.4 onwards, using TCPDF v6.6.2 The suggested fix did resolve it for me as well - I have tested on both PHP 7.4 and PHP 8.1

@BrainforgeUK - your example is missing a close bracket BTW ;)

@jausions - The errors would be something like the attached screenshot. Screenshot from 2023-05-15 12-01-00

So the full function should maybe look something like this:

public function getCellHeight($fontsize, $padding=TRUE) { $height = ($fontsize * $this->cell_height_ratio); if ($padding && !empty($this->cell_padding)) { $height += ($this->cell_padding['T'] + $this->cell_padding['B']); } return round($height, 6); }

Hope this helps!

williamdes commented 1 year ago

Thanks @kosmro Could you open a pull-request request here to fix this?

kosmro commented 1 year ago

Thanks @kosmro Could you open a pull-request request here to fix this?

I'll look into it tonight :)

williamdes commented 1 year ago

Can you close this issue ? 6.6.3 is out and #620 is merged