Browse Source

Fix export PDF from HTML with /main/img paths - refs BT#13856

Angel Fernando Quiroz Campos 7 years ago
parent
commit
d9e1148e4d
1 changed files with 16 additions and 2 deletions
  1. 16 2
      main/inc/lib/pdf.lib.php

+ 16 - 2
main/inc/lib/pdf.lib.php

@@ -316,6 +316,7 @@ class PDF
                     if (!empty($elements)) {
                         foreach ($elements as $item) {
                             $old_src = $item->getAttribute('src');
+                            $old_src = trim($old_src);
 
                             if (strpos($old_src, $protocol) === false) {
                                 if (strpos($old_src, '/main/default_course_document') === false) {
@@ -352,9 +353,20 @@ class PDF
 
                                         $new_path = $document_path.$old_src_fixed;
                                     } else {
-                                        $new_path = $old_src;
+                                        $new_path = str_replace(
+                                            '/main/img/',
+                                            api_get_path(SYS_CODE_PATH).'img/',
+                                            $old_src
+                                        );
                                     }
-                                    $document_html = str_replace($old_src, $new_path, $document_html);
+                                    $item->setAttribute('src', $new_path);
+                                } else {
+                                    $new_path = str_replace(
+                                        '/main/default_course_document',
+                                        api_get_path(SYS_CODE_PATH).'default_course_document',
+                                        $old_src
+                                    );
+                                    $item->setAttribute('src', $new_path);
                                 }
                             } else {
                                 //Check if this is a complete URL
@@ -370,6 +382,8 @@ class PDF
                     }
                 }
 
+                $document_html = $doc->saveHTML();
+
                 api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
                 // TODO: Maybe it is better idea the title to be passed through
                 $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');