pdf.lib.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * @package chamilo.library
  5. */
  6. /**
  7. * Code
  8. */
  9. class PDF {
  10. public $pdf;
  11. public $custom_header = '';
  12. public $custom_footer = '';
  13. public $params = array();
  14. /**
  15. * Creates the mPDF object
  16. * @param string format A4 A4-L see http://mpdf1.com/manual/index.php?tid=184&searchstring=format
  17. * @param string orientation "P" = Portrait "L" = Landscape
  18. */
  19. public function __construct($page_format ='A4', $orientation = 'P', $params = array()) {
  20. /* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=mPDF
  21. * 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 ]]]]]])
  22. */
  23. if (!in_array($orientation, array('P','L'))) {
  24. $orientation = 'P';
  25. }
  26. //$this->pdf = $pdf = new mPDF('UTF-8', $page_format, '', '', 30, 20, 27, 25, 16, 13, $orientation);
  27. //left, right, top, bottom, margin_header, margin footer
  28. $params['left'] = isset($params['left']) ? $params['left'] : 15;
  29. $params['right'] = isset($params['right']) ? $params['right'] : 15;
  30. $params['top'] = isset($params['top']) ? $params['top'] : 20;
  31. $params['bottom'] = isset($params['bottom']) ? $params['bottom'] : 15;
  32. $this->params['filename'] = isset($params['filename']) ? $params['filename'] : api_get_local_time();
  33. $this->params['pdf_title'] = isset($params['pdf_title']) ? $params['pdf_title'] : get_lang('Untitled');
  34. $this->params['course_code'] = isset($params['course_code']) ? $params['course_code'] : api_get_course_id();
  35. $this->params['add_signatures'] = isset($params['add_signatures']) ? $params['add_signatures'] : false;
  36. $this->pdf = new mPDF('UTF-8', $page_format, '', '', $params['left'], $params['right'], $params['top'], $params['bottom'], 8, 8, $orientation);
  37. }
  38. /**
  39. * Export the given HTML to PDF, using a global template
  40. * @param string the HTML content
  41. * @uses export/table_pdf.tpl
  42. */
  43. function html_to_pdf_with_template($content) {
  44. Display :: display_no_header();
  45. //Assignments
  46. Display::$global_template->assign('pdf_content', $content);
  47. $organization = api_get_setting('Institution');
  48. $img = api_get_path(SYS_CODE_PATH).'css/'.api_get_visual_theme().'/images/header-logo.png';
  49. if (file_exists($img)) {
  50. $img = api_get_path(WEB_CODE_PATH).'css/'.api_get_visual_theme().'/images/header-logo.png';
  51. $organization = "<img src='$img'>";
  52. } else {
  53. if (!empty($organization)) {
  54. $organization = '<h2 align="left">'.$organization.'</h2>';
  55. }
  56. }
  57. Display::$global_template->assign('organization', $organization);
  58. //Showing only the current teacher/admin instead the all teacherlist name see BT#4080
  59. $user_info = api_get_user_info();
  60. $teacher_list = $user_info['complete_name'];
  61. $session_name = api_get_session_name(api_get_session_id());
  62. if (!empty($session_name)) {
  63. Display::$global_template->assign('pdf_session', $session_name);
  64. }
  65. Display::$global_template->assign('pdf_course', $this->params['course_code']);
  66. Display::$global_template->assign('pdf_date', api_format_date(api_get_utc_datetime(), DATE_TIME_FORMAT_LONG));
  67. Display::$global_template->assign('pdf_teachers', $teacher_list);
  68. Display::$global_template->assign('pdf_title', $this->params['pdf_title']);
  69. Display::$global_template->assign('add_signatures', $this->params['add_signatures']);
  70. //Getting template
  71. $tpl = Display::$global_template->get_template('export/table_pdf.tpl');
  72. $html = Display::$global_template->fetch($tpl);
  73. $html = api_utf8_encode($html);
  74. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).'/print.css';
  75. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  76. self::content_to_pdf($html, $css, $this->params['filename'], $this->params['course_code']);
  77. }
  78. /**
  79. * Converts HTML files to PDF
  80. * @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'));
  81. * @param string pdf name
  82. * @param string course code (if you are using html that are located in the document tool you must provide this)
  83. * @param bool Whether to print the header, footer and watermark (true) or just the content (false)
  84. * @return void
  85. */
  86. public function html_to_pdf($html_file_array, $pdf_name = '', $course_code = null, $print_title = false, $complete_style = true) {
  87. if ($complete_style === false) { error_log(__FUNCTION__.' with no style'); }
  88. if (empty($html_file_array)) {
  89. return false;
  90. }
  91. if (is_array($html_file_array)) {
  92. if (count($html_file_array) == 0)
  93. return false;
  94. } else {
  95. if (!file_exists($html_file_array)) {
  96. return false;
  97. }
  98. //Converting the string into an array
  99. $html_file_array = array($html_file_array);
  100. }
  101. $course_data = array();
  102. if (!empty($course_code)) {
  103. $course_data = api_get_course_info($course_code);
  104. } else {
  105. $course_data = api_get_course_info();
  106. }
  107. //clean styles and javascript document
  108. $clean_search = array (
  109. '@<script[^>]*?>.*?</script>@si',
  110. '@<style[^>]*?>.*?</style>@si'
  111. );
  112. //Formatting the pdf
  113. self::format_pdf($course_data, $complete_style);
  114. $counter = 1;
  115. foreach ($html_file_array as $file) {
  116. //Add a page break per file
  117. $page_break = '<pagebreak>';
  118. if ($counter == count($html_file_array)) {
  119. $page_break = '';
  120. }
  121. $counter++;
  122. $html_title = '';
  123. //if the array provided contained subarrays with 'title' entry,
  124. // then print the title in the PDF
  125. if (is_array($file) && isset($file['title'])) {
  126. $html_title = $file['title'];
  127. $file = $file['path'];
  128. } else {
  129. //we suppose we've only been sent a file path
  130. $html_title = basename($file);
  131. }
  132. if (empty($file) && !empty($html_title)) {
  133. //this is a chapter, print title & skip the rest
  134. if ($print_title) {
  135. $this->pdf->WriteHTML('<html><body><h3>'.$html_title.'</h3></body></html>'.$page_break, 2);
  136. }
  137. continue;
  138. }
  139. if (!file_exists($file)) {
  140. //the file doesn't exist, skip
  141. continue;
  142. }
  143. //it's not a chapter but the file exists, print its title
  144. if ($print_title) {
  145. $this->pdf->WriteHTML('<html><body><h3>'.$html_title.'</h3></body></html>',2);
  146. }
  147. $file_info = pathinfo($file);
  148. $extension = $file_info['extension'];
  149. if (in_array($extension, array('html', 'htm'))) {
  150. $filename = $file_info['basename'];
  151. $filename = str_replace('_',' ',$filename);
  152. if ($extension == 'html') {
  153. $filename = basename($filename,'.html');
  154. } elseif($extension == 'htm'){
  155. $filename = basename($filename,'.htm');
  156. }
  157. $document_html = @file_get_contents($file);
  158. $document_html = preg_replace($clean_search, '', $document_html);
  159. //absolute path for frames.css //TODO: necessary?
  160. $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
  161. $document_html = str_replace('href="./css/frames.css"', $absolute_css_path, $document_html);
  162. if (!empty($course_data['path'])) {
  163. $document_html= str_replace('../','',$document_html);
  164. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  165. $doc = new DOMDocument();
  166. $result = @$doc->loadHTML($document_html);
  167. //Fixing only images @todo do the same thing with other elements
  168. $elements = $doc->getElementsByTagName('img');
  169. if (!empty($elements)) {
  170. foreach ($elements as $item) {
  171. $old_src = $item->getAttribute('src');
  172. if (strpos($old_src, 'http') === false) {
  173. if (strpos($old_src, '/main/default_course_document') === false) {
  174. $old_src_fixed = '';
  175. if (api_get_path(REL_PATH) != '/') {
  176. $old_src_fixed = str_replace(api_get_path(REL_PATH).'courses/'.$course_data['path'].'/document/', '', $old_src);
  177. } else {
  178. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src);
  179. }
  180. $new_path = $document_path.$old_src_fixed;
  181. $document_html= str_replace($old_src, $new_path, $document_html);
  182. }
  183. } else {
  184. //Check if this is a complete URL
  185. /*if (strpos($old_src, 'courses/'.$course_data['path'].'/document/') === false) {
  186. } else {
  187. $old_src_fixed = str_replace(api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/', '', $old_src);
  188. $new_path = $document_path.$old_src_fixed;
  189. $document_html= str_replace($old_src, $new_path, $document_html);
  190. }*/
  191. }
  192. }
  193. }
  194. }
  195. Text::api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  196. $title = Text::api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through
  197. // $_GET[] too, as it is done with file name.
  198. // At the moment the title is retrieved from the html document itself.
  199. //echo $document_html;exit;
  200. if (empty($title)) {
  201. $title = $filename; // Here file name is expected to contain ASCII symbols only.
  202. }
  203. if (!empty($document_html)) {
  204. $this->pdf->WriteHTML($document_html.$page_break, 2);
  205. }
  206. } elseif (in_array($extension, array('jpg','jpeg','png','gif'))) {
  207. //Images
  208. $image = Display::img($file);
  209. $this->pdf->WriteHTML('<html><body>'.$image.'</body></html>'.$page_break,2);
  210. }
  211. }
  212. if (empty($pdf_name)) {
  213. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  214. } else {
  215. $pdf_name = api_replace_dangerous_char($pdf_name);
  216. $output_file = $pdf_name.'.pdf';
  217. }
  218. $result = $this->pdf->Output($output_file, 'D'); /// F to save the pdf in a file
  219. exit;
  220. }
  221. /**
  222. * Converts an html string to PDF
  223. * @param string valid html
  224. * @param string CSS content of a CSS file
  225. * @param string pdf name
  226. * @param string course code (if you are using html that are located in the document tool you must provide this)
  227. * @return string Web path
  228. */
  229. public function content_to_pdf($document_html, $css = '', $pdf_name = '', $course_code = null) {
  230. if (empty($document_html)) {
  231. return false;
  232. }
  233. //clean styles and javascript document
  234. $clean_search = array (
  235. '@<script[^>]*?>.*?</script>@si',
  236. '@<style[^>]*?>.*?</style>@siU'
  237. );
  238. //Formatting the pdf
  239. $course_data = api_get_course_info($course_code);
  240. self::format_pdf($course_data);
  241. $document_html = preg_replace($clean_search, '', $document_html);
  242. //absolute path for frames.css //TODO: necessary?
  243. $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
  244. $document_html = str_replace('href="./css/frames.css"','href="'.$absolute_css_path.'"', $document_html);
  245. //$document_html=str_replace('<link rel="stylesheet" http://my.chamilo.net/main/css/chamilo/frames.css type="text/css" />','', $document_html);
  246. $document_html= str_replace('../../','',$document_html);
  247. $document_html= str_replace('../','',$document_html);
  248. $document_html= str_replace('courses/'.$course_code.'/document/','',$document_html);
  249. if (!empty($course_data['path'])) {
  250. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  251. $doc = new DOMDocument();
  252. $result = @$doc->loadHTML($document_html);
  253. //Fixing only images @todo do the same thing with other elements
  254. $elements = $doc->getElementsByTagName('img');
  255. if (!empty($elements)) {
  256. foreach ($elements as $item) {
  257. $old_src = $item->getAttribute('src');
  258. //$old_src= str_replace('../','',$old_src);
  259. if (strpos($old_src, 'http') === false) {
  260. if (strpos($old_src, '/main/default_course_document') === false) {
  261. if (strpos($old_src, '/main/inc/lib/') === false) {
  262. $old_src_fixed = str_replace('/courses/'.$course_data['path'].'/document/', '', $old_src);
  263. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed);
  264. $new_path = $document_path.$old_src_fixed;
  265. $document_html= str_replace($old_src, $new_path, $document_html);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. //replace relative path by absolute path for resources
  273. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  274. //$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply
  275. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  276. //$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
  277. //$document_html= str_replace('src="',$src_http_www, $document_html);
  278. //$document_html= str_replace('temp_template_path', 'src="/main/default_course_document/', $document_html);// restore src templates
  279. Text::api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  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 = api_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. /**
  456. * Pre-formats a PDF to the right size and, if not stated otherwise, with
  457. * header, footer and watermark (if any)
  458. * @param array General course information (to fill headers)
  459. * @param bool Whether we want headers, footers and watermark or not
  460. */
  461. public function format_pdf($course_data, $complete = true) {
  462. if($complete === false) {error_log('Asked with no decoration');}
  463. $course_code = null;
  464. if (!empty( $course_data)) {
  465. $course_code = $course_data['code'];
  466. }
  467. /*$pdf->SetAuthor('Documents Chamilo');
  468. $pdf->SetTitle('title');
  469. $pdf->SetSubject('Exported from Chamilo Documents');
  470. $pdf->SetKeywords('Chamilo Documents');
  471. */
  472. $this->pdf->directionality = api_get_text_direction(); // TODO: To be read from the html document.
  473. $this->pdf->useOnlyCoreFonts = true;
  474. $this->pdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins
  475. // Add decoration only if not stated otherwise
  476. if ($complete) {
  477. //Adding watermark
  478. if (api_get_setting('pdf_export_watermark_enable') == 'true') {
  479. $watermark_file = self::get_watermark($course_code);
  480. if ($watermark_file) {
  481. //http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
  482. $this->pdf->SetWatermarkImage($watermark_file);
  483. $this->pdf->showWatermarkImage = true;
  484. } else {
  485. $watermark_file = self::get_watermark(null);
  486. if ($watermark_file) {
  487. $this->pdf->SetWatermarkImage($watermark_file);
  488. $this->pdf->showWatermarkImage = true;
  489. }
  490. }
  491. if ($course_code) {
  492. $watermark_text = api_get_course_setting('pdf_export_watermark_text');
  493. if (empty($watermark_text)) {
  494. $watermark_text = api_get_setting('pdf_export_watermark_text');
  495. }
  496. } else {
  497. $watermark_text = api_get_setting('pdf_export_watermark_text');
  498. }
  499. if (!empty($watermark_text)) {
  500. $this->pdf->SetWatermarkText(strcode2utf($watermark_text),0.1);
  501. $this->pdf->showWatermarkText = true;
  502. }
  503. }
  504. if (empty($this->custom_header)) {
  505. self::set_header($course_data);
  506. } else {
  507. $this->pdf->SetHTMLHeader($this->custom_header,'E');
  508. $this->pdf->SetHTMLHeader($this->custom_header,'O');
  509. }
  510. if (empty($this->custom_footer)) {
  511. self::set_footer();
  512. } else {
  513. $this->pdf->SetHTMLFooter($this->custom_footer);
  514. }
  515. }
  516. }
  517. }