23
23
use PhpOffice \PhpWord \Style \Font ;
24
24
use PhpOffice \PhpWord \Style \Paragraph ;
25
25
use PhpOffice \PhpWord \Style ;
26
- use PhpOffice \PhpWord \Writer \HTML \Element \Element as ElementWriter ;
26
+ use PhpOffice \PhpWord \Writer \HTML \Element \Container ;
27
27
use PhpOffice \PhpWord \Writer \HTML \Element \TextRun as TextRunWriter ;
28
28
use PhpOffice \PhpWord \Writer \HTML \Style \Font as FontStyleWriter ;
29
29
use PhpOffice \PhpWord \Writer \HTML \Style \Generic as GenericStyleWriter ;
@@ -89,28 +89,28 @@ public function save($filename = null)
89
89
*/
90
90
public function writeDocument ()
91
91
{
92
- $ html = '' ;
93
- $ html .= '<!DOCTYPE html> ' . PHP_EOL ;
94
- $ html .= '<!-- Generated by PHPWord --> ' . PHP_EOL ;
95
- $ html .= '<html> ' . PHP_EOL ;
96
- $ html .= '<head> ' . PHP_EOL ;
97
- $ html .= $ this ->writeHTMLHead ();
98
- $ html .= '</head> ' . PHP_EOL ;
99
- $ html .= '<body> ' . PHP_EOL ;
100
- $ html .= $ this ->writeHTMLBody ();
101
- $ html .= $ this ->writeNotes ();
102
- $ html .= '</body> ' . PHP_EOL ;
103
- $ html .= '</html> ' . PHP_EOL ;
92
+ $ content = '' ;
93
+ $ content .= '<!DOCTYPE html> ' . PHP_EOL ;
94
+ $ content .= '<!-- Generated by PHPWord --> ' . PHP_EOL ;
95
+ $ content .= '<html> ' . PHP_EOL ;
96
+ $ content .= '<head> ' . PHP_EOL ;
97
+ $ content .= $ this ->writeHead ();
98
+ $ content .= '</head> ' . PHP_EOL ;
99
+ $ content .= '<body> ' . PHP_EOL ;
100
+ $ content .= $ this ->writeBody ();
101
+ $ content .= $ this ->writeNotes ();
102
+ $ content .= '</body> ' . PHP_EOL ;
103
+ $ content .= '</html> ' . PHP_EOL ;
104
104
105
- return $ html ;
105
+ return $ content ;
106
106
}
107
107
108
108
/**
109
109
* Generate HTML header
110
110
*
111
111
* @return string
112
112
*/
113
- private function writeHTMLHead ()
113
+ private function writeHead ()
114
114
{
115
115
$ properties = $ this ->getPhpWord ()->getDocumentProperties ();
116
116
$ propertiesMapping = array (
@@ -126,76 +126,43 @@ private function writeHTMLHead()
126
126
$ title = $ properties ->getTitle ();
127
127
$ title = ($ title != '' ) ? $ title : 'PHPWord ' ;
128
128
129
- $ html = '' ;
130
- $ html .= '<meta charset="UTF-8" /> ' . PHP_EOL ;
131
- $ html .= '<title> ' . htmlspecialchars ($ title ) . '</title> ' . PHP_EOL ;
129
+ $ content = '' ;
130
+ $ content .= '<meta charset="UTF-8" /> ' . PHP_EOL ;
131
+ $ content .= '<title> ' . htmlspecialchars ($ title ) . '</title> ' . PHP_EOL ;
132
132
foreach ($ propertiesMapping as $ key => $ value ) {
133
133
$ value = ($ value == '' ) ? $ key : $ value ;
134
134
$ method = "get " . $ key ;
135
135
if ($ properties ->$ method () != '' ) {
136
- $ html .= '<meta name=" ' . $ value . '" content=" ' .
136
+ $ content .= '<meta name=" ' . $ value . '" content=" ' .
137
137
htmlspecialchars ($ properties ->$ method ()) . '" /> ' . PHP_EOL ;
138
138
}
139
139
}
140
- $ html .= $ this ->writeStyles ();
140
+ $ content .= $ this ->writeStyles ();
141
141
142
- return $ html ;
142
+ return $ content ;
143
143
}
144
144
145
145
/**
146
146
* Get content
147
147
*
148
148
* @return string
149
149
*/
150
- private function writeHTMLBody ()
150
+ private function writeBody ()
151
151
{
152
152
$ phpWord = $ this ->getPhpWord ();
153
- $ html = '' ;
153
+ $ content = '' ;
154
154
155
155
$ sections = $ phpWord ->getSections ();
156
156
$ countSections = count ($ sections );
157
157
158
158
if ($ countSections > 0 ) {
159
159
foreach ($ sections as $ section ) {
160
- $ elements = $ section ->getElements ();
161
- foreach ($ elements as $ element ) {
162
- if ($ element instanceof AbstractElement) {
163
- $ elementWriter = new ElementWriter ($ this , $ element , false );
164
- $ html .= $ elementWriter ->write ();
165
- }
166
- }
167
- }
168
- }
169
-
170
- return $ html ;
171
- }
172
-
173
- /**
174
- * Write footnote/endnote contents as textruns
175
- */
176
- private function writeNotes ()
177
- {
178
- $ phpWord = $ this ->getPhpWord ();
179
- $ html = '' ;
180
-
181
- if (!empty ($ this ->notes )) {
182
- $ html .= "<hr /> " ;
183
- foreach ($ this ->notes as $ noteId => $ noteMark ) {
184
- $ noteAnchor = "note- {$ noteId }" ;
185
- list ($ noteType , $ noteTypeId ) = explode ('- ' , $ noteMark );
186
- $ method = 'get ' . ($ noteType == 'endnote ' ? 'Endnotes ' : 'Footnotes ' );
187
- $ collection = $ phpWord ->$ method ()->getItems ();
188
- if (array_key_exists ($ noteTypeId , $ collection )) {
189
- $ element = $ collection [$ noteTypeId ];
190
- $ elmWriter = new TextRunWriter ($ this , $ element , true );
191
- $ content = "<a href= \"# {$ noteMark }\" class= \"NoteRef \"><sup> {$ noteId }</sup></a> " ;
192
- $ content .= $ elmWriter ->write ();
193
- $ html .= "<p><a name= \"{$ noteAnchor }\" /> {$ content }</p> " . PHP_EOL ;
194
- }
160
+ $ writer = new Container ($ this , $ section );
161
+ $ content .= $ writer ->write ();
195
162
}
196
163
}
197
164
198
- return $ html ;
165
+ return $ content ;
199
166
}
200
167
201
168
/**
@@ -253,6 +220,36 @@ private function writeStyles()
253
220
return $ css ;
254
221
}
255
222
223
+ /**
224
+ * Write footnote/endnote contents as textruns
225
+ */
226
+ private function writeNotes ()
227
+ {
228
+ $ phpWord = $ this ->getPhpWord ();
229
+ $ content = PHP_EOL ;
230
+
231
+ if (!empty ($ this ->notes )) {
232
+ $ content .= "<hr /> " . PHP_EOL ;
233
+ foreach ($ this ->notes as $ noteId => $ noteMark ) {
234
+ list ($ noteType , $ noteTypeId ) = explode ('- ' , $ noteMark );
235
+ $ method = 'get ' . ($ noteType == 'endnote ' ? 'Endnotes ' : 'Footnotes ' );
236
+ $ collection = $ phpWord ->$ method ()->getItems ();
237
+
238
+ if (array_key_exists ($ noteTypeId , $ collection )) {
239
+ $ element = $ collection [$ noteTypeId ];
240
+ $ noteAnchor = "<a name= \"note- {$ noteId }\" /> " ;
241
+ $ noteAnchor .= "<a href= \"# {$ noteMark }\" class= \"NoteRef \"><sup> {$ noteId }</sup></a> " ;
242
+
243
+ $ writer = new TextRunWriter ($ this , $ element );
244
+ $ writer ->setOpeningText ($ noteAnchor );
245
+ $ content .= $ writer ->write ();
246
+ }
247
+ }
248
+ }
249
+
250
+ return $ content ;
251
+ }
252
+
256
253
/**
257
254
* Get is PDF
258
255
*
0 commit comments