tecnickcom / TCPDF

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

Warnings if strings containe special chars like äöüß in german language #470

Open kruegge82 opened 2 years ago

kruegge82 commented 2 years ago

we using php 7.4 and TCPDF via composer latest version 6.4.4

[16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_map(): Expected parameter 2 to be an array, bool given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2004 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_merge(): Expected parameter 2 to be an array, null given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2012 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: Invalid argument supplied for foreach() in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 1844 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: max(): Array must contain at least one element in vendor/tecnickcom/tcpdf/tcpdf.php on line 6398 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_map(): Expected parameter 2 to be an array, null given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 1783 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: count(): Parameter must be an array or an object that implements Countable in vendor/tecnickcom/tcpdf/tcpdf.php on line 6402 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_merge(): Expected parameter 1 to be an array, null given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2012 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_merge(): Expected parameter 1 to be an array, null given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2012 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_merge(): Expected parameter 1 to be an array, null given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2012 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_map(): Expected parameter 2 to be an array, bool given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2004 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_merge(): Expected parameter 2 to be an array, null given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2012 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: Invalid argument supplied for foreach() in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 1869 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_map(): Expected parameter 2 to be an array, bool given in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2004 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: array_merge(): Expected parameter 1 to be an array, null given in tecnickcom/tcpdf/include/tcpdf_fonts.php on line 2012 [16-Jan-2022 10:58:59 Europe/Berlin] PHP Warning: Invalid argument supplied for foreach() in vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php on line 1844

databird commented 2 years ago

I got same error if I "utf8_decode" string before :

Cause bug : $pdf->Cell(0, 4, uf8_decode('special chars like äöüß in german'), 0, 1, 'C'); Solved : $pdf->Cell(0, 4, 'special chars like äöüß in german', 0, 1, 'C');

sgiehl commented 1 year ago

We have encountered a similar problem. But on PHP 8 it actually throws an error now.

Error: {"message":"array_map(): Argument #2 ($array) must be of type array, bool given","file":"/vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php","line":2005,"request_id":"48f88","backtrace":" on /vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php(2005)
#0 /vendor/tecnickcom/tcpdf/include/tcpdf_fonts.php(2005): array_map(Array, false)
#1 /vendor/tecnickcom/tcpdf/tcpdf.php(5321): TCPDF_FONTS::UTF8StringToArray('LIVE s...', true, Array)
#2 /vendor/tecnickcom/tcpdf/tcpdf.php(5126): TCPDF->getCellCode(276.99758333333, 10, 'LIVE s...', 0, false, 'C', 0, '', 0, true, 'T', 'M')
#3 /core/TCPDF.php(34): TCPDF->Cell(0, 10, 'LIVE s...', 0, false, 'C', 0, '', 0, false, 'T', 'M')
#4 /vendor/tecnickcom/tcpdf/tcpdf.php(3666): Piwik\TCPDF->Footer()
#5 /vendor/tecnickcom/tcpdf/tcpdf.php(3212): TCPDF->setFooter()
#6 /vendor/tecnickcom/tcpdf/tcpdf.php(3194): TCPDF->endPage()
#7 /core/TCPDF.php(73): TCPDF->AddPage('')

It seems the problem is that calling https://github.com/tecnickcom/TCPDF/blob/1ecad8827ae17c07d8839bef8d26943ebbf04812/include/tcpdf_fonts.php#L2004 which calls https://github.com/tecnickcom/TCPDF/blob/1ecad8827ae17c07d8839bef8d26943ebbf04812/include/tcpdf_static.php#L1814 might return false if the preg_split receives a string that is not utf8

@nicolaasuni What would be a preferable solution for that problem? Normally I would suggest to at least check if preg_split returns false and return an empty array in that case. But that would possibly simply hide the text in the resulting PDF, without any notice. We could also try to apply the preg_split on the input after calling utf8_encode on it. But as utf8_encode is marked as deprecated with PHP 8.2, I'm not sure if that would be a solution to go with for now. Or maybe you have another suggestion here? I'm happy to provide a PR for that, once it's clear how to solve this.