pdf.lib.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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 = '';
  12. public $custom_footer = '';
  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(TO_SYS, WEB_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(TO_SYS, WEB_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. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src);
  289. }
  290. $new_path = $document_path.$old_src_fixed;
  291. } else {
  292. $new_path = $old_src;
  293. }
  294. $document_html= str_replace($old_src, $new_path, $document_html);
  295. }
  296. } else {
  297. //Check if this is a complete URL
  298. /*if (strpos($old_src, 'courses/'.$course_data['path'].'/document/') === false) {
  299. } else {
  300. $old_src_fixed = str_replace(api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/', '', $old_src);
  301. $new_path = $document_path.$old_src_fixed;
  302. $document_html= str_replace($old_src, $new_path, $document_html);
  303. }*/
  304. }
  305. }
  306. }
  307. }
  308. api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  309. // TODO: Maybe it is better idea the title to be passed through
  310. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');
  311. // $_GET[] too, as it is done with file name.
  312. // At the moment the title is retrieved from the html document itself.
  313. //echo $document_html;exit;
  314. if (empty($title)) {
  315. $title = $filename; // Here file name is expected to contain ASCII symbols only.
  316. }
  317. if (!empty($document_html)) {
  318. $this->pdf->WriteHTML($document_html.$page_break, 2);
  319. }
  320. } elseif (in_array($extension, array('jpg','jpeg','png','gif'))) {
  321. //Images
  322. $image = Display::img($file);
  323. $this->pdf->WriteHTML('<html><body>'.$image.'</body></html>'.$page_break, 2);
  324. }
  325. }
  326. if (empty($pdf_name)) {
  327. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  328. } else {
  329. $pdf_name = api_replace_dangerous_char($pdf_name);
  330. $output_file = $pdf_name.'.pdf';
  331. }
  332. // F to save the pdf in a file
  333. $this->pdf->Output($output_file, 'D');
  334. exit;
  335. }
  336. /**
  337. * Converts an html string to PDF
  338. * @param string $document_html valid html
  339. * @param string $css CSS content of a CSS file
  340. * @param string $pdf_name pdf name
  341. * @param string $course_code course code
  342. * (if you are using html that are located in the document tool you must provide this)
  343. * @param string $outputMode the MPDF output mode can be:
  344. * 'I' (print on standard output),
  345. * 'D' (download file) (this is the default value),
  346. * 'F' (save to local file) or
  347. * 'S' (return as a string)
  348. * @return string Web path
  349. */
  350. public function content_to_pdf(
  351. $document_html,
  352. $css = '',
  353. $pdf_name = '',
  354. $course_code = null,
  355. $outputMode = 'D',
  356. $saveInFile = false,
  357. $fileToSave = null,
  358. $returnHtml = false
  359. ) {
  360. global $_configuration;
  361. if (empty($document_html)) {
  362. return false;
  363. }
  364. //clean styles and javascript document
  365. $clean_search = array (
  366. '@<script[^>]*?>.*?</script>@si',
  367. '@<style[^>]*?>.*?</style>@siU'
  368. );
  369. // Formatting the pdf
  370. $course_data = api_get_course_info($course_code);
  371. self::format_pdf($course_data);
  372. $document_html = preg_replace($clean_search, '', $document_html);
  373. //absolute path for frames.css //TODO: necessary?
  374. $absolute_css_path = api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/frames.css';
  375. $document_html = str_replace('href="./css/frames.css"','href="'.$absolute_css_path.'"', $document_html);
  376. $document_html= str_replace('../../','',$document_html);
  377. $document_html= str_replace('../','',$document_html);
  378. $document_html= str_replace((empty($_configuration['url_append'])?'':$_configuration['url_append'].'/').'courses/'.$course_code.'/document/','',$document_html);
  379. if (!empty($course_data['path'])) {
  380. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  381. $doc = new DOMDocument();
  382. $result = @$doc->loadHTML($document_html);
  383. //Fixing only images @todo do the same thing with other elements
  384. $elements = $doc->getElementsByTagName('img');
  385. if (!empty($elements)) {
  386. foreach ($elements as $item) {
  387. $old_src = $item->getAttribute('src');
  388. //$old_src= str_replace('../','',$old_src);
  389. if (strpos($old_src, 'http') === false) {
  390. if (strpos($old_src, '/main/default_course_document') === false) {
  391. if (strpos($old_src, '/main/inc/lib/') === false) {
  392. $old_src_fixed = str_replace('/courses/'.$course_data['path'].'/document/', '', $old_src);
  393. $old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed);
  394. $new_path = $document_path.$old_src_fixed;
  395. $document_html= str_replace($old_src, $new_path, $document_html);
  396. }
  397. }
  398. }
  399. }
  400. }
  401. }
  402. //replace relative path by absolute path for resources
  403. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  404. //$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply
  405. //$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  406. //$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
  407. //$document_html= str_replace('src="',$src_http_www, $document_html);
  408. //$document_html= str_replace('temp_template_path', 'src="/main/default_course_document/', $document_html);// restore src templates
  409. api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  410. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through
  411. // $_GET[] too, as it is done with file name.
  412. // At the moment the title is retrieved from the html document itself.
  413. if ($returnHtml) {
  414. return "<style>$css</style>".$document_html;
  415. }
  416. if (!empty($css)) {
  417. $this->pdf->WriteHTML($css, 1);
  418. }
  419. $this->pdf->WriteHTML($document_html, 2);
  420. if (empty($pdf_name)) {
  421. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  422. } else {
  423. $pdf_name = api_replace_dangerous_char($pdf_name);
  424. $output_file = $pdf_name.'.pdf';
  425. }
  426. //$this->pdf->Output($output_file, $outputMode); // F to save the pdf in a file
  427. if ($outputMode == 'F') {
  428. $output_file = api_get_path(SYS_ARCHIVE_PATH) . $output_file;
  429. }
  430. if ($saveInFile) {
  431. $fileToSave = !empty($fileToSave) ? $fileToSave : api_get_path(SYS_ARCHIVE_PATH).uniqid();
  432. $this->pdf->Output(
  433. $fileToSave,
  434. $outputMode
  435. ); // F to save the pdf in a file
  436. } else {
  437. $this->pdf->Output(
  438. $output_file,
  439. $outputMode
  440. ); // F to save the pdf in a file
  441. }
  442. if ($outputMode != 'F') {
  443. exit;
  444. }
  445. }
  446. /**
  447. * Gets the watermark from the platform or a course
  448. * @param string course code (optional)
  449. * @param mixed web path of the watermark image, false if there is nothing to return
  450. */
  451. public static function get_watermark($course_code = null)
  452. {
  453. $web_path = false;
  454. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  455. $course_info = api_get_course_info($course_code);
  456. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  457. if (file_exists($store_path)) {
  458. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  459. }
  460. } else {
  461. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  462. if (file_exists($store_path))
  463. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  464. }
  465. return $web_path;
  466. }
  467. /**
  468. * Deletes the watermark from the Platform or Course
  469. * @param string $course_code course code (optional)
  470. * @param mixed web path of the watermark image, false if there is nothing to return
  471. */
  472. public function delete_watermark($course_code = null)
  473. {
  474. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  475. $course_info = api_get_course_info($course_code);
  476. // course path
  477. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  478. } else {
  479. // course path
  480. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  481. }
  482. if (file_exists($store_path)) {
  483. unlink($store_path);
  484. return true;
  485. }
  486. return false;
  487. }
  488. /**
  489. * Uploads the pdf watermark in the main/default_course_document directory or in the course directory
  490. * @param string $filename filename
  491. * @param string $source_file path of the file
  492. * @param string $course_code
  493. * @return mixed web path of the file if sucess, false otherwise
  494. */
  495. public function upload_watermark($filename, $source_file, $course_code = null)
  496. {
  497. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  498. $course_info = api_get_course_info($course_code);
  499. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path']; // course path
  500. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/pdf_watermark.png';
  501. } else {
  502. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images'; // course path
  503. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  504. }
  505. $course_image = $store_path.'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  506. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  507. $result = false;
  508. if (file_exists($course_image)) {
  509. @unlink($course_image);
  510. }
  511. $my_image = new Image($source_file);
  512. $result = $my_image->send_image($course_image, -1, 'png');
  513. if ($result) {
  514. $result = $web_path;
  515. }
  516. return $result;
  517. }
  518. /**
  519. * Returns the default header
  520. */
  521. public function get_header($course_code = null)
  522. {
  523. /*$header = api_get_setting('pdf_export_watermark_text');
  524. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  525. $header = api_get_course_setting('pdf_export_watermark_text');
  526. }
  527. return $header;*/
  528. }
  529. /**
  530. *
  531. */
  532. public function set_footer()
  533. {
  534. $this->pdf->defaultfooterfontsize = 12; // in pts
  535. $this->pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
  536. $this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer
  537. $platform_name = api_get_setting('Institution');
  538. $left_content = $platform_name;
  539. $center_content = '';
  540. $right_content = '{PAGENO} / {nb}';
  541. //@todo remove this and use a simpler way
  542. $footer = array(
  543. 'odd' => array(
  544. 'L' => array(
  545. 'content' => $left_content,
  546. 'font-size' => 10,
  547. 'font-style' => 'B',
  548. 'font-family' => 'serif',
  549. 'color' => '#000000'
  550. ),
  551. 'C' => array(
  552. 'content' => $center_content,
  553. 'font-size' => 10,
  554. 'font-style' => 'B',
  555. 'font-family' => 'serif',
  556. 'color' => '#000000'
  557. ),
  558. 'R' => array(
  559. 'content' => $right_content,
  560. 'font-size' => 10,
  561. 'font-style' => 'B',
  562. 'font-family' => 'serif',
  563. 'color' => '#000000'
  564. ),
  565. 'line' => 1,
  566. ),
  567. 'even' => array(
  568. 'L' => array(
  569. 'content' => $left_content,
  570. 'font-size' => 10,
  571. 'font-style' => 'B',
  572. 'font-family' => 'serif',
  573. 'color' => '#000000'
  574. ),
  575. 'C' => array(
  576. 'content' => $center_content,
  577. 'font-size' => 10,
  578. 'font-style' => 'B',
  579. 'font-family' => 'serif',
  580. 'color' => '#000000'
  581. ),
  582. 'R' => array(
  583. 'content' => $right_content,
  584. 'font-size' => 10,
  585. 'font-style' => 'B',
  586. 'font-family' => 'serif',
  587. 'color' => '#000000'
  588. ),
  589. 'line' => 1,
  590. ),
  591. );
  592. // defines footer for Odd and Even Pages - placed at Outer margin see http://mpdf1.com/manual/index.php?tid=151&searchstring=setfooter
  593. $this->pdf->SetFooter($footer);
  594. }
  595. /**
  596. * @param array $course_data
  597. */
  598. public function set_header($course_data)
  599. {
  600. $this->pdf->defaultheaderfontsize = 10; // in pts
  601. $this->pdf->defaultheaderfontstyle = 'BI'; // blank, B, I, or BI
  602. $this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer
  603. if (!empty($course_data['code'])) {
  604. $teacher_list = CourseManager::get_teacher_list_from_course_code($course_data['code']);
  605. $teachers = '';
  606. if (!empty($teacher_list)) {
  607. foreach ($teacher_list as $teacher) {
  608. $teachers[]= $teacher['firstname'].' '.$teacher['lastname'];
  609. }
  610. if (count($teachers) > 1) {
  611. $teachers = get_lang('Teachers').': '.implode(', ', $teachers);
  612. } else {
  613. $teachers = get_lang('Teacher').': '.implode('', $teachers);
  614. }
  615. // Do not show the teacher list see BT#4080 only the current teacher name
  616. $user_info = api_get_user_info();
  617. $teachers = $user_info['complete_name'];
  618. }
  619. $left_content = '';
  620. $center_content = '';
  621. $right_content = $teachers;
  622. $header = array(
  623. 'odd' => array(
  624. 'L' => array(
  625. 'content' => $left_content,
  626. 'font-size' => 10,
  627. 'font-style' => 'B',
  628. 'font-family' => 'serif',
  629. 'color'=>'#000000'
  630. ),
  631. 'C' => array(
  632. 'content' => $center_content,
  633. 'font-size' => 10,
  634. 'font-style' => 'B',
  635. 'font-family' => 'serif',
  636. 'color'=>'#000000'
  637. ),
  638. 'R' => array(
  639. 'content' => $right_content,
  640. 'font-size' => 10,
  641. 'font-style' => 'B',
  642. 'font-family' => 'serif',
  643. 'color'=>'#000000'
  644. ),
  645. 'line' => 1,
  646. ),
  647. 'even' => array(
  648. 'L' => array(
  649. 'content' => $left_content,
  650. 'font-size' => 10,
  651. 'font-style' => 'B',
  652. 'font-family' => 'serif',
  653. 'color'=>'#000000'
  654. ),
  655. 'C' => array(
  656. 'content' => $center_content,
  657. 'font-size' => 10,
  658. 'font-style' => 'B',
  659. 'font-family' => 'serif',
  660. 'color'=>'#000000'
  661. ),
  662. 'R' => array(
  663. 'content' => $right_content,
  664. 'font-size' => 10,
  665. 'font-style' => 'B',
  666. 'font-family' => 'serif',
  667. 'color'=>'#000000'
  668. ),
  669. 'line' => 1,
  670. ),
  671. );
  672. $this->pdf->SetHeader($header);// ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
  673. }
  674. }
  675. /**
  676. * @param string $header html content
  677. */
  678. public function set_custom_header($header)
  679. {
  680. $this->custom_header = $header;
  681. }
  682. /**
  683. * @param string $footer html content
  684. */
  685. public function set_custom_footer($footer)
  686. {
  687. $this->custom_footer = $footer;
  688. }
  689. /**
  690. * Pre-formats a PDF to the right size and, if not stated otherwise, with
  691. * header, footer and watermark (if any)
  692. * @param array $course_data General course information (to fill headers)
  693. * @param bool $complete Whether we want headers, footers and watermark or not
  694. */
  695. public function format_pdf($course_data, $complete = true)
  696. {
  697. if ($complete === false) {
  698. error_log('Asked with no decoration');
  699. }
  700. $course_code = null;
  701. if (!empty($course_data)) {
  702. $course_code = $course_data['code'];
  703. }
  704. /*$pdf->SetAuthor('Documents Chamilo');
  705. $pdf->SetTitle('title');
  706. $pdf->SetSubject('Exported from Chamilo Documents');
  707. $pdf->SetKeywords('Chamilo Documents');
  708. */
  709. // TODO: To be read from the html document.
  710. $this->pdf->directionality = api_get_text_direction();
  711. $this->pdf->useOnlyCoreFonts = true;
  712. // Use different Odd/Even headers and footers and mirror margins
  713. $this->pdf->mirrorMargins = 1;
  714. // Add decoration only if not stated otherwise
  715. if ($complete) {
  716. // Adding watermark
  717. if (api_get_setting('pdf_export_watermark_enable') == 'true') {
  718. $watermark_file = self::get_watermark($course_code);
  719. if ($watermark_file) {
  720. //http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
  721. $this->pdf->SetWatermarkImage($watermark_file);
  722. $this->pdf->showWatermarkImage = true;
  723. } else {
  724. $watermark_file = self::get_watermark(null);
  725. if ($watermark_file) {
  726. $this->pdf->SetWatermarkImage($watermark_file);
  727. $this->pdf->showWatermarkImage = true;
  728. }
  729. }
  730. if ($course_code) {
  731. $watermark_text = api_get_course_setting('pdf_export_watermark_text');
  732. if (empty($watermark_text)) {
  733. $watermark_text = api_get_setting('pdf_export_watermark_text');
  734. }
  735. } else {
  736. $watermark_text = api_get_setting('pdf_export_watermark_text');
  737. }
  738. if (!empty($watermark_text)) {
  739. $this->pdf->SetWatermarkText(strcode2utf($watermark_text),0.1);
  740. $this->pdf->showWatermarkText = true;
  741. }
  742. }
  743. if (empty($this->custom_header)) {
  744. self::set_header($course_data);
  745. } else {
  746. $this->pdf->SetHTMLHeader($this->custom_header,'E');
  747. $this->pdf->SetHTMLHeader($this->custom_header,'O');
  748. }
  749. if (empty($this->custom_footer)) {
  750. self::set_footer();
  751. } else {
  752. $this->pdf->SetHTMLFooter($this->custom_footer);
  753. }
  754. }
  755. }
  756. }