Now we can export images (jpg, jpeg, png, gif) in a PDF document using the LP to PDF export

skala
Julio Montoya 15 years ago
parent 0cea08caea
commit 5b96b6aa64
  1. 136
      main/inc/lib/pdf.lib.php

@ -50,6 +50,7 @@ class PDF {
//Converting the string into an array //Converting the string into an array
$html_file_array = array($html_file_array); $html_file_array = array($html_file_array);
} }
$course_data = array(); $course_data = array();
if (!empty($course_code)) { if (!empty($course_code)) {
$course_data = api_get_course_info($course_code); $course_data = api_get_course_info($course_code);
@ -65,98 +66,97 @@ class PDF {
//Formatting the pdf //Formatting the pdf
self::format_pdf($course_data); self::format_pdf($course_data);
foreach ($html_file_array as $html_file) { foreach ($html_file_array as $file) {
$html_title = ''; $html_title = '';
//if the array provided contained subarrays with 'title' entry, //if the array provided contained subarrays with 'title' entry,
// then print the title in the PDF // then print the title in the PDF
if (is_array($html_file) && isset($html_file['title'])) { if (is_array($file) && isset($file['title'])) {
$html_title = $html_file['title']; $html_title = $file['title'];
$html_file = $html_file['path']; $file = $file['path'];
} else { } else {
//we suppose we've only been sent a file path //we suppose we've only been sent a file path
$html_title = basename($html_file); $html_title = basename($file);
} }
if (empty($html_file) && !empty($html_title)) { if (empty($file) && !empty($html_title)) {
//this is a chapter, print title & skip the rest //this is a chapter, print title & skip the rest
if ($print_title) { if ($print_title) {
$this->pdf->WriteHTML('<html><body><h1>'.$html_title.'</h1></body></html>',2); $this->pdf->WriteHTML('<html><body><h3>'.$html_title.'</h3></body></html>',2);
} }
continue; continue;
} }
if (!file_exists($html_file)) { if (!file_exists($file)) {
//the file doesn't exist, skip //the file doesn't exist, skip
continue; continue;
} }
//it's not a chapter but the file exists, print its title //it's not a chapter but the file exists, print its title
if ($print_title) { if ($print_title) {
$this->pdf->WriteHTML('<html><body><h2>'.$html_title.'</h2></body></html>',2); $this->pdf->WriteHTML('<html><body><h3>'.$html_title.'</h3></body></html>',2);
} }
$file_info = pathinfo($html_file); $file_info = pathinfo($file);
$dirname = str_replace("\\", '/', $file_info['dirname']); $extension = $file_info['extension'];
$filename = $file_info['basename'];
$filename =str_replace('_',' ',$filename);
$extension = $file_info['extension'];
if (!($extension == 'html' || $extension == 'htm')) {
continue;
}
if ($extension == 'html') {
$filename =basename($filename,'.html');
} elseif($extension == 'htm'){
$filename =basename($filename,'.htm');
}
$document_html = @file_get_contents($html_file);
$document_html = preg_replace($clean_search, '', $document_html);
//absolute path for frames.css //TODO: necessary? if (in_array($extension, array('html', 'htm'))) {
$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('<link rel="stylesheet" http://my.chamilo.net/main/css/chamilo/frames.css type="text/css" />','', $document_html); $dirname = str_replace("\\", '/', $file_info['dirname']);
$filename = $file_info['basename'];
if (!empty($course_data['path'])) { $filename = str_replace('_',' ',$filename);
$document_html= str_replace('../','',$document_html);
$document_path = api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/'; if ($extension == 'html') {
$filename =basename($filename,'.html');
$doc = new DOMDocument(); } elseif($extension == 'htm'){
$result = @$doc->loadHTML($document_html); $filename =basename($filename,'.htm');
}
//Fixing only images @todo do the same thing with other elements
$elements = $doc->getElementsByTagName('img'); $document_html = @file_get_contents($file);
$replace_img_elements = array(); $document_html = preg_replace($clean_search, '', $document_html);
if (!empty($elements)) {
foreach($elements as $item) { //absolute path for frames.css //TODO: necessary?
$old_src = $item->getAttribute('src'); $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
//$old_src= str_replace('../','',$old_src); $document_html = str_replace('href="./css/frames.css"',$absolute_css_path, $document_html);
if (strpos($old_src, 'http') === false) {
if (strpos($old_src, '/main/default_course_document') === false) { //$document_html=str_replace('<link rel="stylesheet" http://my.chamilo.net/main/css/chamilo/frames.css type="text/css" />','', $document_html);
$document_html= str_replace($old_src, $document_path.$old_src, $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/';
$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();
if (!empty($elements)) {
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);
}
}
}
} }
} }
} api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
//replace relative path by absolute path for resources $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through
//$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply // $_GET[] too, as it is done with file name.
//$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply // At the moment the title is retrieved from the html document itself.
//$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply if (empty($title)) {
$title = $filename; // Here file name is expected to contain ASCII symbols only.
//$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/'; }
//$document_html= str_replace('src="',$src_http_www, $document_html);
//$document_html= str_replace('temp_template_path', 'src="/main/default_course_document/', $document_html);// restore src templates if (!empty($document_html)) {
$this->pdf->WriteHTML($document_html,2);
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 } elseif (in_array($extension, array('jpg','jpeg','png','gif'))) {
// $_GET[] too, as it is done with file name. //Images
// At the moment the title is retrieved from the html document itself. $image = Display::img($file);
if (empty($title)) { $this->pdf->WriteHTML('<html><body>'.$image.'</body></html>',2);
$title = $filename; // Here file name is expected to contain ASCII symbols only. }
}
$this->pdf->WriteHTML($document_html,2);
} }
if (empty($pdf_name)) { if (empty($pdf_name)) {

Loading…
Cancel
Save