Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert html to .docx

Tags:

html

php

docx

I use phpoffice/phpword to work with document templating, the example can be found here: https://github.com/PHPOffice/PHPWord/blob/master/samples/old/Template.php.

The problem is I need to insert formatted text which is html code generated from wysiwyg editor. I can't do:

$document->setValue('Value1', '<span>Sun</span>');

Document generation will be error, I guess because the library can't parse html along with the OpenXML. What I need to do is, probably, convert the html first to OpenXML, I tried it but always get:

enter image description here

I don't know. Any idea or suggestion how to deal with this ? will be greatly apreciated. Thanks.

like image 738
egig Avatar asked Feb 03 '26 03:02

egig


1 Answers

You can add html to table cell, see example:

$templateProcessor=$PHPWord->loadTemplate('Template.docx');

$value = '<b>TEST</b>';
$wordTable = new \PhpOffice\PhpWord\Element\Table();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
 \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value);
                
$templateProcessor->setComplexBlock('SomePlaceholder', $wordTable);
like image 170
Sergey Kharchishin Avatar answered Feb 05 '26 16:02

Sergey Kharchishin