22
22
use PhpOffice \PhpWord \Element \TextBreak ;
23
23
use PhpOffice \PhpWord \Element \TextRun ;
24
24
use PhpOffice \PhpWord \Element \Title ;
25
+ use PhpOffice \PhpWord \Endnotes ;
25
26
use PhpOffice \PhpWord \Exception \Exception ;
27
+ use PhpOffice \PhpWord \Footnotes ;
26
28
use PhpOffice \PhpWord \PhpWord ;
27
29
use PhpOffice \PhpWord \Style ;
28
30
use PhpOffice \PhpWord \Style \Font ;
@@ -42,6 +44,13 @@ class HTML extends AbstractWriter implements WriterInterface
42
44
*/
43
45
protected $ isPdf = false ;
44
46
47
+ /**
48
+ * Footnotes and endnotes collection
49
+ *
50
+ * @var array
51
+ */
52
+ protected $ notes = array ();
53
+
45
54
/**
46
55
* Create new instance
47
56
*/
@@ -85,6 +94,7 @@ public function writeDocument()
85
94
$ html .= '</head> ' . PHP_EOL ;
86
95
$ html .= '<body> ' . PHP_EOL ;
87
96
$ html .= $ this ->writeHTMLBody ();
97
+ $ html .= $ this ->writeNotes ();
88
98
$ html .= '</body> ' . PHP_EOL ;
89
99
$ html .= '</html> ' . PHP_EOL ;
90
100
@@ -181,6 +191,32 @@ private function writeHTMLBody()
181
191
return $ html ;
182
192
}
183
193
194
+ /**
195
+ * Write footnote/endnote contents
196
+ */
197
+ private function writeNotes ()
198
+ {
199
+ $ footnote = Footnotes::getElements ();
200
+ $ endnote = Endnotes::getElements ();
201
+ $ html = '' ;
202
+
203
+ if (count ($ this ->notes ) > 0 ) {
204
+ $ html .= "<hr /> " ;
205
+ foreach ($ this ->notes as $ noteId => $ noteMark ) {
206
+ $ noteAnchor = "note- {$ noteId }" ;
207
+ list ($ noteType , $ noteTypeId ) = explode ('- ' , $ noteMark );
208
+ $ collection = $ $ noteType ;
209
+ if (array_key_exists ($ noteTypeId , $ collection )) {
210
+ $ element = $ collection [$ noteTypeId ];
211
+ $ content = "<a href= \"# {$ noteMark }\" class= \"NoteRef \"><sup> {$ noteId }</sup></a> " . $ this ->writeTextRun ($ element , true );
212
+ $ html .= "<p><a name= \"{$ noteAnchor }\" /> {$ content }</p> " . PHP_EOL ;
213
+ }
214
+ }
215
+ }
216
+
217
+ return $ html ;
218
+ }
219
+
184
220
/**
185
221
* Get text
186
222
*
@@ -226,19 +262,20 @@ private function writeText($text, $withoutP = false)
226
262
}
227
263
228
264
/**
229
- * Get text run content
265
+ * Write text run content
230
266
*
231
- * @param TextRun $textrun
267
+ * @param TextRun|Footnote|Endnote $textrun
232
268
* @return string
233
269
*/
234
- private function writeTextRun ($ textrun )
270
+ private function writeTextRun ($ textrun, $ withoutP = false )
235
271
{
236
272
$ html = '' ;
237
273
$ elements = $ textrun ->getElements ();
238
274
if (count ($ elements ) > 0 ) {
239
275
$ paragraphStyle = $ textrun ->getParagraphStyle ();
240
276
$ spIsObject = ($ paragraphStyle instanceof Paragraph);
241
- $ html .= '<p ' ;
277
+
278
+ $ html .= $ withoutP ? '<span ' : '<p ' ;
242
279
if ($ paragraphStyle ) {
243
280
if (!$ spIsObject ) {
244
281
$ html .= ' class=" ' . $ paragraphStyle . '" ' ;
@@ -262,7 +299,8 @@ private function writeTextRun($textrun)
262
299
$ html .= $ this ->writeFootnote ($ element );
263
300
}
264
301
}
265
- $ html .= '</p> ' . PHP_EOL ;
302
+ $ html .= $ withoutP ? '</span> ' : '</p> ' ;
303
+ $ html .= PHP_EOL ;
266
304
}
267
305
268
306
return $ html ;
@@ -279,11 +317,14 @@ private function writeLink($element, $withoutP = false)
279
317
{
280
318
$ url = $ element ->getLinkSrc ();
281
319
$ text = $ element ->getLinkName ();
320
+ if ($ text == '' ) {
321
+ $ text = $ url ;
322
+ }
282
323
$ html = '' ;
283
324
if (!$ withoutP ) {
284
325
$ html .= "<p> " . PHP_EOL ;
285
326
}
286
- $ html .= "<a href= \"{$ url }' \"> {$ text }</a> " . PHP_EOL ;
327
+ $ html .= "<a href= \"{$ url }\"> {$ text }</a> " . PHP_EOL ;
287
328
if (!$ withoutP ) {
288
329
$ html .= "</p> " . PHP_EOL ;
289
330
}
@@ -467,7 +508,7 @@ private function writeObject($element, $withoutP = false)
467
508
*/
468
509
private function writeFootnote ($ element )
469
510
{
470
- return $ this ->writeUnsupportedElement ($ element, true );
511
+ return $ this ->writeNote ($ element );
471
512
}
472
513
473
514
/**
@@ -478,7 +519,25 @@ private function writeFootnote($element)
478
519
*/
479
520
private function writeEndnote ($ element )
480
521
{
481
- return $ this ->writeUnsupportedElement ($ element , true );
522
+ return $ this ->writeNote ($ element );
523
+ }
524
+
525
+ /**
526
+ * Write footnote/endnote marks
527
+ *
528
+ * @param Footnote|Endnote $element
529
+ * @return string
530
+ */
531
+ private function writeNote ($ element )
532
+ {
533
+ $ index = count ($ this ->notes ) + 1 ;
534
+ $ prefix = ($ element instanceof Endnote) ? 'endnote ' : 'footnote ' ;
535
+ $ noteMark = $ prefix . '- ' . $ element ->getRelationId ();
536
+ $ noteAnchor = "note- {$ index }" ;
537
+ $ this ->notes [$ index ] = $ noteMark ;
538
+ $ html = "<a name= \"{$ noteMark }\"><a href= \"# {$ noteAnchor }\" class= \"NoteRef \"><sup> {$ index }</sup></a> " ;
539
+
540
+ return $ html ;
482
541
}
483
542
484
543
/**
@@ -509,18 +568,33 @@ private function writeUnsupportedElement($element, $withoutP = false)
509
568
*/
510
569
private function writeStyles ()
511
570
{
512
- $ bodyCss = array ();
513
571
$ css = '<style> ' . PHP_EOL ;
514
572
515
573
// Default styles
516
- $ bodyCss ['font-family ' ] = "' " . $ this ->getPhpWord ()->getDefaultFontName () . "' " ;
517
- $ bodyCss ['font-size ' ] = $ this ->getPhpWord ()->getDefaultFontSize () . 'pt ' ;
518
- $ css .= '* ' . $ this ->assembleCss ($ bodyCss , true ) . PHP_EOL ;
574
+ $ defaultStyles = array (
575
+ '* ' => array (
576
+ 'font-family ' => $ this ->getPhpWord ()->getDefaultFontName (),
577
+ 'font-size ' => $ this ->getPhpWord ()->getDefaultFontSize () . 'pt ' ,
578
+ ),
579
+ 'a.NoteRef ' => array (
580
+ 'text-decoration ' => 'none ' ,
581
+ ),
582
+ 'hr ' => array (
583
+ 'height ' => '1px ' ,
584
+ 'padding ' => '0 ' ,
585
+ 'margin ' => '1em 0 ' ,
586
+ 'border ' => '0 ' ,
587
+ 'border-top ' => '1px solid #CCC ' ,
588
+ ),
589
+ );
590
+ foreach ($ defaultStyles as $ selector => $ style ) {
591
+ $ css .= $ selector . ' ' . $ this ->assembleCss ($ style , true ) . PHP_EOL ;
592
+ }
519
593
520
594
// Custom styles
521
- $ styles = Style::getStyles ();
522
- if (is_array ($ styles )) {
523
- foreach ($ styles as $ name => $ style ) {
595
+ $ customStyles = Style::getStyles ();
596
+ if (is_array ($ customStyles )) {
597
+ foreach ($ customStyles as $ name => $ style ) {
524
598
if ($ style instanceof Font) {
525
599
if ($ style ->getStyleType () == 'title ' ) {
526
600
$ name = str_replace ('Heading_ ' , 'h ' , $ name );
0 commit comments