Phpword: error when using tidy

Created on 31 Dec 2018  路  1Comment  路  Source: PHPOffice/PHPWord

This is:

  • [x ] a bug report
  • [ ] a feature request
  • [ ] not a usage question (ask them on https://stackoverflow.com/questions/tagged/phpword)

Expected Behavior

Output a valid .docx file

Current Behavior

PHP error and no output

Failure Information

Warning: DOMDocument::loadXML(): StartTag: invalid element name in Entity, line: 1 in /home4/public_html/libraries/lib_phpword/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php on line 78

How to Reproduce

Please provide a code sample that reproduces the issue.

This is the html input of tidy($html):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
<b>test</b>
</body>
</html>
<?php
function tidy($x)
{
    $options=array
    (
    'output-html'=>true,
    #'show-body-only'=>false,
    'show-body-only'=>false,
    'alt-text'=>'image',
    'output-encoding'=>'utf8'
    );
    $tidy=new tidy;
    $tidy->parseString($x, $options, 'utf8');
    $tidy->cleanRepair();
    return trim($tidy);
}
/* [START PHPWORD] */
require_once('libraries/lib_phpword/vendor/autoload.php');
$pw = new \PhpOffice\PhpWord\PhpWord();

/* [THE HTML] */
$section = $pw->addSection();
$html=tidy('<b>test</b>');
#die($html);
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);

/* [OR FORCE DOWNLOAD] */
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="test.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pw, 'Word2007');
$objWriter->save('php://output');
exit();
?>

Context

  • PHP version:
  • PHPWord version: 0.16

Most helpful comment

tidy(...) is adding <html><body>...</body></html> around your HTML.
You should call with the following arguments. (check param 3)

\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, true, false);

>All comments

tidy(...) is adding <html><body>...</body></html> around your HTML.
You should call with the following arguments. (check param 3)

\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, true, false);
Was this page helpful?
0 / 5 - 0 ratings