PHPWord
PHPWord copied to clipboard
Ability to insert formatted html chunks to template.
A request: It'll be great if we can insert html chunk to doc template, which is urgently I need to do now. e.g:
$doc = $PHPWord->loadTemplate('Template.docx');
$doc->setValueFromHtml('SomePlaceholder', '<h1>some value</h1>');
Is there anyone know how to do this, just for now ? by hack or something ? Thanks.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Hi, @egig, how about using PHPWord_Template.setValue(...)?
@RomanSyroeshko I can't, it generates errors.
For those who probably want to help, I create the question: http://stackoverflow.com/questions/22292195/insert-html-to-docx
@egig I think what you need is a parser that can interpret HTML tags into suitable PHPWord element, e.g. <h1>
to Title
and <b>
to bold
property of Font
. We don't have it yet, though I think this is good to have.
PHPExcel already has an HTML Reader. Perhaps you can tweak it for your own needs. Sorry I can't help much right now.
@ivanlanin yes, that is it, actually. Never mind, I'll take a look into the excel reader. it could be a solution. Thanks for the help.
Check this solution: http://stackoverflow.com/questions/27312321/solved-phpoffice-phpword-loadtemplate-not-rendering-word-as-it-should .... i've tested with success, using https://h2openxml.codeplex.com/
The other answers, propose H2OXML which support just
Bold, italic and underlined text
Bulled lists
from their docs and last update was in 2012, which wasn't a good option for me.
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.
has this gone any where really need this function and think this library need it as well
You can add html to table cell, see example:
$value = '<b>TEST</b>';
$wordTable = new \PhpOffice\PhpWord\Element\Table();
$wordTable->addRow();
$cell = $wordTable->addCell();
\PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value);
$templateProcessor->setComplexBlock($block_id, $wordTable);
You can add html to table cell, see example:
$value = '<b>TEST</b>'; $wordTable = new \PhpOffice\PhpWord\Element\Table(); $wordTable->addRow(); $cell = $wordTable->addCell(); \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value); $templateProcessor->setComplexBlock($block_id, $wordTable);
Omg thank you very much. Never though of using a table... I've used everything but a table! Thank you very much sir.
You can add html to table cell, see example:
$value = '<b>TEST</b>'; $wordTable = new \PhpOffice\PhpWord\Element\Table(); $wordTable->addRow(); $cell = $wordTable->addCell(); \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value); $templateProcessor->setComplexBlock($block_id, $wordTable);
Hi, i have issue if use Numbering Error Call to a member function addNumberingStyle() on null
Please help sir, Thanks
You can add html to table cell, see example:
$value = '<b>TEST</b>'; $wordTable = new \PhpOffice\PhpWord\Element\Table(); $wordTable->addRow(); $cell = $wordTable->addCell(); \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value); $templateProcessor->setComplexBlock($block_id, $wordTable);
Hi, i have issue if use Numbering Error Call to a member function addNumberingStyle() on null
Please help sir, Thanks
I have the same problem ¿do you found a solution?
You can add html to table cell, see example:
$value = '<b>TEST</b>'; $wordTable = new \PhpOffice\PhpWord\Element\Table(); $wordTable->addRow(); $cell = $wordTable->addCell(); \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value); $templateProcessor->setComplexBlock($block_id, $wordTable);
Hi, i have issue if use Numbering Error Call to a member function addNumberingStyle() on null Please help sir, Thanks
I have the same problem ¿do you found a solution?
You must create a table from the Section object:
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$wordTable = $section->addTable();
$wordTable->addRow();
$cell = $table->addCell();
$value = str_replace('&', '&', $value); // Fixed file error with symbol "&"
Html::addHtml($cell, $value);
$templateProcessor->setComplexBlock($block_id, $wordTable);