mpdf icon indicating copy to clipboard operation
mpdf copied to clipboard

Undefined array key "simple" in mpdf/mpdf/src/Mpdf.php:19560

Open craiglondon opened this issue 3 years ago • 8 comments

When chunking a long string passed to WriteHtml() and the setting simpleTables = true.

I found this bug

2022-02-08 15:10:04 [22.22.22.222][5574][jtgku4cd68c2vd4ko917et5qgh][error][yii\base\ErrorException:2] yii\base\ErrorException: Undefined array key "simple" in xx/yy/zz/vendor/mpdf/mpdf/src/Mpdf.php:19560 Stack trace: #0 xx/yy/zz/vendor/mpdf/mpdf/src/Mpdf.php(19560): yii\base\ErrorHandler->handleError() #1 xx/yy/zz/vendor/mpdf/mpdf/src/Tag/Table.php(761): Mpdf\Mpdf->_tableColumnWidth() #2 xx/yy/zz/vendor/mpdf/mpdf/src/Tag.php(247): Mpdf\Tag\Table->close() #3 xx/yy/zz/vendor/mpdf/mpdf/src/Mpdf.php(13718): Mpdf\Tag->CloseTag() #4 xx/yy/zz/frontend/modules/report/common/FormatWriter.php(283): Mpdf\Mpdf->WriteHTML() #5 xx/yy/zz/frontend/modules/report/common/FormatWriter.php(97): frontend\modules\report\common\FormatWriter->formatPDF()

In the method _tableColumnWidth()

if ($this->simpleTables) {
	if ($table['borders_separate']) { // NB twice border width
		$extrcw = $table['simple']['border_details']['L']['w'] + $table['simple']['border_details']['R']['w'] + $c['padding']['L'] + $c['padding']['R'] + $table['border_spacing_H'];
	} else {
		$extrcw = $table['simple']['border_details']['L']['w'] / 2 + $table['simple']['border_details']['R']['w'] / 2 + $c['padding']['L'] + $c['padding']['R'];
	}

This is mPDF and PHP version and environment (server/fpm/cli etc) I am using

PHP 7.3.26 mPDF v8.0.15 FPM/FastCGI NGINX

This is the PHP code snippet I use

<?php
$chunks = str_split($view, 150000);

$mpdf->simpleTables = true;

foreach ($chunks as $chunk) {
    $mpdf->WriteHtml($chunk);
}

craiglondon avatar Feb 09 '22 15:02 craiglondon

The example code is not reproducible as it is missing the actual HTML.

finwe avatar Feb 09 '22 15:02 finwe

I can't share the actual HTML, it has customer information in it. I adjusted the chunking from 150,000 characters to 500,000 characters, when I don't chunk the HTML, it works okay. Let me see if I can sanitize enough customer information in the HTML to put it here. $view strlen = 167452 Split into 2 chunks is 150000 and 17452

craiglondon avatar Feb 09 '22 16:02 craiglondon

Create a MCVE, please. It will be eventually needed for the unit test anyway.

finwe avatar Feb 09 '22 16:02 finwe

@finwe Pardon my ignorance, what is a MCVE?

craiglondon avatar Feb 09 '22 16:02 craiglondon

https://www.google.com/search?q=MCVE

finwe avatar Feb 09 '22 16:02 finwe

@finwe I think the issue is related to HTML comment tags. I removed them and I am not getting the error. The reason they are there is because a WYSWIG is used to edit Twig templates, and the Twig variables have to be commented out with the HTML comment out tags.

Suspected problem area

<tbody>          <!--                                                                                                   --> 
<tr>

MCVE below

<table style="border-collapse: collapse; width: 100%;" border="1">
<thead>
<tr>
<th style="text-align: center; background-color: #f0f0f0; color: #333; font-weight: bold; font-size: 8pt;" colspan="8">Thing <small>( XX - YYY, City of )</small></th>
</tr>
<tr>
<th style="width: 25%; font-size: 8pt;">Payment Details</th>
<th style="width: 20%; font-size: 8pt;">Payer</th>
<th style="width: 5%; font-size: 8pt;">Bill Date</th>
<th style="width: 20%; font-size: 8pt;">Transaction Details</th>
<th style="width: 5%; text-align: right; font-size: 8pt;">Amount</th>
<th style="width: 5%; text-align: right; font-size: 8pt;">Interest</th>
<th style="width: 10%; text-align: right; font-size: 8pt;">Fees</th>
<th style="width: 10%; text-align: right; font-size: 8pt;">Total</th>
</tr>
</thead>
<tbody>          <!--                                                                                                   --> 
<tr>
<td style="font-size: 8pt;">xdaw12929292<br /> Owner last name only: Dole<br /> Street name only: 000005 NoneWhere AVE<br /> Account #: XXXXXX<br /> Bill #: XXXX<br /></td>
<td style="font-size: 8pt;"> A Comedian, Old [email protected], (999) 555-2444 21 Mount Rd, 90210</td>
<td style="font-size: 8pt;">02/04/2022</td>
<td style="font-size: 8pt;">Payment Date: 02/04/2022 02:29:52 PM<br />Payment Type: eCheck<br />Transaction ID: YYYYYY<br />Quantity: 1</td>
<td style="text-align: right; font-size: 8pt;">1,500.00</td>
<td style="text-align: right; font-size: 8pt;">50.00</td>
<td style="text-align: right; font-size: 8pt;">1.75</td>
<td style="text-align: right; font-size: 8pt;">1,551.75</td>
</tr>

<tr>
<td style="text-align: right; font-size: 8pt;" colspan="7"><strong>Amount Total</strong></td>
<td style="text-align: right; font-size: 8pt;">1,500.00</td>
</tr>
<tr>
<td style="text-align: right; font-size: 8pt;" colspan="7"><strong>Interest Total</strong></td>
<td style="text-align: right; font-size: 8pt;">50.00</td>
</tr>
<tr>
<td style="text-align: right; font-size: 8pt;" colspan="7"><strong>Fees Total</strong></td>
<td style="text-align: right; font-size: 8pt;">1.75</td>
</tr>
<tr>
<td style="text-align: right; font-size: 8pt;" colspan="7"><strong>Grand Total</strong></td>
<td style="text-align: right; font-size: 8pt;">1,551.75</td>
</tr>
</tbody>
</table>

craiglondon avatar Feb 09 '22 16:02 craiglondon

You can't arbitrarily split a HTML string into chunks and pass it to WriteHTML. 9 times out of 10 you will be passing malformed HTML to this method which will cause problems like you have described. Chop up the HTML so that each chunk on its own is considered valid HTML and this problem will likely stop.

jakejackson1 avatar Feb 09 '22 17:02 jakejackson1

That's a good point, although the table key should be initialized properly anyway.

finwe avatar Feb 09 '22 17:02 finwe