如何使用PHPWord将html标记字符串转换为word文档?

时间:2014-04-11 07:02:01

标签: php html phpword

我有一个HTML字符串,我希望以与HTML相同的格式和样式在word文档中打印。我正在使用PHPWord

当我给我的HTML字符串说:

$htmlval ="<h6><div style="text-align: center;"><b style=\\"font-size: x-large;\\">OFFER LETTER</b></div><font size=\\"3\\"><div style="text-align: center;"><span style=\\"font-size: medium;\\">(This goes as an email copy)</span></div>";

这是带有break和div的HTML标签。

为:

$section->addText($htmlval);

它会打印所有HTML标记,但我希望内容具有HTML中指定的格式。

2 个答案:

答案 0 :(得分:15)

以有限的方式可行,但仅限于基本的html标签。

<p>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<strong>,
<em>,<sup>,<sub>,<table>,<tr>,<td>,<ul>,<ol>,<li>

在更复杂的情况下它也不能很好地工作(例如表格中的rowspan / colspan)

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);

请在PHPWord github

上查看样本

答案 1 :(得分:0)

我同意@Tomek的答案,但你有更多的html标签可能。

完整清单如下:
https://github.com/PHPOffice/PHPWord/blob/4a530d1d97b064b87d9e2a1cc64cab996246569c/src/PhpWord/Shared/Html.php#L116

不要尝试用桌子或风格做复杂的思考。它只是没有用。

相关问题