20
20
use PhpOffice \PhpWord \Element \AbstractContainer ;
21
21
use PhpOffice \PhpWord \Element \Row ;
22
22
use PhpOffice \PhpWord \Element \Table ;
23
+ use PhpOffice \PhpWord \Settings ;
23
24
use PhpOffice \PhpWord \SimpleType \Jc ;
24
25
use PhpOffice \PhpWord \SimpleType \NumberFormat ;
25
26
@@ -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 ),
@@ -648,7 +655,52 @@ private static function parseImage($node, $element)
648
655
break ;
649
656
}
650
657
}
651
- $ newElement = $ element ->addImage ($ src , $ style );
658
+ $ originSrc = $ src ;
659
+ if (strpos ($ src , 'data:image ' ) !== false ) {
660
+ $ tmpDir = Settings::getTempDir () . '/ ' ;
661
+
662
+ $ match = array ();
663
+ preg_match ('/data:image\/(\w+);base64,(.+)/ ' , $ src , $ match );
664
+
665
+ $ src = $ imgFile = $ tmpDir . uniqid () . '. ' . $ match [1 ];
666
+
667
+ $ ifp = fopen ($ imgFile , 'wb ' );
668
+
669
+ if ($ ifp !== false ) {
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
+ $ match = array ();
687
+ preg_match ('/.+\.(\w+)$/ ' , $ src , $ match );
688
+ $ src = $ tmpDir . uniqid () . '. ' . $ match [1 ];
689
+
690
+ $ ifp = fopen ($ src , 'wb ' );
691
+
692
+ if ($ ifp !== false ) {
693
+ fwrite ($ ifp , $ imgBlob );
694
+ fclose ($ ifp );
695
+ }
696
+ }
697
+ }
698
+
699
+ if (is_file ($ src )) {
700
+ $ newElement = $ element ->addImage ($ src , $ style );
701
+ } else {
702
+ throw new \Exception ("Could not load image $ originSrc " );
703
+ }
652
704
653
705
return $ newElement ;
654
706
}
0 commit comments