pdf.lib.php 34 KB

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