From 12b2f0bd4f446a538a0421a98334859a2bd9d513 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:49:57 -0500 Subject: [PATCH] Internal: mPdf load css files in header - refs BT#21621 --- public/main/exercise/result.php | 3 +- .../main/gradebook/lib/fe/exportgradebook.php | 9 +-- public/main/inc/lib/exercise.lib.php | 3 +- public/main/inc/lib/glossary.lib.php | 2 +- public/main/inc/lib/pdf.lib.php | 60 ++++++++++--------- public/main/lp/learnpath.class.php | 3 +- .../main/my_space/access_details_session.php | 4 +- public/main/my_space/myStudents.php | 2 +- public/main/my_space/session.php | 2 +- public/plugin/buycourses/src/invoice.php | 2 +- .../src/export_pdf_all_in_one.php | 6 +- .../src/print_certificate.php | 6 +- 12 files changed, 51 insertions(+), 51 deletions(-) diff --git a/public/main/exercise/result.php b/public/main/exercise/result.php index f0a3ba34f8..87d965d7b0 100644 --- a/public/main/exercise/result.php +++ b/public/main/exercise/result.php @@ -120,7 +120,6 @@ switch ($action) { if ($allowExportPdf) { $allAnswers = $stats['all_answers_html']; @$pdf = new PDF(); - $cssFile = api_get_path(SYS_CSS_PATH).'themes/chamilo/default.css'; $title = get_lang('ResponseReport'); $exerciseTitle = $objExercise->get_formated_title(); $studentInfo = api_get_user_info($student_id); @@ -138,7 +137,7 @@ switch ($action) { $userHeader $allAnswers ", - file_get_contents($cssFile), + null, $filename, api_get_course_id(), 'D', diff --git a/public/main/gradebook/lib/fe/exportgradebook.php b/public/main/gradebook/lib/fe/exportgradebook.php index 51acf9724d..0bd7faeafe 100644 --- a/public/main/gradebook/lib/fe/exportgradebook.php +++ b/public/main/gradebook/lib/fe/exportgradebook.php @@ -2,6 +2,8 @@ /* For licensing terms, see /license.txt */ +use Chamilo\CoreBundle\Framework\Container; + /** * Prints an HTML page with a table containing the gradebook data. * @@ -126,12 +128,7 @@ function export_pdf_with_html($headers_table, $data_table, $headers_pdf, $footer $footer .= '
{PAGENO}/{nb}
'; // preparing content pdf - $css_file = api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/print.css'; - if (file_exists($css_file)) { - $css = @file_get_contents($css_file); - } else { - $css = ''; - } + $css = Container::getThemeHelper()->getAssetContents('print.css'); $items_per_page = 30; $count_pages = ceil(count($data_table) / $items_per_page); $content_table = ''; diff --git a/public/main/inc/lib/exercise.lib.php b/public/main/inc/lib/exercise.lib.php index 9f6eb740ac..c6ec72353b 100644 --- a/public/main/inc/lib/exercise.lib.php +++ b/public/main/inc/lib/exercise.lib.php @@ -5931,10 +5931,9 @@ EOT; @$pdf = new PDF(); $filename = get_lang('Exercise'); - $cssFile = api_get_path(SYS_CSS_PATH).'themes/chamilo/default.css'; $pdfPath = @$pdf->content_to_pdf( "$pdfContent", - file_get_contents($cssFile), + null, $filename, api_get_course_id(), 'F', diff --git a/public/main/inc/lib/glossary.lib.php b/public/main/inc/lib/glossary.lib.php index 65263ab8fe..c3aca30f1e 100644 --- a/public/main/inc/lib/glossary.lib.php +++ b/public/main/inc/lib/glossary.lib.php @@ -952,7 +952,7 @@ class GlossaryManager $html = $template->fetch($layout); $courseCode = api_get_course_id(); $pdf = new PDF(); - $pdf->content_to_pdf($html, '', get_lang('Glossary').'_'.$courseCode, $courseCode); + $pdf->content_to_pdf($html, null, get_lang('Glossary').'_'.$courseCode, $courseCode); } /** diff --git a/public/main/inc/lib/pdf.lib.php b/public/main/inc/lib/pdf.lib.php index 20827a3dc7..ff76b5c3cb 100644 --- a/public/main/inc/lib/pdf.lib.php +++ b/public/main/inc/lib/pdf.lib.php @@ -4,8 +4,8 @@ use Chamilo\CoreBundle\Component\Utils\ChamiloApi; use Chamilo\CoreBundle\Framework\Container; -use Chamilo\CoreBundle\ServiceHelper\ThemeHelper; use Masterminds\HTML5; +use Mpdf\HTMLParserMode; use Mpdf\Mpdf; use Mpdf\Output\Destination; @@ -158,11 +158,7 @@ class PDF return $html; } - $css_file = api_get_path(SYS_CSS_PATH).'themes/'.$tpl->theme.'/print.css'; - if (!file_exists($css_file)) { - $css_file = api_get_path(SYS_CSS_PATH).'print.css'; - } - $css = file_get_contents($css_file); + $css = Container::getThemeHelper()->getAssetContents('print.css'); self::content_to_pdf( $html, @@ -377,7 +373,7 @@ class PDF */ public function content_to_pdf( $document_html, - $css = '', + ?string $css = null, $pdf_name = '', $courseCode = null, $outputMode = 'D', @@ -406,9 +402,6 @@ class PDF self::format_pdf($courseInfo, $completeHeader, $disablePagination); $document_html = preg_replace($clean_search, '', $document_html); - //absolute path for frames.css //TODO: necessary? - $absolute_css_path = api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/frames.css'; - $document_html = str_replace('href="./css/frames.css"', 'href="'.$absolute_css_path.'"', $document_html); $document_html = str_replace('../../', '', $document_html); $document_html = str_replace('../', '', $document_html); $document_html = str_replace( @@ -417,10 +410,28 @@ class PDF $document_html ); - if (!empty($courseInfo['path'])) { - $doc = new DOMDocument(); - @$doc->loadHTML($document_html); + $basicStyles = []; + + $doc = new DOMDocument(); + @$doc->loadHTML($document_html); + + $linksToRemove = []; + + foreach ($doc->getElementsByTagName('link') as $link) { + if ($link->getAttribute('href') === './css/frames.css') { + $linksToRemove[] = $link; + } + } + + foreach ($linksToRemove as $link) { + $link->parentNode->removeChild($link); + } + $basicStyles[] = Container::getThemeHelper()->getAssetContents('frames.css'); + + $document_html = $doc->saveHTML(); + + if (!empty($courseInfo['path'])) { //Fixing only images @todo do the same thing with other elements $elements = $doc->getElementsByTagName('img'); $protocol = api_get_protocol(); @@ -474,17 +485,16 @@ class PDF } if (!empty($css)) { - $this->pdf->WriteHTML($css, 1); + $this->pdf->WriteHTML($css, HTMLParserMode::HEADER_CSS); } if ($addDefaultCss) { - $themeHelper = Container::$container->get(ThemeHelper::class); - $basicStyles = [ - $themeHelper->getThemeAssetUrl('default.css'), - ]; - foreach ($basicStyles as $style) { - $cssContent = file_get_contents($style); - @$this->pdf->WriteHTML($cssContent, 1); + $basicStyles[] = Container::getThemeHelper()->getAssetContents('default.css'); + } + + foreach ($basicStyles as $cssContent) { + if ($cssContent) { + @$this->pdf->WriteHTML($cssContent, HTMLParserMode::HEADER_CSS); } } @@ -807,15 +817,11 @@ class PDF { $this->template = $this->template ?: new Template('', false, false, false, false, false, false); - $cssFile = api_get_path(SYS_CSS_PATH).'themes/'.$this->template->theme.'/print.css'; - - if (!file_exists($cssFile)) { - $cssFile = api_get_path(SYS_CSS_PATH).'print.css'; - } + $css = Container::getThemeHelper()->getAssetContents('print.css'); $pdfPath = self::content_to_pdf( $html, - file_get_contents($cssFile), + $css, $fileName, $this->params['course_code'], 'F' diff --git a/public/main/lp/learnpath.class.php b/public/main/lp/learnpath.class.php index c5eb2f6218..22d56e8e87 100644 --- a/public/main/lp/learnpath.class.php +++ b/public/main/lp/learnpath.class.php @@ -8444,8 +8444,7 @@ class learnpath return ''; } - $themeHelper = Container::$container->get(ThemeHelper::class); - $path = $themeHelper->getThemeAssetUrl("lp_icons/$icon"); + $path = Container::getThemeHelper()->getThemeAssetUrl("lp_icons/$icon"); return Display::img($path); } diff --git a/public/main/my_space/access_details_session.php b/public/main/my_space/access_details_session.php index 4a8477a723..07b0ca85cc 100644 --- a/public/main/my_space/access_details_session.php +++ b/public/main/my_space/access_details_session.php @@ -378,7 +378,7 @@ if ($form->validate()) { @$pdf->setBackground($tpl->theme); @$pdf->content_to_pdf( $content, - '', + null, $pdfName, null, 'D', @@ -583,7 +583,7 @@ if ($formByDay->validate()) { @$pdf->setBackground($tpl->theme, true); @$pdf->content_to_pdf( $content, - '', + null, $pdfName, null, 'D', diff --git a/public/main/my_space/myStudents.php b/public/main/my_space/myStudents.php index 913cb733ce..77bf09851c 100644 --- a/public/main/my_space/myStudents.php +++ b/public/main/my_space/myStudents.php @@ -426,7 +426,7 @@ switch ($action) { $pdf->setBackground($tpl->theme); @$pdf->content_to_pdf( $content, - '', + null, '', null, 'D', diff --git a/public/main/my_space/session.php b/public/main/my_space/session.php index e669a5a80d..7eb4b78d9c 100644 --- a/public/main/my_space/session.php +++ b/public/main/my_space/session.php @@ -267,7 +267,7 @@ switch ($action) { $pdf->set_footer(); $result = @$pdf->content_to_pdf( $content, - '', + null, $pdfName, null, $mode, diff --git a/public/plugin/buycourses/src/invoice.php b/public/plugin/buycourses/src/invoice.php index bc52b98657..f45c7d6f49 100644 --- a/public/plugin/buycourses/src/invoice.php +++ b/public/plugin/buycourses/src/invoice.php @@ -149,5 +149,5 @@ $params = [ 'orientation' => 'P', ]; $pdf = new PDF($params['format'], $params['orientation'], $params); -@$pdf->content_to_pdf($htmlText, '', $fileName, null, 'D', false, null, false, false, false); +@$pdf->content_to_pdf($htmlText, null, $fileName, null, 'D', false, null, false, false, false); exit; diff --git a/public/plugin/customcertificate/src/export_pdf_all_in_one.php b/public/plugin/customcertificate/src/export_pdf_all_in_one.php index ada7ac0e69..c5d55240ec 100644 --- a/public/plugin/customcertificate/src/export_pdf_all_in_one.php +++ b/public/plugin/customcertificate/src/export_pdf_all_in_one.php @@ -629,7 +629,7 @@ if ($exportAllInOne) { $pdf->content_to_pdf( $certificateContent, - '', + null, 'certificate'.date('Y_m_d_His'), null, 'D', @@ -656,10 +656,10 @@ if ($exportAllInOne) { $pdf = new PDF($params['format'], $params['orientation'], $params); if ($exportZip) { $filePath = $archivePath.$fileName.'.pdf'; - $pdf->content_to_pdf($content, '', $fileName, null, 'F', true, $filePath, false, false, false); + $pdf->content_to_pdf($content, null, $fileName, null, 'F', true, $filePath, false, false, false); $fileList[] = $filePath; } else { - $pdf->content_to_pdf($content, '', $fileName, null, 'D', false, null, false, false, false); + $pdf->content_to_pdf($content, null, $fileName, null, 'D', false, null, false, false, false); } } diff --git a/public/plugin/customcertificate/src/print_certificate.php b/public/plugin/customcertificate/src/print_certificate.php index 2bf24094da..1072aec3cb 100644 --- a/public/plugin/customcertificate/src/print_certificate.php +++ b/public/plugin/customcertificate/src/print_certificate.php @@ -516,7 +516,7 @@ if ($exportAllInOne) { $pdf->content_to_pdf( $certificateContent, - '', + null, 'certificate'.date("Y_m_d_His"), null, 'D', @@ -543,10 +543,10 @@ if ($exportAllInOne) { $pdf = new PDF($params['format'], $params['orientation'], $params); if ($exportZip) { $filePath = $archivePath.$fileName.'.pdf'; - $pdf->content_to_pdf($content, '', $fileName, null, 'F', true, $filePath, false, false, false); + $pdf->content_to_pdf($content, null, $fileName, null, 'F', true, $filePath, false, false, false); $fileList[] = $filePath; } else { - $pdf->content_to_pdf($content, '', $fileName, null, 'D', false, null, false, false, false); + $pdf->content_to_pdf($content, null, $fileName, null, 'D', false, null, false, false, false); } }