pdf.lib.php 36 KB

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