It seems that PHPWord required an element to insert the output of \PhpOffice\PhpWord\Shared\Html::addHtml into. I'm trying to output the contents of a HTML string into a table cell, example below:
//Example HTML
$html = '<h1>Adding element via HTML</h1>';
$html .= '<p>Some well formed HTML snippet needs to be used</p>';
$html .= '<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>';
$html .= '<p>Unordered (bulleted) list:</p>';
$html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>';
$html .= '<p>Ordered (numbered) list:</p>';
$html .= '<ol><li>Item 1</li><li>Item 2</li></ol>';
$table = $section->addTable();
$target = $table->addRow()->addCell(10000)->addText();
\PhpOffice\PhpWord\Shared\Html::addHtml($target, $html);
The target cell remains empty but I can generate a Word2007 document without errors. I've tried without the addText() call but the resulting document is damaged.
Any suggestions on how I could achieve my objective?
EDIT: The example HTML above does work with $target = $table->addRow()->addCell(10000). I seem to have issues in the actual HTML that I am using, some tags seem to be creating issues. I have stripped those not required but now have ended up with an exception:
BadMethodCallException in AbstractContainer.php line 232:
Cannot add ListItem in TextRun.
This is strange as in the working example HTML above there are list items
Try this
$table->addRow();
$resource_cell_left = $table->addCell(3000);
$resource_cell_left->addText('Test', array('name' => 'Century Gothic', 'size' => 9, 'bold' => true), array('align'=>'left'));
$resource_cell_right = $table->addCell(6000);
$resource_text->addText('Test ', array('name' => 'Century Gothic', 'size' => 9, 'bold' => true), array('align' => 'right'));
\PhpOffice\PhpWord\Shared\Html::addHtml($resource_cell_right, $html);
$resource_cell_right->addTextBreak();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With