2020use PhpOffice \PhpWord \Element \AbstractContainer ;
2121use PhpOffice \PhpWord \Element \Row ;
2222use PhpOffice \PhpWord \Element \Table ;
23+ use PhpOffice \PhpWord \Settings ;
2324use PhpOffice \PhpWord \SimpleType \Jc ;
2425use PhpOffice \PhpWord \SimpleType \NumberFormat ;
2526
@@ -32,6 +33,7 @@ class Html
3233{
3334 private static $ listIndex = 0 ;
3435 private static $ xpath ;
36+ private static $ options ;
3537
3638 /**
3739 * Add HTML parts.
@@ -44,13 +46,17 @@ class Html
4446 * @param string $html The code to parse
4547 * @param bool $fullHTML If it's a full HTML, no need to add 'body' tag
4648 * @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
4752 */
48- public static function addHtml ($ element , $ html , $ fullHTML = false , $ preserveWhiteSpace = true )
53+ public static function addHtml ($ element , $ html , $ fullHTML = false , $ preserveWhiteSpace = true , $ options = null )
4954 {
5055 /*
5156 * @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
5257 * which could be applied when such an element occurs in the parseNode function.
5358 */
59+ self ::$ options = $ options ;
5460
5561 // Preprocess: remove all line ends, decode HTML entity,
5662 // 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 =
141147 'sup ' => array ('Property ' , null , null , $ styles , null , 'superScript ' , true ),
142148 'sub ' => array ('Property ' , null , null , $ styles , null , 'subScript ' , true ),
143149 'span ' => array ('Span ' , $ node , null , $ styles , null , null , null ),
150+ 'font ' => array ('Span ' , $ node , null , $ styles , null , null , null ),
144151 'table ' => array ('Table ' , $ node , $ element , $ styles , null , null , null ),
145152 'tr ' => array ('Row ' , $ node , $ element , $ styles , null , null , null ),
146153 'td ' => array ('Cell ' , $ node , $ element , $ styles , null , null , null ),
@@ -648,7 +655,52 @@ private static function parseImage($node, $element)
648655 break ;
649656 }
650657 }
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+ }
652704
653705 return $ newElement ;
654706 }
0 commit comments