如何将html标记字符串转换为使用PHPWord的word文档? [英] How to convert the html tag string to a word document using PHPWord?

查看:857
本文介绍了如何将html标记字符串转换为使用PHPWord的word文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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; \\>(这是电子邮件副本)< / span>< / div>;



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

<

  $ section-> addText($ htmlval); 

它会打印所有的HTML标签,但我想要HTML格式的内容。

解决方案

限制它的可能性,但仅限于基本的html标记。

 < p>,< h1>,< h2>,< h3>,< h4< ,h5>,< h6>,< strong>,
< em>,sup,< sub>,< table>,< tr> UL>,<醇>,<李>

在更复杂的情况下(例如表中的rowspan / colspan),它也不起作用

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

请检查样本: PHPWord github


I have a HTML string and I want to print in the word document in same format and style as in the HTML. I am using the PHPWord

When I give my HTML string say:

$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>";

this is the HTML tags with break and div.

to:

$section->addText($htmlval);

it prints all HTML tag but I want the content with the format specified in the HTML.

解决方案

In limited way its possible, but just only for basic html tags.

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

Its also not work well in more complicated cases (for example rowspan/colspan in tables)

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

Please check samples on : PHPWord github

这篇关于如何将html标记字符串转换为使用PHPWord的word文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆