123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862 |
- <?php
- use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
- class PDF
- {
- public $pdf;
- public $custom_header = array();
- public $custom_footer = array();
- public $params = array();
- public $template;
-
- public function __construct(
- $pageFormat = 'A4',
- $orientation = 'P',
- $params = array(),
- $template = null
- ) {
- $this->template = $template;
-
- if (!in_array($orientation, array('P','L'))) {
- $orientation = 'P';
- }
-
-
- $params['left'] = isset($params['left']) ? $params['left'] : 15;
- $params['right'] = isset($params['right']) ? $params['right'] : 15;
- $params['top'] = isset($params['top']) ? $params['top'] : 20;
- $params['bottom'] = isset($params['bottom']) ? $params['bottom'] : 15;
- $this->params['filename'] = isset($params['filename']) ? $params['filename'] : api_get_local_time();
- $this->params['pdf_title'] = isset($params['pdf_title']) ? $params['pdf_title'] : get_lang('Untitled');
- $this->params['course_info'] = isset($params['course_info']) ? $params['course_info'] : api_get_course_info();
- $this->params['session_info'] = isset($params['session_info']) ? $params['session_info'] : api_get_session_info(api_get_session_id());
- $this->params['course_code'] = isset($params['course_code']) ? $params['course_code'] : api_get_course_id();
- $this->params['add_signatures'] = isset($params['add_signatures']) ? $params['add_signatures'] : false;
- $this->params['show_real_course_teachers'] = isset($params['show_real_course_teachers']) ? $params['show_real_course_teachers'] : false;
- $this->params['student_info'] = isset($params['student_info']) ? $params['student_info'] : false;
- $this->params['show_grade_generated_date'] = isset($params['show_grade_generated_date']) ? $params['show_grade_generated_date'] : false;
- $this->params['show_teacher_as_myself'] = isset($params['show_teacher_as_myself']) ? $params['show_teacher_as_myself'] : true;
- $this->params['pdf_date'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date(api_get_local_time(), DATE_TIME_FORMAT_LONG);
- $this->pdf = new mPDF(
- 'UTF-8',
- $pageFormat,
- '',
- '',
- $params['left'],
- $params['right'],
- $params['top'],
- $params['bottom'],
- 8,
- 8,
- $orientation
- );
- }
-
- public function html_to_pdf_with_template($content, $saveToFile = false, $returnHtml = false)
- {
- if (empty($this->template)) {
- $tpl = new Template('', false, false, false);
- } else {
- $tpl = $this->template;
- }
-
- $tpl->assign('pdf_content', $content);
- $organization = ChamiloApi::getPlatformLogo();
-
- $pdfLogo = api_get_setting('pdf_logo_header');
- if ($pdfLogo === 'true') {
- $visualTheme = api_get_visual_theme();
- $img = api_get_path(SYS_CSS_PATH).'themes/'.$visualTheme.'/images/pdf_logo_header.png';
- if (file_exists($img)) {
- $img = api_get_path(WEB_CSS_PATH) . 'themes/' . $visualTheme . '/images/pdf_logo_header.png';
- $organization = "<img src='$img'>";
- }
- }
- $tpl->assign('organization', $organization);
-
- if (isset($this->params['show_real_course_teachers']) &&
- $this->params['show_real_course_teachers']
- ) {
- if (isset($this->params['session_info']) &&
- !empty($this->params['session_info'])
- ) {
- $teacher_list = SessionManager::getCoachesByCourseSessionToString(
- $this->params['session_info']['id'],
- $this->params['course_info']['real_id']
- );
- } else {
- $teacher_list = CourseManager::get_teacher_list_from_course_code_to_string(
- $this->params['course_code']
- );
- }
- } else {
- $user_info = api_get_user_info();
- if ($this->params['show_teacher_as_myself']) {
- $teacher_list = $user_info['complete_name'];
- }
- }
- $tpl->assign('pdf_course', $this->params['course_code']);
- $tpl->assign('pdf_course_info', $this->params['course_info']);
- $tpl->assign('pdf_session_info', $this->params['session_info']);
- $tpl->assign('pdf_date', $this->params['pdf_date']);
- $tpl->assign('pdf_teachers', $teacher_list);
- $tpl->assign('pdf_title', $this->params['pdf_title']);
- $tpl->assign('pdf_student_info', $this->params['student_info']);
- $tpl->assign('show_grade_generated_date', $this->params['show_grade_generated_date']);
- $tpl->assign('add_signatures', $this->params['add_signatures']);
-
- $tableTemplate = $tpl->get_template('export/table_pdf.tpl');
- $html = $tpl->fetch($tableTemplate);
- $html = api_utf8_encode($html);
- $css_file = api_get_path(SYS_CSS_PATH).'/print.css';
- $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
- $html = self::content_to_pdf(
- $html,
- $css,
- $this->params['filename'],
- $this->params['course_code'],
- 'D',
- $saveToFile,
- null,
- $returnHtml
- );
- if ($returnHtml) {
- return $html;
- }
- }
-
- public function html_to_pdf(
- $html_file_array,
- $pdf_name = '',
- $course_code = null,
- $print_title = false,
- $complete_style = true,
- $addStyle = true
- ) {
- if ($complete_style === false) {
- error_log(__FUNCTION__.' with no style');
- }
- if (empty($html_file_array)) {
- return false;
- }
- if (is_array($html_file_array)) {
- if (count($html_file_array) == 0) {
- return false;
- }
- } else {
- if (!file_exists($html_file_array)) {
- return false;
- }
-
- $html_file_array = array($html_file_array);
- }
- if (!empty($course_code)) {
- $course_data = api_get_course_info($course_code);
- } else {
- $course_data = api_get_course_info();
- }
-
- $clean_search = array(
- '@<script[^>]*?>.*?</script>@si',
- '@<style[^>]*?>.*?</style>@si'
- );
-
- self::format_pdf($course_data, $complete_style);
- $counter = 1;
- foreach ($html_file_array as $file) {
-
- $page_break = '<pagebreak>';
- if ($counter == count($html_file_array)) {
- $page_break = '';
- }
- $counter++;
-
-
- if (is_array($file) && isset($file['title'])) {
- $html_title = $file['title'];
- $file = $file['path'];
- } else {
-
- $html_title = basename($file);
- }
- if (empty($file) && !empty($html_title)) {
-
- if ($print_title) {
- $this->pdf->WriteHTML(
- '<html><body><h3>'.$html_title.'</h3></body></html>'.$page_break
- );
- }
- continue;
- }
- if (!file_exists($file)) {
-
- continue;
- }
- if ($addStyle) {
- $css_file = api_get_path(SYS_CSS_PATH).'/print.css';
- $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
- $this->pdf->WriteHTML($css, 1);
- }
-
- if ($print_title) {
- $this->pdf->WriteHTML(
- '<html><body><h3>' . $html_title . '</h3></body></html>'
- );
- }
- $file_info = pathinfo($file);
- $extension = $file_info['extension'];
- if (in_array($extension, array('html', 'htm'))) {
- $dirName = $file_info['dirname'];
- $filename = $file_info['basename'];
- $filename = str_replace('_', ' ', $filename);
- if ($extension === 'html') {
- $filename = basename($filename,'.html');
- } elseif($extension === 'htm'){
- $filename = basename($filename,'.htm');
- }
- $document_html = @file_get_contents($file);
- $document_html = preg_replace($clean_search, '', $document_html);
-
- $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);
- if (!empty($course_data['path'])) {
- $document_html= str_replace('../', '', $document_html);
- $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
- $doc = new DOMDocument();
- $result = @$doc->loadHTML($document_html);
-
- $elements = $doc->getElementsByTagName('img');
- if (!empty($elements)) {
- foreach ($elements as $item) {
- $old_src = $item->getAttribute('src');
- if (strpos($old_src, 'http') === false) {
- if (strpos($old_src, '/main/default_course_document') === false) {
- $old_src_fixed = '';
- if (strpos($old_src, '/main/img') === false) {
- if (api_get_path(REL_PATH) != '/') {
- $old_src_fixed = str_replace(
- api_get_path(REL_PATH).'courses/'.$course_data['path'].'/document/',
- '',
- $old_src
- );
-
- if ($old_src_fixed == $old_src) {
- if (file_exists($dirName.'/'.$old_src)) {
- $document_path = '';
- $old_src_fixed = $dirName.'/'.$old_src;
- }
- }
- } else {
- if (strpos($old_src, 'courses/'.$course_data['path'].'/document/') !== false) {
- $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src);
- } else {
-
- if (file_exists($dirName.'/'.$old_src)) {
- $document_path = '';
- $old_src_fixed = $dirName.'/'.$old_src;
- } else {
- $document_path = '';
- $old_src_fixed = $old_src;
- }
- }
- }
- $new_path = $document_path.$old_src_fixed;
- } else {
- $new_path = $old_src;
- }
- $document_html = str_replace($old_src, $new_path, $document_html);
- }
- } else {
-
-
- }
- }
- }
- }
- api_set_encoding_html($document_html, 'UTF-8');
-
- $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');
-
-
-
- if (empty($title)) {
- $title = $filename;
- }
- if (!empty($document_html)) {
- $this->pdf->WriteHTML($document_html.$page_break);
- }
- } elseif (in_array($extension, array('jpg','jpeg','png','gif'))) {
-
- $image = Display::img($file);
- $this->pdf->WriteHTML('<html><body>'.$image.'</body></html>'.$page_break);
- }
- }
- if (empty($pdf_name)) {
- $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
- } else {
- $pdf_name = api_replace_dangerous_char($pdf_name);
- $output_file = $pdf_name.'.pdf';
- }
-
- $this->pdf->Output($output_file, 'D');
- exit;
- }
-
- public function content_to_pdf(
- $document_html,
- $css = '',
- $pdf_name = '',
- $course_code = null,
- $outputMode = 'D',
- $saveInFile = false,
- $fileToSave = null,
- $returnHtml = false
- ) {
- global $_configuration;
- if (empty($document_html)) {
- return false;
- }
-
- $clean_search = array(
- '@<script[^>]*?>.*?</script>@si',
- '@<style[^>]*?>.*?</style>@siU'
- );
-
- $course_data = api_get_course_info($course_code);
- self::format_pdf($course_data);
- $document_html = preg_replace($clean_search, '', $document_html);
-
- $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(
- (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);
-
- $elements = $doc->getElementsByTagName('img');
- if (!empty($elements)) {
- foreach ($elements as $item) {
- $old_src = $item->getAttribute('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(api_get_path(REL_COURSE_PATH).$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');
- $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');
-
-
- if ($returnHtml) {
- return "<style>$css</style>".$document_html;
- }
- if (!empty($css)) {
- $this->pdf->WriteHTML($css, 1);
- }
- $this->pdf->WriteHTML($document_html);
- if (empty($pdf_name)) {
- $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
- } else {
- $pdf_name = api_replace_dangerous_char($pdf_name);
- $output_file = $pdf_name.'.pdf';
- }
-
- if ($outputMode == 'F') {
- $output_file = api_get_path(SYS_ARCHIVE_PATH) . $output_file;
- }
- if ($saveInFile) {
- $fileToSave = !empty($fileToSave) ? $fileToSave : api_get_path(SYS_ARCHIVE_PATH).uniqid();
- $this->pdf->Output(
- $fileToSave,
- $outputMode
- );
- } else {
- $this->pdf->Output(
- $output_file,
- $outputMode
- );
- }
- if ($outputMode != 'F') {
- exit;
- }
- }
-
- public static function get_watermark($course_code = null)
- {
- $web_path = false;
- if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
- $course_info = api_get_course_info($course_code);
- $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
- if (file_exists($store_path)) {
- $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
- }
- } else {
- $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
- if (file_exists($store_path))
- $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
- }
- return $web_path;
- }
-
- public function delete_watermark($course_code = null)
- {
- if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
- $course_info = api_get_course_info($course_code);
-
- $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
- } else {
-
- $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
- }
- if (file_exists($store_path)) {
- unlink($store_path);
- return true;
- }
- return false;
- }
-
- public function upload_watermark($filename, $source_file, $course_code = null)
- {
- if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
- $course_info = api_get_course_info($course_code);
- $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'];
- $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/pdf_watermark.png';
- } else {
- $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images';
- $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
- }
- $course_image = $store_path.'/'.api_get_current_access_url_id().'_pdf_watermark.png';
- if (file_exists($course_image)) {
- @unlink($course_image);
- }
- $my_image = new Image($source_file);
- $result = $my_image->send_image($course_image, -1, 'png');
- if ($result) {
- $result = $web_path;
- }
- return $result;
- }
-
- public function get_header($course_code = null)
- {
-
- }
-
- public function set_footer()
- {
- $this->pdf->defaultfooterfontsize = 12;
- $this->pdf->defaultfooterfontstyle = 'B';
- $this->pdf->defaultfooterline = 1;
- $platform_name = api_get_setting('Institution');
- $left_content = $platform_name;
- $center_content = '';
- $right_content = '{PAGENO} / {nb}';
-
- $footer = array(
- 'odd' => array(
- 'L' => array(
- 'content' => $left_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color' => '#000000'
- ),
- 'C' => array(
- 'content' => $center_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color' => '#000000'
- ),
- 'R' => array(
- 'content' => $right_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color' => '#000000'
- ),
- 'line' => 1,
- ),
- 'even' => array(
- 'L' => array(
- 'content' => $left_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color' => '#000000'
- ),
- 'C' => array(
- 'content' => $center_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color' => '#000000'
- ),
- 'R' => array(
- 'content' => $right_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color' => '#000000'
- ),
- 'line' => 1,
- ),
- );
-
- $this->pdf->SetFooter($footer);
- }
-
- public function set_header($course_data)
- {
- $this->pdf->defaultheaderfontsize = 10;
- $this->pdf->defaultheaderfontstyle = 'BI';
- $this->pdf->defaultheaderline = 1;
- if (!empty($course_data['code'])) {
- $teacher_list = CourseManager::get_teacher_list_from_course_code($course_data['code']);
- $teachers = '';
- if (!empty($teacher_list)) {
- foreach ($teacher_list as $teacher) {
- $teachers[]= $teacher['firstname'].' '.$teacher['lastname'];
- }
- if (count($teachers) > 1) {
- $teachers = get_lang('Teachers').': '.implode(', ', $teachers);
- } else {
- $teachers = get_lang('Teacher').': '.implode('', $teachers);
- }
-
- $user_info = api_get_user_info();
- $teachers = $user_info['complete_name'];
- }
- $left_content = '';
- $center_content = '';
- $right_content = $teachers;
- $header = array(
- 'odd' => array(
- 'L' => array(
- 'content' => $left_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color'=>'#000000'
- ),
- 'C' => array(
- 'content' => $center_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color'=>'#000000'
- ),
- 'R' => array(
- 'content' => $right_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color'=>'#000000'
- ),
- 'line' => 1,
- ),
- 'even' => array(
- 'L' => array(
- 'content' => $left_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color'=>'#000000'
- ),
- 'C' => array(
- 'content' => $center_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color'=>'#000000'
- ),
- 'R' => array(
- 'content' => $right_content,
- 'font-size' => 10,
- 'font-style' => 'B',
- 'font-family' => 'serif',
- 'color'=>'#000000'
- ),
- 'line' => 1,
- ),
- );
- $this->pdf->SetHeader($header);
- }
- }
-
- public function set_custom_header($header)
- {
- $this->custom_header = $header;
- }
-
- public function set_custom_footer($footer)
- {
- $this->custom_footer = $footer;
- }
-
- public function format_pdf($course_data, $complete = true)
- {
- if ($complete === false) {
- error_log('Asked with no decoration');
- }
- $course_code = null;
- if (!empty($course_data)) {
- $course_code = $course_data['code'];
- }
-
-
- $this->pdf->directionality = api_get_text_direction();
- $this->pdf->useOnlyCoreFonts = true;
-
- $this->pdf->mirrorMargins = 1;
-
- if ($complete) {
-
- if (api_get_setting('pdf_export_watermark_enable') == 'true') {
- $watermark_file = self::get_watermark($course_code);
- if ($watermark_file) {
-
- $this->pdf->SetWatermarkImage($watermark_file);
- $this->pdf->showWatermarkImage = true;
- } else {
- $watermark_file = self::get_watermark(null);
- if ($watermark_file) {
- $this->pdf->SetWatermarkImage($watermark_file);
- $this->pdf->showWatermarkImage = true;
- }
- }
- if ($course_code) {
- $watermark_text = api_get_course_setting('pdf_export_watermark_text');
- if (empty($watermark_text)) {
- $watermark_text = api_get_setting('pdf_export_watermark_text');
- }
- } else {
- $watermark_text = api_get_setting('pdf_export_watermark_text');
- }
- if (!empty($watermark_text)) {
- $this->pdf->SetWatermarkText(strcode2utf($watermark_text),0.1);
- $this->pdf->showWatermarkText = true;
- }
- }
- if (empty($this->custom_header)) {
- self::set_header($course_data);
- } else {
- $this->pdf->SetHTMLHeader($this->custom_header,'E');
- $this->pdf->SetHTMLHeader($this->custom_header,'O');
- }
- if (empty($this->custom_footer)) {
- self::set_footer();
- } else {
- $this->pdf->SetHTMLFooter($this->custom_footer);
- }
- }
- }
- }
|