Convert HTML code to doc using PHP and PHPWord

折月煮酒 提交于 2019-12-23 09:56:10

问题


I am using PHPWord to load a docx template and replace tags like {test}. This is working perfectly fine.

But I want to replace a value with html code. Directly replacing it into the template is not possible. There is now way to do this using PHPWord, as far as I know.

I looked at htmltodocx. But it seams it will not work either, is it posible to transform a peace of code like <p>Test<b>test</b><br>test</p> to a working doc markup? I only need the basic code, no styleing. but Linebreaks have to work.


回答1:


Here is the link to the github. It is working fine Html-Docx-js.

And it is the demo also available here.

Other option is this Link.

 $toOpenXML = HTMLtoOpenXML::getInstance()->fromHTML("<p>te<b>s</b>t</p>");
    $templateProcessor->setValue('test', $toOpenXML);



回答2:


The other answers propose H2OXML which only supports

  • Bold, italic and underlined text

  • Bulled lists

As described in their docs and their last update was in 2012.

I did some research and found a pretty nice solution:

$var = 'Some text';
$xml = "<w:p><w:r><w:rPr><w:strike/></w:rPr><w:t>". $var."</w:t></w:r></w:p>";

$templateProcessor->setValue('param_1', $xml);

The above example, shows how would be a striked text. Instead of "w:strike" you can use "w:i" for italic or "w:b" bold, and so on. Not sure if it works on all tags or not.




回答3:


Thanks for your answer, Varun.

The simple PHP library H2OXML works for me https://h2openxml.codeplex.com/

$toOpenXML = HTMLtoOpenXML::getInstance()->fromHTML("<p>te<b>s</b>t</p>");
$templateProcessor->setValue('test', $toOpenXML);

I can now convert html code to insert it using PHPWord.




回答4:


$content = '<p>Test<b>test</b><br>test</p>';

use it before IOFactory::createWriter();

\PhpOffice\PhpWord\Shared\Html::addHtml($section, $content);


来源:https://stackoverflow.com/questions/30076922/convert-html-code-to-doc-using-php-and-phpword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!