Skip to content

Commit e61c40e

Browse files
author
Abubakkar Rangara
committed
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.
1 parent 2bfd82e commit e61c40e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/PhpWord/Writer/HTML/Element/Table.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public function write()
3939
$rows = $this->element->getRows();
4040
$rowCount = count($rows);
4141
if ($rowCount > 0) {
42-
$content .= '<table>' . PHP_EOL;
42+
$tableStyle = $this->element->getStyle();
43+
$tableLayout = $tableStyle === null ? '' : $tableStyle->getLayout();
44+
$content .= '<table'. (empty($tableLayout) ? '' : ' style="table-layout: '.$tableLayout.'"') .'>'. PHP_EOL;
4345
for ($i = 0; $i < $rowCount; $i++) {
4446
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
4547
$rowStyle = $rows[$i]->getStyle();

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,23 @@ public function testWriteTitleTextRun()
157157

158158
$this->assertTrue(strpos($content, $expected) !== false);
159159
}
160+
161+
/**
162+
* Tests writing table with layout
163+
*/
164+
public function testWriteTableLayout()
165+
{
166+
$phpWord = new PhpWord();
167+
$section = $phpWord->addSection();
168+
$section->addTable();
169+
$table = $section->addTable(array('layout' => 'fixed'));
170+
171+
$row1 = $table->addRow();
172+
$row1->addCell()->addText('fixed layout table');
173+
174+
$dom = $this->getAsHTML($phpWord);
175+
$xpath = new \DOMXPath($dom);
176+
177+
$this->assertEquals('table-layout: fixed', $xpath->query('/html/body/table')->item(0)->attributes->getNamedItem('style')->textContent);
178+
}
160179
}

0 commit comments

Comments
 (0)