pdf.lib.php 29 KB

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