This is:
Output a valid .docx file
PHP error and no output
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
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();
?>
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);
Most helpful comment
tidy(...)is adding<html><body>...</body></html>around your HTML.You should call with the following arguments. (check param 3)