22
22
use PhpOffice \PhpWord \Element \Table ;
23
23
use PhpOffice \PhpWord \SimpleType \Jc ;
24
24
use PhpOffice \PhpWord \SimpleType \NumberFormat ;
25
+ use PhpOffice \PhpWord \Settings ;
25
26
26
27
/**
27
28
* Common Html functions
@@ -32,6 +33,7 @@ class Html
32
33
{
33
34
private static $ listIndex = 0 ;
34
35
private static $ xpath ;
36
+ private static $ options ;
35
37
36
38
/**
37
39
* Add HTML parts.
@@ -44,13 +46,17 @@ class Html
44
46
* @param string $html The code to parse
45
47
* @param bool $fullHTML If it's a full HTML, no need to add 'body' tag
46
48
* @param bool $preserveWhiteSpace If false, the whitespaces between nodes will be removed
49
+ * @param array $options:
50
+ * + IMG_SRC_SEARCH: optional to speed up images loading from remote url when files can be found locally
51
+ * + IMG_SRC_REPLACE: optional to speed up images loading from remote url when files can be found locally
47
52
*/
48
- public static function addHtml ($ element , $ html , $ fullHTML = false , $ preserveWhiteSpace = true )
53
+ public static function addHtml ($ element , $ html , $ fullHTML = false , $ preserveWhiteSpace = true , $ options = null )
49
54
{
50
55
/*
51
56
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
52
57
* which could be applied when such an element occurs in the parseNode function.
53
58
*/
59
+ self ::$ options = $ options ;
54
60
55
61
// Preprocess: remove all line ends, decode HTML entity,
56
62
// fix ampersand and angle brackets and add body tag for HTML fragments
@@ -141,6 +147,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
141
147
'sup ' => array ('Property ' , null , null , $ styles , null , 'superScript ' , true ),
142
148
'sub ' => array ('Property ' , null , null , $ styles , null , 'subScript ' , true ),
143
149
'span ' => array ('Span ' , $ node , null , $ styles , null , null , null ),
150
+ 'font ' => array ('Span ' , $ node , null , $ styles , null , null , null ),
144
151
'table ' => array ('Table ' , $ node , $ element , $ styles , null , null , null ),
145
152
'tr ' => array ('Row ' , $ node , $ element , $ styles , null , null , null ),
146
153
'td ' => array ('Cell ' , $ node , $ element , $ styles , null , null , null ),
@@ -296,8 +303,9 @@ private static function parseSpan($node, &$styles)
296
303
*
297
304
* @todo As soon as TableItem, RowItem and CellItem support relative width and height
298
305
*/
299
- private static function parseTable ($ node , $ element , &$ styles )
306
+ private static function parseTable ($ node , $ element , &$ styles )
300
307
{
308
+
301
309
$ elementStyles = self ::parseInlineStyle ($ node , $ styles ['table ' ]);
302
310
303
311
$ newElement = $ element ->addTable ($ elementStyles );
@@ -648,6 +656,45 @@ private static function parseImage($node, $element)
648
656
break ;
649
657
}
650
658
}
659
+ if ( strpos ( $ src , "data:image " ) !== false ){
660
+ if ( ! is_dir ( self ::$ imgdir ) )
661
+ mkdir ( self ::$ imgdir ) ;
662
+
663
+ $ match = array ();
664
+ preg_match ( '/data:image\/(\w+);base64,(.+)/ ' , $ src , $ match );
665
+
666
+ $ src = $ imgFile = self ::$ imgdir . uniqid () . ". " . $ match [1 ];
667
+
668
+ $ ifp = fopen ( $ imgFile , "wb " );
669
+
670
+ fwrite ($ ifp , base64_decode ( $ match [2 ] ) );
671
+ fclose ($ ifp );
672
+
673
+ }
674
+ $ src = urldecode ($ src );
675
+
676
+ if ( ! is_file ( $ src )
677
+ && !is_null (self ::$ options )
678
+ && isset (self ::$ options ['IMG_SRC_SEARCH ' ])
679
+ && isset (self ::$ options ['IMG_SRC_REPLACE ' ])){
680
+ $ src = str_replace ( self ::$ options ['IMG_SRC_SEARCH ' ], self ::$ options ['IMG_SRC_REPLACE ' ], $ src );
681
+ }
682
+
683
+ if (! is_file ($ src )){
684
+ if ($ imgBlob =file_get_contents ($ src )){
685
+ $ tmpDir = Settings::getTempDir ().'/ ' ;
686
+ if ( ! is_dir ( $ tmpDir ) )
687
+ mkdir ( $ tmpDir ) ;
688
+ $ match = array ();
689
+ preg_match ( '/.+\.(\w+)$/ ' , $ src , $ match );
690
+ $ src = $ tmpDir . uniqid () . ". " . $ match [1 ];
691
+
692
+ $ ifp = fopen ( $ src , "wb " );
693
+
694
+ fwrite ($ ifp , $ imgBlob );
695
+ fclose ($ ifp );
696
+ }
697
+ }
651
698
$ newElement = $ element ->addImage ($ src , $ style );
652
699
653
700
return $ newElement ;
0 commit comments