pdf.lib.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * Class PDF
  5. * @package chamilo.library
  6. *
  7. */
  8. class PDF
  9. {
  10. public $pdf;
  11. public $custom_header = '';
  12. public $custom_footer = '';
  13. public $params = array();
  14. /**
  15. * Creates the mPDF object
  16. * @param string $pageFormat format A4 A4-L see http://mpdf1.com/manual/index.php?tid=184&searchstring=format
  17. * @param string $orientation orientation "P" = Portrait "L" = Landscape
  18. * @param array $params
  19. */
  20. public function __construct(
  21. $pageFormat = 'A4',
  22. $orientation = 'P',
  23. $params = array()
  24. ) {
  25. /* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=mPDF
  26. * mPDF ([ string $mode [, mixed $format [, float $default_font_size [, string $default_font [, float $margin_left , float $margin_right , float $margin_top , float $margin_bottom , float $margin_header , float $margin_footer [, string $orientation ]]]]]])
  27. */
  28. if (!in_array($orientation, array('P','L'))) {
  29. $orientation = 'P';
  30. }
  31. //$this->pdf = $pdf = new mPDF('UTF-8', $pageFormat, '', '', 30, 20, 27, 25, 16, 13, $orientation);
  32. //left, right, top, bottom, margin_header, margin footer
  33. $params['left'] = isset($params['left']) ? $params['left'] : 15;
  34. $params['right'] = isset($params['right']) ? $params['right'] : 15;
  35. $params['top'] = isset($params['top']) ? $params['top'] : 20;
  36. $params['bottom'] = isset($params['bottom']) ? $params['bottom'] : 15;
  37. $this->params['filename'] = isset($params['filename']) ? $params['filename'] : api_get_local_time();
  38. $this->params['pdf_title'] = isset($params['pdf_title']) ? $params['pdf_title'] : get_lang('Untitled');
  39. $this->params['course_info'] = isset($params['course_info']) ? $params['course_info'] : api_get_course_info();
  40. $this->params['session_info'] = isset($params['session_info']) ? $params['session_info'] : api_get_session_info(api_get_session_id());
  41. $this->params['course_code'] = isset($params['course_code']) ? $params['course_code'] : api_get_course_id();
  42. $this->params['add_signatures'] = isset($params['add_signatures']) ? $params['add_signatures'] : false;
  43. $this->params['show_real_course_teachers'] = isset($params['show_real_course_teachers']) ? $params['show_real_course_teachers'] : false;
  44. $this->params['student_info'] = isset($params['student_info']) ? $params['student_info'] : false;
  45. $this->params['show_grade_generated_date'] = isset($params['show_grade_generated_date']) ? $params['show_grade_generated_date'] : false;
  46. $this->pdf = new mPDF(
  47. 'UTF-8',
  48. $pageFormat,
  49. '',
  50. '',
  51. $params['left'],
  52. $params['right'],
  53. $params['top'],
  54. $params['bottom'],
  55. 8,
  56. 8,
  57. $orientation
  58. );
  59. }
  60. /**
  61. * Export the given HTML to PDF, using a global template
  62. *
  63. * @uses export/table_pdf.tpl
  64. * @param $content
  65. * @param bool|false $saveToFile
  66. * @param bool|false $returnHtml
  67. *
  68. * @return string
  69. */
  70. public function html_to_pdf_with_template($content, $saveToFile = false, $returnHtml = false)
  71. {
  72. Display :: display_no_header();
  73. // Assignments
  74. Display::$global_template->assign('pdf_content', $content);
  75. $organization = api_get_setting('Institution');
  76. $img = api_get_path(SYS_CSS_PATH).'themes/'.api_get_visual_theme().'/images/header-logo.png';
  77. // Search for classic logo
  78. if (file_exists($img)) {
  79. $img = api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/images/header-logo.png';
  80. $organization = "<img src='$img'>";
  81. } else {
  82. // Just use the platform title.
  83. if (!empty($organization)) {
  84. $organization = '<h2 align="left">'.$organization.'</h2>';
  85. }
  86. }
  87. // Use custom logo image.
  88. $pdfLogo = api_get_setting('pdf_logo_header');
  89. if ($pdfLogo === 'true') {
  90. $visualTheme = api_get_visual_theme();
  91. $img = api_get_path(SYS_CSS_PATH).'themes/'.$visualTheme.'/images/pdf_logo_header.png';
  92. if (file_exists($img)) {
  93. $img = api_get_path(WEB_CSS_PATH) . 'themes/' . $visualTheme . '/images/pdf_logo_header.png';
  94. $organization = "<img src='$img'>";
  95. }
  96. }
  97. Display::$global_template->assign('organization', $organization);
  98. //Showing only the current teacher/admin instead the all teacher list name see BT#4080
  99. if (isset($this->params['show_real_course_teachers']) &&
  100. $this->params['show_real_course_teachers']
  101. ) {
  102. if (isset($this->params['session_info']) &&
  103. !empty($this->params['session_info'])
  104. ) {
  105. $teacher_list = SessionManager::getCoachesByCourseSessionToString(
  106. $this->params['session_info']['id'],
  107. $this->params['course_info']['real_id']
  108. );
  109. } else {
  110. $teacher_list = CourseManager::get_teacher_list_from_course_code_to_string(
  111. $this->params['course_code']
  112. );
  113. }
  114. } else {
  115. $user_info = api_get_user_info();
  116. $teacher_list = $user_info['complete_name'];
  117. }
  118. Display::$global_template->assign('pdf_course', $this->params['course_code']);
  119. Display::$global_template->assign('pdf_course_info', $this->params['course_info']);
  120. Display::$global_template->assign('pdf_session_info', $this->params['session_info']);
  121. Display::$global_template->assign('pdf_date', api_format_date(api_get_local_time(), DATE_TIME_FORMAT_LONG));
  122. Display::$global_template->assign('pdf_teachers', $teacher_list);
  123. Display::$global_template->assign('pdf_title', $this->params['pdf_title']);
  124. Display::$global_template->assign('pdf_student_info', $this->params['student_info']);
  125. Display::$global_template->assign('show_grade_generated_date', $this->params['show_grade_generated_date']);
  126. Display::$global_template->assign('add_signatures', $this->params['add_signatures']);
  127. // Getting template
  128. $tpl = Display::$global_template->get_template('export/table_pdf.tpl');
  129. $html = Display::$global_template->fetch($tpl);
  130. $html = api_utf8_encode($html);
  131. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).'/print.css';
  132. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  133. $html = self::content_to_pdf(
  134. $html,
  135. $css,
  136. $this->params['filename'],
  137. $this->params['course_code'],
  138. 'D',
  139. $saveToFile,
  140. null,
  141. $returnHtml
  142. );
  143. if ($returnHtml) {
  144. return $html;
  145. }
  146. }
  147. /**
  148. * Converts HTML files to PDF
  149. * @param mixed $html_file_array could be an html file path or an array
  150. * with paths example:
  151. * /var/www/myfile.html or array('/myfile.html','myotherfile.html') or
  152. * even an indexed array with both 'title' and 'path' indexes
  153. * for each element like
  154. * array(
  155. * 0 => array('title'=>'Hello','path'=>'file.html'),
  156. * 1 => array('title'=>'Bye','path'=>'file2.html')
  157. * );
  158. * @param string pdf name
  159. * @param string course code (if you are using html that are located in the document tool you must provide this)
  160. * @param bool Whether to print the header, footer and watermark (true) or just the content (false)
  161. * @return bool
  162. */
  163. public function html_to_pdf(
  164. $html_file_array,
  165. $pdf_name = '',
  166. $course_code = null,
  167. $print_title = false,
  168. $complete_style = true,
  169. $addStyle = true
  170. ) {
  171. if ($complete_style === false) {
  172. error_log(__FUNCTION__.' with no style');
  173. }
  174. if (empty($html_file_array)) {
  175. return false;
  176. }
  177. if (is_array($html_file_array)) {
  178. if (count($html_file_array) == 0) {
  179. return false;
  180. }
  181. } else {
  182. if (!file_exists($html_file_array)) {
  183. return false;
  184. }
  185. // Converting the string into an array
  186. $html_file_array = array($html_file_array);
  187. }
  188. if (!empty($course_code)) {
  189. $course_data = api_get_course_info($course_code);
  190. } else {
  191. $course_data = api_get_course_info();
  192. }
  193. // Clean styles and javascript document
  194. $clean_search = array (
  195. '@<script[^>]*?>.*?</script>@si',
  196. '@<style[^>]*?>.*?</style>@si'
  197. );
  198. // Formatting the pdf
  199. self::format_pdf($course_data, $complete_style);
  200. $counter = 1;
  201. foreach ($html_file_array as $file) {
  202. //Add a page break per file
  203. $page_break = '<pagebreak>';
  204. if ($counter == count($html_file_array)) {
  205. $page_break = '';
  206. }
  207. $counter++;
  208. //if the array provided contained subarrays with 'title' entry,
  209. // then print the title in the PDF
  210. if (is_array($file) && isset($file['title'])) {
  211. $html_title = $file['title'];
  212. $file = $file['path'];
  213. } else {
  214. //we suppose we've only been sent a file path
  215. $html_title = basename($file);
  216. }
  217. if (empty($file) && !empty($html_title)) {
  218. //this is a chapter, print title & skip the rest
  219. if ($print_title) {
  220. $this->pdf->WriteHTML(
  221. '<html><body><h3>'.$html_title.'</h3></body></html>'.$page_break,
  222. 2
  223. );
  224. }
  225. continue;
  226. }
  227. if (!file_exists($file)) {
  228. //the file doesn't exist, skip
  229. continue;
  230. }
  231. if ($addStyle) {
  232. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).'/print.css';
  233. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  234. $this->pdf->WriteHTML($css, 1);
  235. }
  236. //it's not a chapter but the file exists, print its title
  237. if ($print_title) {
  238. $this->pdf->WriteHTML(
  239. '<html><body><h3>' . $html_title . '</h3></body></html>',
  240. 2
  241. );
  242. }
  243. $file_info = pathinfo($file);
  244. $extension = $file_info['extension'];
  245. if (in_array($extension, array('html', 'htm'))) {
  246. $filename = $file_info['basename'];
  247. $filename = str_replace('_',' ',$filename);
  248. if ($extension == 'html') {
  249. $filename = basename($filename,'.html');
  250. } elseif($extension == 'htm'){
  251. $filename = basename($filename,'.htm');
  252. }
  253. $document_html = @file_get_contents($file);
  254. $document_html = preg_replace($clean_search, '', $document_html);
  255. //absolute path for frames.css //TODO: necessary?
  256. $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
  257. $document_html = str_replace('href="./css/frames.css"', $absolute_css_path, $document_html);
  258. if (!empty($course_data['path'])) {
  259. $document_html= str_replace('../','', $document_html);
  260. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  261. $doc = new DOMDocument();
  262. $result = @$doc->loadHTML($document_html);
  263. //Fixing only images @todo do the same thing with other elements
  264. $elements = $doc->getElementsByTagName('img');
  265. if (!empty($elements)) {
  266. foreach ($elements as $item) {
  267. $old_src = $item->getAttribute('src');
  268. if (strpos($old_src, 'http') === false) {
  269. if (strpos($old_src, '/main/default_course_document') === false) {
  270. $old_src_fixed = '';
  271. if (strpos($old_src, '/main/img') === false) {
  272. if (api_get_path(REL_PATH) != '/') {
  273. $old_src_fixed = str_replace(api_get_path(REL_PATH).'courses/'.$course_data['path'].'/document/', '', $old_src);
  274. } else {
  275. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src);
  276. }
  277. $new_path = $document_path.$old_src_fixed;
  278. } else {
  279. $new_path = $old_src;
  280. }
  281. $document_html= str_replace($old_src, $new_path, $document_html);
  282. }
  283. } else {
  284. //Check if this is a complete URL
  285. /*if (strpos($old_src, 'courses/'.$course_data['path'].'/document/') === false) {
  286. } else {
  287. $old_src_fixed = str_replace(api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/', '', $old_src);
  288. $new_path = $document_path.$old_src_fixed;
  289. $document_html= str_replace($old_src, $new_path, $document_html);
  290. }*/
  291. }
  292. }
  293. }
  294. }
  295. api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  296. // TODO: Maybe it is better idea the title to be passed through
  297. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');
  298. // $_GET[] too, as it is done with file name.
  299. // At the moment the title is retrieved from the html document itself.
  300. //echo $document_html;exit;
  301. if (empty($title)) {
  302. $title = $filename; // Here file name is expected to contain ASCII symbols only.
  303. }
  304. if (!empty($document_html)) {
  305. $this->pdf->WriteHTML($document_html.$page_break, 2);
  306. }
  307. } elseif (in_array($extension, array('jpg','jpeg','png','gif'))) {
  308. //Images
  309. $image = Display::img($file);
  310. $this->pdf->WriteHTML('<html><body>'.$image.'</body></html>'.$page_break, 2);
  311. }
  312. }
  313. if (empty($pdf_name)) {
  314. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  315. } else {
  316. $pdf_name = api_replace_dangerous_char($pdf_name);
  317. $output_file = $pdf_name.'.pdf';
  318. }
  319. // F to save the pdf in a file
  320. $this->pdf->Output($output_file, 'D');
  321. exit;
  322. }
  323. /**
  324. * Converts an html string to PDF
  325. * @param string $document_html valid html
  326. * @param string $css CSS content of a CSS file
  327. * @param string $pdf_name pdf name
  328. * @param string $course_code course code
  329. * (if you are using html that are located in the document tool you must provide this)
  330. * @param string $outputMode the MPDF output mode can be:
  331. * 'I' (print on standard output),
  332. * 'D' (download file) (this is the default value),
  333. * 'F' (save to local file) or
  334. * 'S' (return as a string)
  335. * @return string Web path
  336. */
  337. public function content_to_pdf(
  338. $document_html,
  339. $css = '',
  340. $pdf_name = '',
  341. $course_code = null,
  342. $outputMode = 'D',
  343. $saveInFile = false,
  344. $fileToSave = null,
  345. $returnHtml = false
  346. ) {
  347. global $_configuration;
  348. if (empty($document_html)) {
  349. return false;
  350. }
  351. //clean styles and javascript document
  352. $clean_search = array (
  353. '@<script[^>]*?>.*?</script>@si',
  354. '@<style[^>]*?>.*?</style>@siU'
  355. );
  356. // Formatting the pdf
  357. $course_data = api_get_course_info($course_code);
  358. self::format_pdf($course_data);
  359. $document_html = preg_replace($clean_search, '', $document_html);
  360. //absolute path for frames.css //TODO: necessary?
  361. $absolute_css_path = api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/frames.css';
  362. $document_html = str_replace('href="./css/frames.css"','href="'.$absolute_css_path.'"', $document_html);
  363. $document_html= str_replace('../../','',$document_html);
  364. $document_html= str_replace('../','',$document_html);
  365. $document_html= str_replace((empty($_configuration['url_append'])?'':$_configuration['url_append'].'/').'courses/'.$course_code.'/document/','',$document_html);
  366. if (!empty($course_data['path'])) {
  367. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  368. $doc = new DOMDocument();
  369. $result = @$doc->loadHTML($document_html);
  370. //Fixing only images @todo do the same thing with other elements
  371. $elements = $doc->getElementsByTagName('img');
  372. if (!empty($elements)) {
  373. foreach ($elements as $item) {
  374. $old_src = $item->getAttribute('src');
  375. //$old_src= str_replace('../','',$old_src);
  376. if (strpos($old_src, 'http') === false) {
  377. if (strpos($old_src, '/main/default_course_document') === false) {
  378. if (strpos($old_src, '/main/inc/lib/') === false) {
  379. $old_src_fixed = str_replace('/courses/'.$course_data['path'].'/document/', '', $old_src);
  380. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed);
  381. $new_path = $document_path.$old_src_fixed;
  382. $document_html= str_replace($old_src, $new_path, $document_html);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. //replace relative path by absolute path for resources
  390. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  391. //$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply
  392. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  393. //$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
  394. //$document_html= str_replace('src="',$src_http_www, $document_html);
  395. //$document_html= str_replace('temp_template_path', 'src="/main/default_course_document/', $document_html);// restore src templates
  396. api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  397. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through
  398. // $_GET[] too, as it is done with file name.
  399. // At the moment the title is retrieved from the html document itself.
  400. if ($returnHtml) {
  401. return "<style>$css</style>".$document_html;
  402. }
  403. if (!empty($css)) {
  404. $this->pdf->WriteHTML($css, 1);
  405. }
  406. $this->pdf->WriteHTML($document_html, 2);
  407. if (empty($pdf_name)) {
  408. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  409. } else {
  410. $pdf_name = api_replace_dangerous_char($pdf_name);
  411. $output_file = $pdf_name.'.pdf';
  412. }
  413. //$this->pdf->Output($output_file, $outputMode); // F to save the pdf in a file
  414. if ($outputMode == 'F') {
  415. $output_file = api_get_path(SYS_ARCHIVE_PATH) . $output_file;
  416. }
  417. if ($saveInFile) {
  418. $fileToSave = !empty($fileToSave) ? $fileToSave : api_get_path(SYS_ARCHIVE_PATH).uniqid();
  419. $this->pdf->Output(
  420. $fileToSave,
  421. $outputMode
  422. ); // F to save the pdf in a file
  423. } else {
  424. $this->pdf->Output(
  425. $output_file,
  426. $outputMode
  427. ); // F to save the pdf in a file
  428. }
  429. if ($outputMode != 'F') {
  430. exit;
  431. }
  432. }
  433. /**
  434. * Gets the watermark from the platform or a course
  435. * @param string course code (optional)
  436. * @param mixed web path of the watermark image, false if there is nothing to return
  437. */
  438. public static function get_watermark($course_code = null)
  439. {
  440. $web_path = false;
  441. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  442. $course_info = api_get_course_info($course_code);
  443. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  444. if (file_exists($store_path)) {
  445. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  446. }
  447. } else {
  448. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  449. if (file_exists($store_path))
  450. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  451. }
  452. return $web_path;
  453. }
  454. /**
  455. * Deletes the watermark from the Platform or Course
  456. * @param string $course_code course code (optional)
  457. * @param mixed web path of the watermark image, false if there is nothing to return
  458. */
  459. public function delete_watermark($course_code = null)
  460. {
  461. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  462. $course_info = api_get_course_info($course_code);
  463. // course path
  464. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  465. } else {
  466. // course path
  467. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  468. }
  469. if (file_exists($store_path)) {
  470. unlink($store_path);
  471. return true;
  472. }
  473. return false;
  474. }
  475. /**
  476. * Uploads the pdf watermark in the main/default_course_document directory or in the course directory
  477. * @param string $filename filename
  478. * @param string $source_file path of the file
  479. * @param string $course_code
  480. * @return mixed web path of the file if sucess, false otherwise
  481. */
  482. public function upload_watermark($filename, $source_file, $course_code = null)
  483. {
  484. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  485. $course_info = api_get_course_info($course_code);
  486. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path']; // course path
  487. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/pdf_watermark.png';
  488. } else {
  489. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images'; // course path
  490. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  491. }
  492. $course_image = $store_path.'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  493. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  494. $result = false;
  495. if (file_exists($course_image)) {
  496. @unlink($course_image);
  497. }
  498. $my_image = new Image($source_file);
  499. $result = $my_image->send_image($course_image, -1, 'png');
  500. if ($result) {
  501. $result = $web_path;
  502. }
  503. return $result;
  504. }
  505. /**
  506. * Returns the default header
  507. */
  508. public function get_header($course_code = null)
  509. {
  510. /*$header = api_get_setting('pdf_export_watermark_text');
  511. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  512. $header = api_get_course_setting('pdf_export_watermark_text');
  513. }
  514. return $header;*/
  515. }
  516. /**
  517. *
  518. */
  519. public function set_footer()
  520. {
  521. $this->pdf->defaultfooterfontsize = 12; // in pts
  522. $this->pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
  523. $this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer
  524. $platform_name = api_get_setting('Institution');
  525. $left_content = $platform_name;
  526. $center_content = '';
  527. $right_content = '{PAGENO} / {nb}';
  528. //@todo remove this and use a simpler way
  529. $footer = array(
  530. 'odd' => array(
  531. 'L' => array(
  532. 'content' => $left_content,
  533. 'font-size' => 10,
  534. 'font-style' => 'B',
  535. 'font-family' => 'serif',
  536. 'color' => '#000000'
  537. ),
  538. 'C' => array(
  539. 'content' => $center_content,
  540. 'font-size' => 10,
  541. 'font-style' => 'B',
  542. 'font-family' => 'serif',
  543. 'color' => '#000000'
  544. ),
  545. 'R' => array(
  546. 'content' => $right_content,
  547. 'font-size' => 10,
  548. 'font-style' => 'B',
  549. 'font-family' => 'serif',
  550. 'color' => '#000000'
  551. ),
  552. 'line' => 1,
  553. ),
  554. 'even' => array(
  555. 'L' => array(
  556. 'content' => $left_content,
  557. 'font-size' => 10,
  558. 'font-style' => 'B',
  559. 'font-family' => 'serif',
  560. 'color' => '#000000'
  561. ),
  562. 'C' => array(
  563. 'content' => $center_content,
  564. 'font-size' => 10,
  565. 'font-style' => 'B',
  566. 'font-family' => 'serif',
  567. 'color' => '#000000'
  568. ),
  569. 'R' => array(
  570. 'content' => $right_content,
  571. 'font-size' => 10,
  572. 'font-style' => 'B',
  573. 'font-family' => 'serif',
  574. 'color' => '#000000'
  575. ),
  576. 'line' => 1,
  577. ),
  578. );
  579. // defines footer for Odd and Even Pages - placed at Outer margin see http://mpdf1.com/manual/index.php?tid=151&searchstring=setfooter
  580. $this->pdf->SetFooter($footer);
  581. }
  582. /**
  583. * @param array $course_data
  584. */
  585. public function set_header($course_data)
  586. {
  587. $this->pdf->defaultheaderfontsize = 10; // in pts
  588. $this->pdf->defaultheaderfontstyle = 'BI'; // blank, B, I, or BI
  589. $this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer
  590. if (!empty($course_data['code'])) {
  591. $teacher_list = CourseManager::get_teacher_list_from_course_code($course_data['code']);
  592. $teachers = '';
  593. if (!empty($teacher_list)) {
  594. foreach ($teacher_list as $teacher) {
  595. $teachers[]= $teacher['firstname'].' '.$teacher['lastname'];
  596. }
  597. if (count($teachers) > 1) {
  598. $teachers = get_lang('Teachers').': '.implode(', ', $teachers);
  599. } else {
  600. $teachers = get_lang('Teacher').': '.implode('', $teachers);
  601. }
  602. // Do not show the teacher list see BT#4080 only the current teacher name
  603. $user_info = api_get_user_info();
  604. $teachers = $user_info['complete_name'];
  605. }
  606. $left_content = '';
  607. $center_content = '';
  608. $right_content = $teachers;
  609. $header = array(
  610. 'odd' => array(
  611. 'L' => array(
  612. 'content' => $left_content,
  613. 'font-size' => 10,
  614. 'font-style' => 'B',
  615. 'font-family' => 'serif',
  616. 'color'=>'#000000'
  617. ),
  618. 'C' => array(
  619. 'content' => $center_content,
  620. 'font-size' => 10,
  621. 'font-style' => 'B',
  622. 'font-family' => 'serif',
  623. 'color'=>'#000000'
  624. ),
  625. 'R' => array(
  626. 'content' => $right_content,
  627. 'font-size' => 10,
  628. 'font-style' => 'B',
  629. 'font-family' => 'serif',
  630. 'color'=>'#000000'
  631. ),
  632. 'line' => 1,
  633. ),
  634. 'even' => array(
  635. 'L' => array(
  636. 'content' => $left_content,
  637. 'font-size' => 10,
  638. 'font-style' => 'B',
  639. 'font-family' => 'serif',
  640. 'color'=>'#000000'
  641. ),
  642. 'C' => array(
  643. 'content' => $center_content,
  644. 'font-size' => 10,
  645. 'font-style' => 'B',
  646. 'font-family' => 'serif',
  647. 'color'=>'#000000'
  648. ),
  649. 'R' => array(
  650. 'content' => $right_content,
  651. 'font-size' => 10,
  652. 'font-style' => 'B',
  653. 'font-family' => 'serif',
  654. 'color'=>'#000000'
  655. ),
  656. 'line' => 1,
  657. ),
  658. );
  659. $this->pdf->SetHeader($header);// ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
  660. }
  661. }
  662. /**
  663. * @param string $header html content
  664. */
  665. public function set_custom_header($header)
  666. {
  667. $this->custom_header = $header;
  668. }
  669. /**
  670. * @param string $footer html content
  671. */
  672. public function set_custom_footer($footer)
  673. {
  674. $this->custom_footer = $footer;
  675. }
  676. /**
  677. * Pre-formats a PDF to the right size and, if not stated otherwise, with
  678. * header, footer and watermark (if any)
  679. * @param array $course_data General course information (to fill headers)
  680. * @param bool $complete Whether we want headers, footers and watermark or not
  681. */
  682. public function format_pdf($course_data, $complete = true)
  683. {
  684. if ($complete === false) {
  685. error_log('Asked with no decoration');
  686. }
  687. $course_code = null;
  688. if (!empty($course_data)) {
  689. $course_code = $course_data['code'];
  690. }
  691. /*$pdf->SetAuthor('Documents Chamilo');
  692. $pdf->SetTitle('title');
  693. $pdf->SetSubject('Exported from Chamilo Documents');
  694. $pdf->SetKeywords('Chamilo Documents');
  695. */
  696. // TODO: To be read from the html document.
  697. $this->pdf->directionality = api_get_text_direction();
  698. $this->pdf->useOnlyCoreFonts = true;
  699. // Use different Odd/Even headers and footers and mirror margins
  700. $this->pdf->mirrorMargins = 1;
  701. // Add decoration only if not stated otherwise
  702. if ($complete) {
  703. // Adding watermark
  704. if (api_get_setting('pdf_export_watermark_enable') == 'true') {
  705. $watermark_file = self::get_watermark($course_code);
  706. if ($watermark_file) {
  707. //http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
  708. $this->pdf->SetWatermarkImage($watermark_file);
  709. $this->pdf->showWatermarkImage = true;
  710. } else {
  711. $watermark_file = self::get_watermark(null);
  712. if ($watermark_file) {
  713. $this->pdf->SetWatermarkImage($watermark_file);
  714. $this->pdf->showWatermarkImage = true;
  715. }
  716. }
  717. if ($course_code) {
  718. $watermark_text = api_get_course_setting('pdf_export_watermark_text');
  719. if (empty($watermark_text)) {
  720. $watermark_text = api_get_setting('pdf_export_watermark_text');
  721. }
  722. } else {
  723. $watermark_text = api_get_setting('pdf_export_watermark_text');
  724. }
  725. if (!empty($watermark_text)) {
  726. $this->pdf->SetWatermarkText(strcode2utf($watermark_text),0.1);
  727. $this->pdf->showWatermarkText = true;
  728. }
  729. }
  730. if (empty($this->custom_header)) {
  731. self::set_header($course_data);
  732. } else {
  733. $this->pdf->SetHTMLHeader($this->custom_header,'E');
  734. $this->pdf->SetHTMLHeader($this->custom_header,'O');
  735. }
  736. if (empty($this->custom_footer)) {
  737. self::set_footer();
  738. } else {
  739. $this->pdf->SetHTMLFooter($this->custom_footer);
  740. }
  741. }
  742. }
  743. }