pdf.lib.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * @package chamilo.library
  5. */
  6. /**
  7. * Code
  8. */
  9. define('_MPDF_PATH', api_get_path(LIBRARY_PATH).'mpdf/');
  10. require_once _MPDF_PATH.'mpdf.php';
  11. class PDF {
  12. public $pdf;
  13. public $custom_header = '';
  14. public $custom_footer = '';
  15. public $params = array();
  16. /**
  17. * Creates the mPDF object
  18. * @param string format A4 A4-L see http://mpdf1.com/manual/index.php?tid=184&searchstring=format
  19. * @param string orientation "P" = Portrait "L" = Landscape
  20. */
  21. public function __construct($page_format ='A4', $orientation = 'P', $params = array()) {
  22. /* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=mPDF
  23. * 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 ]]]]]])
  24. */
  25. if (!in_array($orientation, array('P','L'))) {
  26. $orientation = 'P';
  27. }
  28. //$this->pdf = $pdf = new mPDF('UTF-8', $page_format, '', '', 30, 20, 27, 25, 16, 13, $orientation);
  29. //left, right, top, bottom, margin_header, margin footer
  30. $params['left'] = isset($params['left']) ? $params['left'] : 15;
  31. $params['right'] = isset($params['right']) ? $params['right'] : 15;
  32. $params['top'] = isset($params['top']) ? $params['top'] : 20;
  33. $params['bottom'] = isset($params['bottom']) ? $params['bottom'] : 15;
  34. $this->params['filename'] = isset($params['filename']) ? $params['filename'] : api_get_local_time();
  35. $this->params['pdf_title'] = isset($params['pdf_title']) ? $params['pdf_title'] : get_lang('Untitled');
  36. $this->params['course_code'] = isset($params['course_code']) ? $params['course_code'] : api_get_course_id();
  37. $this->params['add_signatures'] = isset($params['add_signatures']) ? $params['add_signatures'] : false;
  38. $this->pdf = new mPDF('UTF-8', $page_format, '', '', $params['left'], $params['right'], $params['top'], $params['bottom'], 8, 8, $orientation);
  39. }
  40. function html_to_pdf_with_template($content) {
  41. Display :: display_no_header();
  42. //Assignments
  43. Display::$global_template->assign('pdf_content', $content);
  44. $organization = api_get_setting('Institution');
  45. $img = api_get_path(SYS_CODE_PATH).'css/'.api_get_visual_theme().'/images/header-logo.png';
  46. if (file_exists($img)) {
  47. $img = api_get_path(WEB_CODE_PATH).'css/'.api_get_visual_theme().'/images/header-logo.png';
  48. $organization = "<img src='$img'>";
  49. } else {
  50. if (!empty($organization)) {
  51. $organization = '<h2 align="left">'.$organization.'</h2>';
  52. }
  53. }
  54. Display::$global_template->assign('organization', $organization);
  55. //Showing only the current teacher/admin instead the all teacherlist name see BT#4080
  56. //$teacher_list = CourseManager::get_teacher_list_from_course_code_to_string($course_code);
  57. $user_info = api_get_user_info();
  58. $teacher_list = $user_info['complete_name'];
  59. $session_name = api_get_session_name(api_get_session_id());
  60. if (!empty($session_name)) {
  61. Display::$global_template->assign('pdf_session', $session_name);
  62. }
  63. Display::$global_template->assign('pdf_course', $this->params['course_code']);
  64. Display::$global_template->assign('pdf_date', api_format_date(api_get_utc_datetime(), DATE_TIME_FORMAT_LONG));
  65. Display::$global_template->assign('pdf_teachers', $teacher_list);
  66. Display::$global_template->assign('pdf_title', $this->params['pdf_title']);
  67. Display::$global_template->assign('add_signatures', $this->params['add_signatures']);
  68. //Getting template
  69. $tpl = Display::$global_template->get_template('export/table_pdf.tpl');
  70. $html = Display::$global_template->fetch($tpl);
  71. $html = api_utf8_encode($html);
  72. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).'/print.css';
  73. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  74. self::content_to_pdf($html, $css, $this->params['filename'], $this->params['course_code']);
  75. }
  76. /**
  77. * Converts HTML files to PDF
  78. * @param mixed could be an html file path or an array with paths example: /var/www/myfile.html or array('/myfile.html','myotherfile.html') or even an indexed array with both 'title' and 'path' indexes for each element like array(0=>array('title'=>'Hello','path'=>'file.html'),1=>array('title'=>'Bye','path'=>'file2.html'));
  79. * @param string pdf name
  80. * @param string course code (if you are using html that are located in the document tool you must provide this)
  81. * @return void
  82. */
  83. public function html_to_pdf($html_file_array, $pdf_name = '', $course_code = null, $print_title = false) {
  84. if (empty($html_file_array)) {
  85. return false;
  86. }
  87. if (is_array($html_file_array)) {
  88. if (count($html_file_array) == 0)
  89. return false;
  90. } else {
  91. if (!file_exists($html_file_array)) {
  92. return false;
  93. }
  94. //Converting the string into an array
  95. $html_file_array = array($html_file_array);
  96. }
  97. $course_data = array();
  98. if (!empty($course_code)) {
  99. $course_data = api_get_course_info($course_code);
  100. } else {
  101. $course_data = api_get_course_info();
  102. }
  103. //clean styles and javascript document
  104. $clean_search = array (
  105. '@<script[^>]*?>.*?</script>@si',
  106. '@<style[^>]*?>.*?</style>@si'
  107. );
  108. //Formatting the pdf
  109. self::format_pdf($course_data);
  110. $counter = 1;
  111. foreach ($html_file_array as $file) {
  112. //Add a page break per file
  113. $page_break = '<pagebreak>';
  114. if ($counter == count($html_file_array)) {
  115. $page_break = '';
  116. }
  117. $counter++;
  118. $html_title = '';
  119. //if the array provided contained subarrays with 'title' entry,
  120. // then print the title in the PDF
  121. if (is_array($file) && isset($file['title'])) {
  122. $html_title = $file['title'];
  123. $file = $file['path'];
  124. } else {
  125. //we suppose we've only been sent a file path
  126. $html_title = basename($file);
  127. }
  128. if (empty($file) && !empty($html_title)) {
  129. //this is a chapter, print title & skip the rest
  130. if ($print_title) {
  131. $this->pdf->WriteHTML('<html><body><h3>'.$html_title.'</h3></body></html>'.$page_break, 2);
  132. }
  133. continue;
  134. }
  135. if (!file_exists($file)) {
  136. //the file doesn't exist, skip
  137. continue;
  138. }
  139. //it's not a chapter but the file exists, print its title
  140. if ($print_title) {
  141. $this->pdf->WriteHTML('<html><body><h3>'.$html_title.'</h3></body></html>',2);
  142. }
  143. $file_info = pathinfo($file);
  144. $extension = $file_info['extension'];
  145. if (in_array($extension, array('html', 'htm'))) {
  146. $filename = $file_info['basename'];
  147. $filename = str_replace('_',' ',$filename);
  148. if ($extension == 'html') {
  149. $filename = basename($filename,'.html');
  150. } elseif($extension == 'htm'){
  151. $filename = basename($filename,'.htm');
  152. }
  153. $document_html = @file_get_contents($file);
  154. $document_html = preg_replace($clean_search, '', $document_html);
  155. //absolute path for frames.css //TODO: necessary?
  156. $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
  157. $document_html = str_replace('href="./css/frames.css"', $absolute_css_path, $document_html);
  158. if (!empty($course_data['path'])) {
  159. $document_html= str_replace('../','',$document_html);
  160. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  161. $doc = new DOMDocument();
  162. $result = @$doc->loadHTML($document_html);
  163. //Fixing only images @todo do the same thing with other elements
  164. $elements = $doc->getElementsByTagName('img');
  165. if (!empty($elements)) {
  166. foreach ($elements as $item) {
  167. $old_src = $item->getAttribute('src');
  168. if (strpos($old_src, 'http') === false) {
  169. if (strpos($old_src, '/main/default_course_document') === false) {
  170. if (api_get_path(REL_PATH) != '/') {
  171. $old_src = str_replace(api_get_path(REL_PATH), '', $old_src);
  172. }
  173. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src);
  174. $new_path = $document_path.$old_src_fixed;
  175. $document_html= str_replace($old_src, $new_path, $document_html);
  176. }
  177. } else {
  178. //Check if this is a complete URL
  179. /*if (strpos($old_src, 'courses/'.$course_data['path'].'/document/') === false) {
  180. } else {
  181. $old_src_fixed = str_replace(api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/', '', $old_src);
  182. $new_path = $document_path.$old_src_fixed;
  183. $document_html= str_replace($old_src, $new_path, $document_html);
  184. }*/
  185. }
  186. }
  187. }
  188. }
  189. api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  190. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through
  191. // $_GET[] too, as it is done with file name.
  192. // At the moment the title is retrieved from the html document itself.
  193. //echo $document_html;exit;
  194. if (empty($title)) {
  195. $title = $filename; // Here file name is expected to contain ASCII symbols only.
  196. }
  197. if (!empty($document_html)) {
  198. $this->pdf->WriteHTML($document_html.$page_break, 2);
  199. }
  200. } elseif (in_array($extension, array('jpg','jpeg','png','gif'))) {
  201. //Images
  202. $image = Display::img($file);
  203. $this->pdf->WriteHTML('<html><body>'.$image.'</body></html>'.$page_break,2);
  204. }
  205. }
  206. if (empty($pdf_name)) {
  207. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  208. } else {
  209. $pdf_name = replace_dangerous_char($pdf_name);
  210. $output_file = $pdf_name.'.pdf';
  211. }
  212. $result = $this->pdf->Output($output_file, 'D'); /// F to save the pdf in a file
  213. exit;
  214. }
  215. /**
  216. * Converts an html string to PDF
  217. * @param string valid html
  218. * @param string CSS content of a CSS file
  219. * @param string pdf name
  220. * @param string course code (if you are using html that are located in the document tool you must provide this)
  221. * @return string Web path
  222. */
  223. public function content_to_pdf($document_html, $css = '', $pdf_name = '', $course_code = null) {
  224. if (empty($document_html)) {
  225. return false;
  226. }
  227. //clean styles and javascript document
  228. $clean_search = array (
  229. '@<script[^>]*?>.*?</script>@si',
  230. '@<style[^>]*?>.*?</style>@siU'
  231. );
  232. //Formatting the pdf
  233. $course_data = api_get_course_info($course_code);
  234. self::format_pdf($course_data);
  235. $document_html = preg_replace($clean_search, '', $document_html);
  236. //absolute path for frames.css //TODO: necessary?
  237. $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
  238. $document_html = str_replace('href="./css/frames.css"','href="'.$absolute_css_path.'"', $document_html);
  239. //$document_html=str_replace('<link rel="stylesheet" http://my.chamilo.net/main/css/chamilo/frames.css type="text/css" />','', $document_html);
  240. $document_html= str_replace('../../','',$document_html);
  241. $document_html= str_replace('../','',$document_html);
  242. $document_html= str_replace('courses/'.$course_code.'/document/','',$document_html);
  243. if (!empty($course_data['path'])) {
  244. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  245. $doc = new DOMDocument();
  246. $result = @$doc->loadHTML($document_html);
  247. //Fixing only images @todo do the same thing with other elements
  248. $elements = $doc->getElementsByTagName('img');
  249. if (!empty($elements)) {
  250. foreach ($elements as $item) {
  251. $old_src = $item->getAttribute('src');
  252. //$old_src= str_replace('../','',$old_src);
  253. if (strpos($old_src, 'http') === false) {
  254. if (strpos($old_src, '/main/default_course_document') === false) {
  255. if (strpos($old_src, '/main/inc/lib/') === false) {
  256. $old_src_fixed = str_replace('/courses/'.$course_data['path'].'/document/', '', $old_src);
  257. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed);
  258. $new_path = $document_path.$old_src_fixed;
  259. $document_html= str_replace($old_src, $new_path, $document_html);
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. //replace relative path by absolute path for resources
  267. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  268. //$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply
  269. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  270. //$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
  271. //$document_html= str_replace('src="',$src_http_www, $document_html);
  272. //$document_html= str_replace('temp_template_path', 'src="/main/default_course_document/', $document_html);// restore src templates
  273. api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  274. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through
  275. // $_GET[] too, as it is done with file name.
  276. // At the moment the title is retrieved from the html document itself.
  277. /* if (empty($title)) {
  278. $title = $filename; // Here file name is expected to contain ASCII symbols only.
  279. }*/
  280. if (!empty($css)) {
  281. $this->pdf->WriteHTML($css, 1);
  282. }
  283. $this->pdf->WriteHTML($document_html,2);
  284. if (empty($pdf_name)) {
  285. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  286. } else {
  287. $pdf_name = replace_dangerous_char($pdf_name);
  288. $output_file = $pdf_name.'.pdf';
  289. }
  290. $result = $this->pdf->Output($output_file, 'D'); // F to save the pdf in a file
  291. exit;
  292. }
  293. /**
  294. * Gets the watermark from the platform or a course
  295. * @param string course code (optional)
  296. * @param mixed web path of the watermark image, false if there is nothing to return
  297. */
  298. public static function get_watermark($course_code = null) {
  299. $web_path = false;
  300. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  301. $course_info = api_get_course_info($course_code);
  302. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  303. if (file_exists($store_path)) {
  304. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  305. }
  306. } else {
  307. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  308. if (file_exists($store_path))
  309. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  310. }
  311. return $web_path;
  312. }
  313. /**
  314. * Deletes the watermark from the Platform or Course
  315. * @param string course code (optional)
  316. * @param mixed web path of the watermark image, false if there is nothing to return
  317. */
  318. public function delete_watermark($course_code = null) {
  319. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  320. $course_info = api_get_course_info($course_code);
  321. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  322. } else {
  323. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  324. }
  325. if (file_exists($store_path)) {
  326. @unlink($store_path);
  327. return true;
  328. }
  329. return false;
  330. }
  331. /**
  332. * Uploads the pdf watermark in the main/default_course_document directory or in the course directory
  333. * @param string filename
  334. * @param string path of the file
  335. * @param string course code
  336. * @return mixed web path of the file if sucess, false otherwise
  337. */
  338. public function upload_watermark($filename, $source_file, $course_code = null) {
  339. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  340. $course_info = api_get_course_info($course_code);
  341. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path']; // course path
  342. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/pdf_watermark.png';
  343. } else {
  344. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images'; // course path
  345. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  346. }
  347. $course_image = $store_path.'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  348. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  349. $result = false;
  350. if (file_exists($course_image)) {
  351. @unlink($course_image);
  352. }
  353. $my_image = new Image($source_file);
  354. $result = $my_image->send_image($course_image, -1, 'png');
  355. if ($result) {
  356. $result = $web_path;
  357. }
  358. return $result;
  359. }
  360. /**
  361. * Returns the default header
  362. */
  363. public function get_header($course_code = null) {
  364. /*$header = api_get_setting('pdf_export_watermark_text');
  365. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  366. $header = api_get_course_setting('pdf_export_watermark_text');
  367. }
  368. return $header;*/
  369. }
  370. public function set_footer() {
  371. $this->pdf->defaultfooterfontsize = 12; // in pts
  372. $this->pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
  373. $this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer
  374. $platform_name = api_get_setting('Institution');
  375. $left_content = $platform_name;
  376. $center_content = '';
  377. $right_content = '{PAGENO} / {nb}';
  378. //@todo remove this and use a simpler way
  379. $footer = array (
  380. 'odd' => array (
  381. 'L' => array (
  382. 'content' => $left_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  383. 'C' => array (
  384. 'content' => $center_content,'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  385. 'R' => array (
  386. 'content' => $right_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  387. 'line' => 1,
  388. ),
  389. 'even' => array (
  390. 'L' => array (
  391. 'content' => $left_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  392. 'C' => array (
  393. 'content' => $center_content,'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  394. 'R' => array (
  395. 'content' => $right_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  396. 'line' => 1,
  397. ),
  398. );
  399. // defines footer for Odd and Even Pages - placed at Outer margin see http://mpdf1.com/manual/index.php?tid=151&searchstring=setfooter
  400. $this->pdf->SetFooter($footer);
  401. }
  402. public function set_header($course_data) {
  403. $this->pdf->defaultheaderfontsize = 10; // in pts
  404. $this->pdf->defaultheaderfontstyle = BI; // blank, B, I, or BI
  405. $this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer
  406. if (!empty($course_data['code'])) {
  407. $teacher_list = CourseManager::get_teacher_list_from_course_code($course_data['code']);
  408. $teachers = '';
  409. if (!empty($teacher_list)) {
  410. foreach ($teacher_list as $teacher) {
  411. //$teachers[]= api_get_person_name($teacher['firstname'], $teacher['lastname']);
  412. $teachers[]= $teacher['firstname'].' '.$teacher['lastname'];
  413. }
  414. if (count($teachers) > 1) {
  415. $teachers = get_lang('Teachers').': '.implode(', ', $teachers);
  416. } else {
  417. $teachers = get_lang('Teacher').': '.implode('', $teachers);
  418. }
  419. //do not show the teacher list see BT#4080 only the current teacher name
  420. $user_info = api_get_user_info();
  421. $teachers = $user_info['complete_name'];
  422. }
  423. $left_content = '';
  424. $center_content = '';
  425. $right_content = $teachers;
  426. $header = array (
  427. 'odd' => array (
  428. 'L' => array (
  429. 'content' => $left_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  430. 'C' => array (
  431. 'content' => $center_content,'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  432. 'R' => array (
  433. 'content' => $right_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  434. 'line' => 1,
  435. ),
  436. 'even' => array (
  437. 'L' => array (
  438. 'content' => $left_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  439. 'C' => array (
  440. 'content' => $center_content,'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  441. 'R' => array (
  442. 'content' => $right_content, 'font-size' => 10,'font-style' => 'B','font-family' => 'serif','color'=>'#000000'),
  443. 'line' => 1,
  444. ),
  445. );
  446. $this->pdf->SetHeader($header);// ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
  447. }
  448. }
  449. public function set_custom_header($header) {
  450. $this->custom_header = $header;
  451. }
  452. public function set_custom_footer($footer) {
  453. $this->custom_footer = $footer;
  454. }
  455. public function format_pdf($course_data) {
  456. $course_code = null;
  457. if (!empty( $course_data)) {
  458. $course_code = $course_data['code'];
  459. }
  460. /*$pdf->SetAuthor('Documents Chamilo');
  461. $pdf->SetTitle('title');
  462. $pdf->SetSubject('Exported from Chamilo Documents');
  463. $pdf->SetKeywords('Chamilo Documents');
  464. */
  465. $this->pdf->directionality = api_get_text_direction(); // TODO: To be read from the html document.
  466. $this->pdf->useOnlyCoreFonts = true;
  467. $this->pdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins
  468. //Adding watermark
  469. if (api_get_setting('pdf_export_watermark_enable') == 'true') {
  470. $watermark_file = self::get_watermark($course_code);
  471. if ($watermark_file) {
  472. //http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
  473. $this->pdf->SetWatermarkImage($watermark_file);
  474. $this->pdf->showWatermarkImage = true;
  475. } else {
  476. $watermark_file = self::get_watermark(null);
  477. if ($watermark_file) {
  478. $this->pdf->SetWatermarkImage($watermark_file);
  479. $this->pdf->showWatermarkImage = true;
  480. }
  481. }
  482. if ($course_code) {
  483. $watermark_text = api_get_course_setting('pdf_export_watermark_text');
  484. if (empty($watermark_text)) {
  485. $watermark_text = api_get_setting('pdf_export_watermark_text');
  486. }
  487. } else {
  488. $watermark_text = api_get_setting('pdf_export_watermark_text');
  489. }
  490. if (!empty($watermark_text)) {
  491. $this->pdf->SetWatermarkText(strcode2utf($watermark_text),0.1);
  492. $this->pdf->showWatermarkText = true;
  493. }
  494. }
  495. if (empty($this->custom_header)) {
  496. self::set_header($course_data);
  497. } else {
  498. $this->pdf->SetHTMLHeader($this->custom_header,'E');
  499. $this->pdf->SetHTMLHeader($this->custom_header,'O');
  500. }
  501. if (empty($this->custom_footer)) {
  502. self::set_footer();
  503. } else {
  504. $this->pdf->SetHTMLFooter($this->custom_footer);
  505. }
  506. }
  507. }