From 0d8a956c3ce39c8b34e9b854e14a788f625a29bb Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 9 Jul 2012 18:45:15 +0200 Subject: [PATCH] Should fix bug when exporting LP to PDF see #4894 --- main/inc/lib/pdf.lib.php | 64 +++++++++++++++++-------------- main/newscorm/learnpath.class.php | 49 +++++++++++------------ 2 files changed, 61 insertions(+), 52 deletions(-) diff --git a/main/inc/lib/pdf.lib.php b/main/inc/lib/pdf.lib.php index 77bb829572..bb24963d25 100644 --- a/main/inc/lib/pdf.lib.php +++ b/main/inc/lib/pdf.lib.php @@ -42,7 +42,7 @@ class PDF { } /** - * Converts an html file to PDF + * Converts HTML files to PDF * @param mixed could be an html file path or an array with paths example: /var/www/myfile.html or array('/myfile.html','myotherfile.html') or even an indexed array with both 'title' and 'path' indexes for each element like array(0=>array('title'=>'Hello','path'=>'file.html'),1=>array('title'=>'Bye','path'=>'file2.html')); * @param string pdf name * @param string course code (if you are using html that are located in the document tool you must provide this) @@ -68,8 +68,7 @@ class PDF { $course_data = array(); if (!empty($course_code)) { $course_data = api_get_course_info($course_code); - } - + } //clean styles and javascript document $clean_search = array ( @@ -79,17 +78,20 @@ class PDF { //Formatting the pdf self::format_pdf($course_data); + $counter = 1; foreach ($html_file_array as $file) { + //Add a page break per file $page_break = ''; if ($counter == count($html_file_array)) { $page_break = ''; } - $counter++; + $counter++; $html_title = ''; + //if the array provided contained subarrays with 'title' entry, // then print the title in the PDF if (is_array($file) && isset($file['title'])) { @@ -99,14 +101,15 @@ class PDF { //we suppose we've only been sent a file path $html_title = basename($file); } + if (empty($file) && !empty($html_title)) { //this is a chapter, print title & skip the rest if ($print_title) { - $this->pdf->WriteHTML('

'.$html_title.'

'.$page_break,2); + $this->pdf->WriteHTML('

'.$html_title.'

'.$page_break, 2); } continue; - } - + } + if (!file_exists($file)) { //the file doesn't exist, skip continue; @@ -121,28 +124,28 @@ class PDF { $extension = $file_info['extension']; if (in_array($extension, array('html', 'htm'))) { - $dirname = str_replace("\\", '/', $file_info['dirname']); $filename = $file_info['basename']; $filename = str_replace('_',' ',$filename); if ($extension == 'html') { - $filename =basename($filename,'.html'); + $filename = basename($filename,'.html'); } elseif($extension == 'htm'){ - $filename =basename($filename,'.htm'); - } + $filename = basename($filename,'.htm'); + } $document_html = @file_get_contents($file); $document_html = preg_replace($clean_search, '', $document_html); //absolute path for frames.css //TODO: necessary? $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css'; - $document_html = str_replace('href="./css/frames.css"',$absolute_css_path, $document_html); + $document_html = str_replace('href="./css/frames.css"', $absolute_css_path, $document_html); //$document_html=str_replace('','', $document_html); if (!empty($course_data['path'])) { $document_html= str_replace('../','',$document_html); - $document_path = api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/'; + //$document_path = api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/'; + $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/'; $doc = new DOMDocument(); $result = @$doc->loadHTML($document_html); @@ -150,17 +153,21 @@ class PDF { //Fixing only images @todo do the same thing with other elements $elements = $doc->getElementsByTagName('img'); if (!empty($elements)) { - foreach($elements as $item) { + foreach ($elements as $item) { $old_src = $item->getAttribute('src'); - //$old_src= str_replace('../','',$old_src); + if (strpos($old_src, 'http') === false) { - if (strpos($old_src, '/main/default_course_document') === false) { - $document_html= str_replace($old_src, $document_path.$old_src, $document_html); + if (strpos($old_src, '/main/default_course_document') === false) { + $old_src_fixed = str_replace('/courses/'.$course_data['path'].'/document/', '', $old_src); + $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed); + $new_path = $document_path.$old_src_fixed; + $document_html= str_replace($old_src, $new_path, $document_html); } } } } } + api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through // $_GET[] too, as it is done with file name. @@ -187,8 +194,7 @@ class PDF { } $result = $this->pdf->Output($output_file, 'D'); /// F to save the pdf in a file exit; - } - + } /** @@ -229,23 +235,26 @@ class PDF { $document_html= str_replace('courses/'.$course_code.'/document/','',$document_html); if (!empty($course_data['path'])) { - $document_path = api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/'; + $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/'; $doc = new DOMDocument(); $result = @$doc->loadHTML($document_html); //Fixing only images @todo do the same thing with other elements - $elements = $doc->getElementsByTagName('img'); - $replace_img_elements = array(); + $elements = $doc->getElementsByTagName('img'); if (!empty($elements)) { - foreach($elements as $item) { + foreach ($elements as $item) { $old_src = $item->getAttribute('src'); //$old_src= str_replace('../','',$old_src); if (strpos($old_src, 'http') === false) { if (strpos($old_src, '/main/default_course_document') === false) { - if (strpos($old_src, '/main/inc/lib/') === false) { - $document_html= str_replace($old_src, $document_path.$old_src, $document_html); - //var_dump($old_src, $document_path.$old_src); + if (strpos($old_src, '/main/inc/lib/') === false) { + + $old_src_fixed = str_replace('/courses/'.$course_data['path'].'/document/', '', $old_src); + $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed); + $new_path = $document_path.$old_src_fixed; + $document_html= str_replace($old_src, $new_path, $document_html); + } } } @@ -281,8 +290,7 @@ class PDF { $pdf_name = replace_dangerous_char($pdf_name); $output_file = $pdf_name.'.pdf'; } - $result = $this->pdf->Output($output_file, 'D'); /// F to save the pdf in a file - + $result = $this->pdf->Output($output_file, 'D'); // F to save the pdf in a file exit; } diff --git a/main/newscorm/learnpath.class.php b/main/newscorm/learnpath.class.php index 92e0552c0d..0535fe84bb 100644 --- a/main/newscorm/learnpath.class.php +++ b/main/newscorm/learnpath.class.php @@ -8966,30 +8966,31 @@ EOD; $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path; $list = $this->get_flat_ordered_items_list($lp_id); - - foreach ($list as $item_id) { - $item = $this->items[$item_id]; - //Getting documents from a LP with chamilo documents - switch ($item->type) { - case 'document': - $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc); - $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path']; - if (file_exists($file_path)) { - $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path); - } - break; - case 'asset': //commes from a scorm package generated by chamilo - case 'sco': - $file_path = $scorm_path.'/'.$item->path; - if (file_exists($file_path)) { - $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path); - } - break; - case 'dokeos_chapter': - case 'dir': - case 'chapter': - $files_to_export[] = array('title'=>$item->get_title(),'path'=>null); - break; + if (!empty($list)) { + foreach ($list as $item_id) { + $item = $this->items[$item_id]; + switch ($item->type) { + case 'document': + //Getting documents from a LP with chamilo documents + $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc); + $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path']; + if (file_exists($file_path)) { + $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path); + } + break; + case 'asset': //commes from a scorm package generated by chamilo + case 'sco': + $file_path = $scorm_path.'/'.$item->path; + if (file_exists($file_path)) { + $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path); + } + break; + case 'dokeos_chapter': + case 'dir': + case 'chapter': + $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null); + break; + } } } $pdf = new PDF();