Forum


Replies: 2   Views: 1232
Php warning when generating word file with loadhtml
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by jeroen8087  · 09-02-2022 - 08:18

Using PHPDocx 12.5, I get several PHP warnings and notices in my log file after creating a Word file. I use CreateDocxFromTemplate and replaceVariableByHTML, which produces the errors.

Depending on how many variables are replaced in the template, I get the following errors more than once, sometimes over 100 of them for one Word file:

Severity: Warning  --> DOMDocument::loadHTML(): Tag close invalid in Entity, line: 1 /Users/rolandvaandrager/Development/app/source/vendor/phpdocx/classes/DOMPDF_lib.php 2641
Severity: Notice  --> Undefined index: text_decoration /Users/rolandvaandrager/Development/app/source/vendor/phpdocx/classes/HTML2WordML.php 2493
Severity: Notice  --> Undefined offset: 5 /Users/rolandvaandrager/Development/app/source/vendor/phpdocx/classes/HTML2WordML.php 2019
Severity: Notice  --> Trying to access array offset on value of type null /Users/rolandvaandrager/Development/app/source/vendor/phpdocx/classes/HTML2WordML.php 2019

 

The first loadHTML() error is caused by a <close> tag in the HTML, which is generated by line 2705 in DOMPDF_lib.php:

$str = str_replace('</body>', '<close></body>', $str);

I don't know why this is done, it creates invalid HTML. If I remove this statement though, I get an invalid Word file with many more errors about unclosed tags in WordML.

 

The notices are caused by using the @ sign before an array, which is not accepted by PHP 7.4. Line 2493 of HTML2WordML.php states:

if (($this->openLinks && @$properties['text_decoration'] != 'none') || @$properties['text_decoration'] == 'underline') {

Here, $properties has no key 'text_decoration', and @ does not suppress the notice. I think that isset should be used here. These notices are also generated on other lines in this file, where the @ sign is used the same way.

 

What can be done to suppress these errors, other than just suppressing all PHP errors and warnings?

Posted by admin  · 09-02-2022 - 09:47

Hello,

The warning

everity: Warning  --> DOMDocument::loadHTML(): Tag close invalid in Entity

is from PHP DOM, loadHTML function, and it appears when some not standard tag is added (as phpdocx uses to transform HTML such as the close tag) and the error level you are using is displaying all PHP warnings and PHP messages or you are using xdebug or the scream extension (https://pecl.php.net/package/scream), even @ is added to supress them.
phpdocx uses @ in the code when calling loadHTML from PHP DOM to remove that warning:

@$this->_xml->loadHTML($str);

This line (with @) hides the warning in all PHP versions (from PHP 5 to PHP 8.1), unless the error level you are using disables it. PHP 8 was updated to disable @ when a Fatal error is thrown, not warnings or notices (https://php.watch/versions/8.0/fatal-error-suppression).

If you are using a custom error level that shows all PHP messages, even those suppressed by @ as phpdocx includes, you can hide that specific warning adding:

libxml_use_internal_errors(true);

before transforming the HTML, for example:

libxml_use_internal_errors(true);
$docx->embedHTML($html);

About the notices, all versions of phpdocx applies the following error level when importing HTML:

error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);

show notices shouldn't appear unless your code is overwritting this default error level or or you are using xdebug. Added @ should hide the notices in all PHP versions (including PHP 7.4, PHP 8 and PHP 8.1). We have tested it with: PHP 5.2, PHP 5.6, PHP 7, PHP 7.3, PHP 7.4, PHP 8 and PHP 8.1 and no warnings or notices appear with the default PHP settings.

Today, phpdocx 12.5.1 has been launched (https://www.phpdocx.com/news/post/phpdocx-v12-5-release-notes/224) with three minor updates. This new version calls libxml_use_internal_errors internally before loadHTML to hide that warning automatically when using PHP settings that @ doesn't run as expected with PHP DOM loadHTML.

Please do some test using phpdocx standalone using PHP CLI mode, and check if you get the same notices with the default code and default error reporting settings.
Also please send to contact[at]phpdocx.com or post the error reporting level you are using and if you are using xdebug, and a smalll HTML you are transforming that shows the notices so we can test it using the same setup you are using.

Regards.

Posted by jeroen8087  · 18-02-2022 - 14:39

Hello,

Thank you very much for the extensive and informative answer. The new version of PHPDocx 12.5.1 solves the PHP warning. All the PHP notices were put in our log by some custom error handler I was not aware of. Changing that error handler to ignore notices also solves this.

Thanks again, have a nice weekend!