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

show correct error on incorrect encoding #594

Closed rubo77 closed 1 year ago

rubo77 commented 1 year ago

Signed-off-by: Ruben Barkow-Kuder github@r.z11.de

E.G if you try this:

$pdf->multiCell(0, 4, 'Some non-UTF-8 text: '.unhtmlentities('ü'), 0, 'L');

You will get this error on PHP 8.1:

Fatal error: Uncaught ValueError: max(): Argument #1 ($value) must contain at least one element in tcpdf.php:6412

This commit will show a useful error with the causing string

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

rubo77 commented 1 year ago

The problem is, that this line:

// get array of unicode values
$chars = TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont);

sets $char to an empty array if $s contains non-utf8 characters

UTF8StringToArray() calls this line:

$chars = TCPDF_STATIC::pregSplit('//','u', $str, -1, PREG_SPLIT_NO_EMPTY);

which creates the empty array()

rubo77 commented 1 year ago

I moved the error code generation into the function UTF8StringToArray().

The problem always was before PHP8: if you create

new TCPDF(pageFormat, $unit, $pageSize, $isunicode)

with $isunicode== true, and there are non-utf8- characters in a string, the PDF generation was just ignoring this whole Cell for that string, leaving it empty without errror warning.

This should enlighten users a bit, what is going wrong then

rubo77 commented 1 year ago

Maybe we should add and !empty($str) ?

rubo77 commented 1 year ago

Please resolve this someone else. Sorry I don't have the time any more