Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
### Fixed
- Fix regex in `cloneBlock` function @nicoder #1269
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
- Adding table layout to the generated HTML @aarangara #1441
- Fix loading of Sharepoint document @Garrcomm #1498
- RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493
- Fix parsing of Office 365 documents @Timanx #1485
Expand Down
24 changes: 23 additions & 1 deletion src/PhpWord/Writer/HTML/Element/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function write()
$rows = $this->element->getRows();
$rowCount = count($rows);
if ($rowCount > 0) {
$content .= '<table>' . PHP_EOL;
$content .= '<table' . self::getTableStyle($this->element->getStyle()) . '>' . PHP_EOL;

for ($i = 0; $i < $rowCount; $i++) {
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
$rowStyle = $rows[$i]->getStyle();
Expand Down Expand Up @@ -102,4 +103,25 @@ public function write()

return $content;
}

/**
* Translates Table style in CSS equivalent
*
* @param \PhpOffice\PhpWord\Style\Table|null $tableStyle
* @return string
*/
private function getTableStyle(\PhpOffice\PhpWord\Style\Table $tableStyle = null)
{
if ($tableStyle == null) {
return '';
}
$style = ' style="';
if ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED) {
$style .= 'table-layout: fixed;';
} elseif ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO) {
$style .= 'table-layout: auto;';
}

return $style . '"';
}
}
24 changes: 24 additions & 0 deletions tests/PhpWord/Writer/HTML/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,28 @@ public function testWriteTitleTextRun()

$this->assertTrue(strpos($content, $expected) !== false);
}

/**
* Tests writing table with layout
*/
public function testWriteTableLayout()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addTable();

$table1 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED));
$row1 = $table1->addRow();
$row1->addCell()->addText('fixed layout table');

$table2 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO));
$row2 = $table2->addRow();
$row2->addCell()->addText('auto layout table');

$dom = $this->getAsHTML($phpWord);
$xpath = new \DOMXPath($dom);

$this->assertEquals('table-layout: fixed;', $xpath->query('/html/body/table[1]')->item(0)->attributes->getNamedItem('style')->textContent);
$this->assertEquals('table-layout: auto;', $xpath->query('/html/body/table[2]')->item(0)->attributes->getNamedItem('style')->textContent);
}
}