Adding table layout to the generated HTML if element has layout style. This is useful when using creating PDF from PHPWord (e.g. using dompdf), otherwise the PDF does not contain any layout for table.

This commit is contained in:
Abubakkar Rangara 2018-07-24 13:59:16 +01:00
parent 2bfd82e229
commit e61c40e71d
2 changed files with 22 additions and 1 deletions

View File

@ -39,7 +39,9 @@ class Table extends AbstractElement
$rows = $this->element->getRows();
$rowCount = count($rows);
if ($rowCount > 0) {
$content .= '<table>' . PHP_EOL;
$tableStyle = $this->element->getStyle();
$tableLayout = $tableStyle === null ? '' : $tableStyle->getLayout();
$content .= '<table'. (empty($tableLayout) ? '' : ' style="table-layout: '.$tableLayout.'"') .'>'. PHP_EOL;
for ($i = 0; $i < $rowCount; $i++) {
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
$rowStyle = $rows[$i]->getStyle();

View File

@ -157,4 +157,23 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertTrue(strpos($content, $expected) !== false);
}
/**
* Tests writing table with layout
*/
public function testWriteTableLayout()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addTable();
$table = $section->addTable(array('layout' => 'fixed'));
$row1 = $table->addRow();
$row1->addCell()->addText('fixed layout table');
$dom = $this->getAsHTML($phpWord);
$xpath = new \DOMXPath($dom);
$this->assertEquals('table-layout: fixed', $xpath->query('/html/body/table')->item(0)->attributes->getNamedItem('style')->textContent);
}
}