PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Ability to insert formatted html chunks to template.

Open egig opened this issue 10 years ago • 12 comments

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.

egig avatar Mar 12 '14 02:03 egig

Hi, @egig, how about using PHPWord_Template.setValue(...)?

ghost avatar Mar 12 '14 11:03 ghost

@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 avatar Mar 13 '14 04:03 egig

@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 avatar Mar 13 '14 06:03 ivanlanin

@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.

egig avatar Mar 13 '14 08:03 egig

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/

calita78 avatar Feb 25 '15 14:02 calita78

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.

GarryOne avatar Jul 27 '16 16:07 GarryOne

has this gone any where really need this function and think this library need it as well

pascaldls avatar Sep 23 '17 20:09 pascaldls

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);

kindrosker avatar Dec 10 '20 04:12 kindrosker

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.

Warafux avatar Mar 07 '21 22:03 Warafux

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

adamwiguna avatar Dec 01 '21 16:12 adamwiguna

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?

albertolarah avatar Apr 04 '22 17:04 albertolarah

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('&', '&amp;', $value); // Fixed file error with symbol "&"
Html::addHtml($cell, $value);

$templateProcessor->setComplexBlock($block_id, $wordTable);

YummiK avatar Sep 21 '22 04:09 YummiK