pdf.lib.php 32 KB

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