pdf.lib.php 31 KB

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