/** * Gets the title of the quiz file given as parameter. * @param string File name * @param string File path * @return string The exercise title */ function GetQuizName($fname, $fpath) { $title = GetComment($fname); if (trim($title) == '') { if (file_exists($fpath . $fname)) { if (!($fp = @fopen($fpath . $fname, 'r'))) { //die('Could not open Quiz input.'); return basename($fname); } $contents = @fread($fp, filesize($fpath . $fname)); @fclose($fp); $title = api_get_title_html($contents); } } if ($title == '') { $title = basename($fname); } return (string) $title; }
/** * Converts an html string to PDF * @param string $document_html valid html * @param string $css CSS content of a CSS file * @param string $pdf_name pdf name * @param string $course_code course code * (if you are using html that are located in the document tool you must provide this) * @return string Web path */ public function content_to_pdf( $document_html, $css = '', $pdf_name = '', $course_code = null ) { global $_configuration; if (empty($document_html)) { return false; } //clean styles and javascript document $clean_search = array ( '@<script[^>]*?>.*?</script>@si', '@<style[^>]*?>.*?</style>@siU' ); // Formatting the pdf $course_data = api_get_course_info($course_code); self::format_pdf($course_data); $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"','href="'.$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); $document_html= str_replace('../../','',$document_html); $document_html= str_replace('../','',$document_html); $document_html= str_replace((empty($_configuration['url_append'])?'':$_configuration['url_append'].'/').'courses/'.$course_code.'/document/','',$document_html); if (!empty($course_data['path'])) { $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'); 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) { 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); } } } } } } //replace relative path by absolute path for resources //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply //$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply //$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 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. // At the moment the title is retrieved from the html document itself. if (!empty($css)) { $this->pdf->WriteHTML($css, 1); } $this->pdf->WriteHTML($document_html, 2); if (empty($pdf_name)) { $output_file = 'pdf_'.date('Y-m-d-his').'.pdf'; } else { $pdf_name = replace_dangerous_char($pdf_name); $output_file = $pdf_name.'.pdf'; } $this->pdf->Output($output_file, 'D'); // F to save the pdf in a file exit; }