Skip to content

Commit 9dd5e0c

Browse files
authored
Merge pull request #1441 from aarangara/add_html_table_layout
Adding table layout to the generated HTML
2 parents 7aef21f + f472bfb commit 9dd5e0c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
1010
### Fixed
1111
- Fix regex in `cloneBlock` function @nicoder #1269
1212
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
13+
- Adding table layout to the generated HTML @aarangara #1441
1314
- Fix loading of Sharepoint document @Garrcomm #1498
1415
- RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493
1516
- Fix parsing of Office 365 documents @Timanx #1485

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public function write()
3939
$rows = $this->element->getRows();
4040
$rowCount = count($rows);
4141
if ($rowCount > 0) {
42-
$content .= '<table>' . PHP_EOL;
42+
$content .= '<table' . self::getTableStyle($this->element->getStyle()) . '>' . PHP_EOL;
43+
4344
for ($i = 0; $i < $rowCount; $i++) {
4445
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
4546
$rowStyle = $rows[$i]->getStyle();
@@ -102,4 +103,25 @@ public function write()
102103

103104
return $content;
104105
}
106+
107+
/**
108+
* Translates Table style in CSS equivalent
109+
*
110+
* @param \PhpOffice\PhpWord\Style\Table|null $tableStyle
111+
* @return string
112+
*/
113+
private function getTableStyle(\PhpOffice\PhpWord\Style\Table $tableStyle = null)
114+
{
115+
if ($tableStyle == null) {
116+
return '';
117+
}
118+
$style = ' style="';
119+
if ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED) {
120+
$style .= 'table-layout: fixed;';
121+
} elseif ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO) {
122+
$style .= 'table-layout: auto;';
123+
}
124+
125+
return $style . '"';
126+
}
105127
}

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,28 @@ 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+
170+
$table1 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED));
171+
$row1 = $table1->addRow();
172+
$row1->addCell()->addText('fixed layout table');
173+
174+
$table2 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO));
175+
$row2 = $table2->addRow();
176+
$row2->addCell()->addText('auto layout table');
177+
178+
$dom = $this->getAsHTML($phpWord);
179+
$xpath = new \DOMXPath($dom);
180+
181+
$this->assertEquals('table-layout: fixed;', $xpath->query('/html/body/table[1]')->item(0)->attributes->getNamedItem('style')->textContent);
182+
$this->assertEquals('table-layout: auto;', $xpath->query('/html/body/table[2]')->item(0)->attributes->getNamedItem('style')->textContent);
183+
}
160184
}

0 commit comments

Comments
 (0)