pdf.lib.php 27 KB

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