Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPWord reading HTML into table cell

Tags:

php

phpword

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

like image 563
David Faux Avatar asked Apr 21 '26 04:04

David Faux


1 Answers

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();
like image 65
woninana Avatar answered Apr 23 '26 16:04

woninana