簡體   English   中英

如何使用PHPWord將html標簽字符串轉換為word文檔?

[英]How to convert the html tag string to a word document using 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 中指定格式的內容。

以有限的方式它是可能的,但僅適用於基本的 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

我同意@Tomek 的回答,但您可能有更多的 html 標簽。

完整列表在這里:
https://github.com/PHPOffice/PHPWord/blob/4a530d1d97b064b87d9e2a1cc64cab996246569c/src/PhpWord/Shared/Html.php#L116

不要試圖用表格或樣式做復雜的思考。 它只是不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM