pdf.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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('Institution');
  51. $img = api_get_path(SYS_CODE_PATH) . 'css/' . api_get_visual_theme() . '/images/header-logo.png';
  52. if (file_exists($img)) {
  53. $img = api_get_path(WEB_CODE_PATH) . 'css/' . 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->get_template('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_CODE_PATH) . 'css/' . 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_CODE_PATH) . 'css/' . 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('<link rel="stylesheet" http://my.chamilo.net/main/css/chamilo/frames.css type="text/css" />','', $document_html);
  257. $document_html = str_replace('../../', '', $document_html);
  258. $document_html = str_replace('../', '', $document_html);
  259. $document_html = str_replace('courses/' . $course_code . '/document/', '', $document_html);
  260. if (!empty($course_data['path'])) {
  261. $document_path = api_get_path(SYS_COURSE_PATH) . $course_data['path'] . '/document/';
  262. $doc = new DOMDocument();
  263. $result = @$doc->loadHTML($document_html);
  264. //Fixing only images @todo do the same thing with other elements
  265. $elements = $doc->getElementsByTagName('img');
  266. if (!empty($elements)) {
  267. foreach ($elements as $item) {
  268. $old_src = $item->getAttribute('src');
  269. //$old_src= str_replace('../','',$old_src);
  270. if (strpos($old_src, 'http') === false) {
  271. if (strpos($old_src, '/main/default_course_document') === false) {
  272. if (strpos($old_src, '/main/inc/lib/') === false) {
  273. $old_src_fixed = str_replace('/courses/' . $course_data['path'] . '/document/', '', $old_src);
  274. $old_src_fixed = str_replace('courses/' . $course_data['path'] . '/document/', '', $old_src_fixed);
  275. $new_path = $document_path . $old_src_fixed;
  276. $document_html = str_replace($old_src, $new_path, $document_html);
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. //replace relative path by absolute path for resources
  284. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  285. //$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply
  286. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  287. //$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
  288. //$document_html= str_replace('src="',$src_http_www, $document_html);
  289. //$document_html= str_replace('temp_template_path', 'src="/main/default_course_document/', $document_html);// restore src templates
  290. Text::api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  291. if (!empty($css)) {
  292. $this->pdf->WriteHTML($css, 1);
  293. }
  294. $this->pdf->WriteHTML($document_html, 2);
  295. if (empty($pdf_name)) {
  296. $output_file = 'pdf_' . date('Y-m-d-his') . '.pdf';
  297. } else {
  298. $pdf_name = api_replace_dangerous_char($pdf_name);
  299. $output_file = $pdf_name . '.pdf';
  300. }
  301. $result = $this->pdf->Output($output_file, 'D'); // F to save the pdf in a file
  302. exit;
  303. }
  304. /**
  305. * Gets the watermark from the platform or a course
  306. * @param string course code (optional)
  307. * @param mixed web path of the watermark image, false if there is nothing to return
  308. */
  309. public static function get_watermark($course_code = null)
  310. {
  311. $web_path = false;
  312. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  313. $course_info = api_get_course_info($course_code);
  314. $store_path = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/' . api_get_current_access_url_id() . '_pdf_watermark.png'; // course path
  315. if (file_exists($store_path)) {
  316. $web_path = api_get_path(WEB_COURSE_PATH) . $course_info['path'] . '/' . api_get_current_access_url_id() . '_pdf_watermark.png';
  317. }
  318. } else {
  319. $store_path = api_get_path(SYS_DEFAULT_COURSE_DOCUMENT_PATH).'images/' . api_get_current_access_url_id() . '_pdf_watermark.png'; // course path
  320. if (file_exists($store_path))
  321. $web_path = api_get_path(SYS_DEFAULT_COURSE_DOCUMENT_PATH).'images/' . api_get_current_access_url_id() . '_pdf_watermark.png';
  322. }
  323. return $web_path;
  324. }
  325. /**
  326. * Deletes the watermark from the Platform or Course
  327. * @param string course code (optional)
  328. * @param mixed web path of the watermark image, false if there is nothing to return
  329. */
  330. public function delete_watermark($course_code = null)
  331. {
  332. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  333. $course_info = api_get_course_info($course_code);
  334. $store_path = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/' . api_get_current_access_url_id() . '_pdf_watermark.png'; // course path
  335. } else {
  336. $store_path = api_get_path(SYS_DEFAULT_COURSE_DOCUMENT_PATH).'default_course_document/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  337. }
  338. if (file_exists($store_path)) {
  339. @unlink($store_path);
  340. return true;
  341. }
  342. return false;
  343. }
  344. /**
  345. * Uploads the pdf watermark in the main/default_course_document directory or in the course directory
  346. * @param string filename
  347. * @param string path of the file
  348. * @param string course code
  349. * @return mixed web path of the file if sucess, false otherwise
  350. */
  351. public function upload_watermark($filename, $source_file, $course_code = null)
  352. {
  353. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  354. $course_info = api_get_course_info($course_code);
  355. $store_path = api_get_path(SYS_COURSE_PATH) . $course_info['path']; // course path
  356. $web_path = api_get_path(WEB_COURSE_PATH) . $course_info['path'] . '/pdf_watermark.png';
  357. } else {
  358. $store_path = api_get_path(SYS_DEFAULT_COURSE_DOCUMENT_PATH).'images'; // course path
  359. $web_path = api_get_path(WEB_DEFAULT_COURSE_DOCUMENT_PATH).'images/' . api_get_current_access_url_id() . '_pdf_watermark.png';
  360. }
  361. $course_image = $store_path . '/' . api_get_current_access_url_id() . '_pdf_watermark.png';
  362. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  363. $result = false;
  364. if (file_exists($course_image)) {
  365. @unlink($course_image);
  366. }
  367. $my_image = new Image($source_file);
  368. $result = $my_image->send_image($course_image, -1, 'png');
  369. if ($result) {
  370. $result = $web_path;
  371. }
  372. return $result;
  373. }
  374. /**
  375. * Returns the default header
  376. */
  377. public function get_header($course_code = null)
  378. {
  379. /*$header = api_get_setting('pdf_export_watermark_text');
  380. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  381. $header = api_get_course_setting('pdf_export_watermark_text');
  382. }
  383. return $header;*/
  384. }
  385. public function set_footer()
  386. {
  387. $this->pdf->defaultfooterfontsize = 12; // in pts
  388. $this->pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
  389. $this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer
  390. $platform_name = api_get_setting('Institution');
  391. $left_content = $platform_name;
  392. $center_content = '';
  393. $right_content = '{PAGENO} / {nb}';
  394. //@todo remove this and use a simpler way
  395. $footer = array(
  396. 'odd' => array(
  397. 'L' => array(
  398. 'content' => $left_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  399. 'C' => array(
  400. 'content' => $center_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  401. 'R' => array(
  402. 'content' => $right_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  403. 'line' => 1,
  404. ),
  405. 'even' => array(
  406. 'L' => array(
  407. 'content' => $left_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  408. 'C' => array(
  409. 'content' => $center_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  410. 'R' => array(
  411. 'content' => $right_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  412. 'line' => 1,
  413. ),
  414. );
  415. // defines footer for Odd and Even Pages - placed at Outer margin see http://mpdf1.com/manual/index.php?tid=151&searchstring=setfooter
  416. $this->pdf->SetFooter($footer);
  417. }
  418. public function set_header($course_data)
  419. {
  420. $this->pdf->defaultheaderfontsize = 10; // in pts
  421. $this->pdf->defaultheaderfontstyle = BI; // blank, B, I, or BI
  422. $this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer
  423. if (!empty($course_data['code'])) {
  424. $teacher_list = CourseManager::get_teacher_list_from_course_code($course_data['code']);
  425. $teachers = '';
  426. if (!empty($teacher_list)) {
  427. foreach ($teacher_list as $teacher) {
  428. //$teachers[]= api_get_person_name($teacher['firstname'], $teacher['lastname']);
  429. $teachers[] = $teacher['firstname'] . ' ' . $teacher['lastname'];
  430. }
  431. if (count($teachers) > 1) {
  432. $teachers = get_lang('Teachers') . ': ' . implode(', ', $teachers);
  433. } else {
  434. $teachers = get_lang('Teacher') . ': ' . implode('', $teachers);
  435. }
  436. //do not show the teacher list see BT#4080 only the current teacher name
  437. $user_info = api_get_user_info();
  438. $teachers = $user_info['complete_name'];
  439. }
  440. $left_content = '';
  441. $center_content = '';
  442. $right_content = $teachers;
  443. $header = array(
  444. 'odd' => array(
  445. 'L' => array(
  446. 'content' => $left_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  447. 'C' => array(
  448. 'content' => $center_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  449. 'R' => array(
  450. 'content' => $right_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  451. 'line' => 1,
  452. ),
  453. 'even' => array(
  454. 'L' => array(
  455. 'content' => $left_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  456. 'C' => array(
  457. 'content' => $center_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  458. 'R' => array(
  459. 'content' => $right_content, 'font-size' => 10, 'font-style' => 'B', 'font-family' => 'serif', 'color' => '#000000'),
  460. 'line' => 1,
  461. ),
  462. );
  463. $this->pdf->SetHeader($header); // ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
  464. }
  465. }
  466. public function set_custom_header($header)
  467. {
  468. $this->custom_header = $header;
  469. }
  470. public function set_custom_footer($footer)
  471. {
  472. $this->custom_footer = $footer;
  473. }
  474. /**
  475. * Pre-formats a PDF to the right size and, if not stated otherwise, with
  476. * header, footer and watermark (if any)
  477. * @param array General course information (to fill headers)
  478. * @param bool Whether we want headers, footers and watermark or not
  479. */
  480. public function format_pdf($course_data, $complete = true)
  481. {
  482. if ($complete === false) {
  483. error_log('Asked with no decoration');
  484. }
  485. $course_code = null;
  486. if (!empty($course_data)) {
  487. $course_code = $course_data['code'];
  488. }
  489. /*$pdf->SetAuthor('Documents Chamilo');
  490. $pdf->SetTitle('title');
  491. $pdf->SetSubject('Exported from Chamilo Documents');
  492. $pdf->SetKeywords('Chamilo Documents');
  493. */
  494. $this->pdf->directionality = api_get_text_direction(); // TODO: To be read from the html document.
  495. $this->pdf->useOnlyCoreFonts = true;
  496. $this->pdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins
  497. // Add decoration only if not stated otherwise
  498. if ($complete) {
  499. //Adding watermark
  500. if (api_get_setting('pdf_export_watermark_enable') == 'true') {
  501. $watermark_file = self::get_watermark($course_code);
  502. if ($watermark_file) {
  503. //http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
  504. $this->pdf->SetWatermarkImage($watermark_file);
  505. $this->pdf->showWatermarkImage = true;
  506. } else {
  507. $watermark_file = self::get_watermark(null);
  508. if ($watermark_file) {
  509. $this->pdf->SetWatermarkImage($watermark_file);
  510. $this->pdf->showWatermarkImage = true;
  511. }
  512. }
  513. if ($course_code) {
  514. $watermark_text = api_get_course_setting('pdf_export_watermark_text');
  515. if (empty($watermark_text)) {
  516. $watermark_text = api_get_setting('pdf_export_watermark_text');
  517. }
  518. } else {
  519. $watermark_text = api_get_setting('pdf_export_watermark_text');
  520. }
  521. if (!empty($watermark_text)) {
  522. $this->pdf->SetWatermarkText(strcode2utf($watermark_text), 0.1);
  523. $this->pdf->showWatermarkText = true;
  524. }
  525. }
  526. if (empty($this->custom_header)) {
  527. self::set_header($course_data);
  528. } else {
  529. $this->pdf->SetHTMLHeader($this->custom_header, 'E');
  530. $this->pdf->SetHTMLHeader($this->custom_header, 'O');
  531. }
  532. if (empty($this->custom_footer)) {
  533. self::set_footer();
  534. } else {
  535. $this->pdf->SetHTMLFooter($this->custom_footer);
  536. }
  537. }
  538. }
  539. }