File tree Expand file tree Collapse file tree 3 files changed +48
-1
lines changed
src/PhpWord/Writer/HTML/Element
tests/PhpWord/Writer/HTML Expand file tree Collapse file tree 3 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
10
10
### Fixed
11
11
- Fix regex in ` cloneBlock ` function @nicoder #1269
12
12
- 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
13
14
- Fix loading of Sharepoint document @Garrcomm #1498
14
15
- RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493
15
16
- Fix parsing of Office 365 documents @Timanx #1485
Original file line number Diff line number Diff line change @@ -39,7 +39,8 @@ public function write()
39
39
$ rows = $ this ->element ->getRows ();
40
40
$ rowCount = count ($ rows );
41
41
if ($ rowCount > 0 ) {
42
- $ content .= '<table> ' . PHP_EOL ;
42
+ $ content .= '<table ' . self ::getTableStyle ($ this ->element ->getStyle ()) . '> ' . PHP_EOL ;
43
+
43
44
for ($ i = 0 ; $ i < $ rowCount ; $ i ++) {
44
45
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
45
46
$ rowStyle = $ rows [$ i ]->getStyle ();
@@ -102,4 +103,25 @@ public function write()
102
103
103
104
return $ content ;
104
105
}
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
+ }
105
127
}
Original file line number Diff line number Diff line change @@ -157,4 +157,28 @@ public function testWriteTitleTextRun()
157
157
158
158
$ this ->assertTrue (strpos ($ content , $ expected ) !== false );
159
159
}
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
+ }
160
184
}
You can’t perform that action at this time.
0 commit comments