pdf.lib.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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('platform.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::getTeacherListFromCourseToString(
  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. $urlAppend = api_get_configuration_value('url_append');
  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($urlAppend)?'':$urlAppend.'/').'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(
  460. 'document.pdf_export_watermark_by_course'
  461. ) == 'true'
  462. ) {
  463. $course_info = api_get_course_info($course_code);
  464. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  465. if (file_exists($store_path)) {
  466. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  467. }
  468. } else {
  469. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path
  470. if (file_exists($store_path))
  471. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  472. }
  473. return $web_path;
  474. }
  475. /**
  476. * Deletes the watermark from the Platform or Course
  477. * @param string $course_code course code (optional)
  478. * @param mixed web path of the watermark image, false if there is nothing to return
  479. */
  480. public function delete_watermark($course_code = null)
  481. {
  482. if (!empty($course_code) && api_get_setting(
  483. 'document.pdf_export_watermark_by_course'
  484. ) == 'true'
  485. ) {
  486. $course_info = api_get_course_info($course_code);
  487. // course path
  488. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  489. } else {
  490. // course path
  491. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  492. }
  493. if (file_exists($store_path)) {
  494. unlink($store_path);
  495. return true;
  496. }
  497. return false;
  498. }
  499. /**
  500. * Uploads the pdf watermark in the main/default_course_document directory or in the course directory
  501. * @param string $filename filename
  502. * @param string $source_file path of the file
  503. * @param string $course_code
  504. * @return mixed web path of the file if sucess, false otherwise
  505. */
  506. public function upload_watermark($filename, $source_file, $course_code = null)
  507. {
  508. if (!empty($course_code) && api_get_setting(
  509. 'document.pdf_export_watermark_by_course'
  510. ) == 'true'
  511. ) {
  512. $course_info = api_get_course_info($course_code);
  513. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path']; // course path
  514. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/pdf_watermark.png';
  515. } else {
  516. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images'; // course path
  517. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png';
  518. }
  519. $course_image = $store_path.'/'.api_get_current_access_url_id().'_pdf_watermark.png';
  520. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  521. $result = false;
  522. if (file_exists($course_image)) {
  523. @unlink($course_image);
  524. }
  525. $my_image = new Image($source_file);
  526. $result = $my_image->send_image($course_image, -1, 'png');
  527. if ($result) {
  528. $result = $web_path;
  529. }
  530. return $result;
  531. }
  532. /**
  533. * Returns the default header
  534. */
  535. public function get_header($course_code = null)
  536. {
  537. /*$header = api_get_setting('pdf_export_watermark_text');
  538. if (!empty($course_code) && api_get_setting('document.pdf_export_watermark_by_course') == 'true') {
  539. $header = api_get_course_setting('pdf_export_watermark_text');
  540. }
  541. return $header;*/
  542. }
  543. /**
  544. *
  545. */
  546. public function set_footer()
  547. {
  548. $this->pdf->defaultfooterfontsize = 12; // in pts
  549. $this->pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
  550. $this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer
  551. $platform_name = api_get_setting('platform.institution');
  552. $left_content = $platform_name;
  553. $center_content = '';
  554. $right_content = '{PAGENO} / {nb}';
  555. //@todo remove this and use a simpler way
  556. $footer = array(
  557. 'odd' => array(
  558. 'L' => array(
  559. 'content' => $left_content,
  560. 'font-size' => 10,
  561. 'font-style' => 'B',
  562. 'font-family' => 'serif',
  563. 'color' => '#000000'
  564. ),
  565. 'C' => array(
  566. 'content' => $center_content,
  567. 'font-size' => 10,
  568. 'font-style' => 'B',
  569. 'font-family' => 'serif',
  570. 'color' => '#000000'
  571. ),
  572. 'R' => array(
  573. 'content' => $right_content,
  574. 'font-size' => 10,
  575. 'font-style' => 'B',
  576. 'font-family' => 'serif',
  577. 'color' => '#000000'
  578. ),
  579. 'line' => 1,
  580. ),
  581. 'even' => array(
  582. 'L' => array(
  583. 'content' => $left_content,
  584. 'font-size' => 10,
  585. 'font-style' => 'B',
  586. 'font-family' => 'serif',
  587. 'color' => '#000000'
  588. ),
  589. 'C' => array(
  590. 'content' => $center_content,
  591. 'font-size' => 10,
  592. 'font-style' => 'B',
  593. 'font-family' => 'serif',
  594. 'color' => '#000000'
  595. ),
  596. 'R' => array(
  597. 'content' => $right_content,
  598. 'font-size' => 10,
  599. 'font-style' => 'B',
  600. 'font-family' => 'serif',
  601. 'color' => '#000000'
  602. ),
  603. 'line' => 1,
  604. ),
  605. );
  606. // defines footer for Odd and Even Pages - placed at Outer margin see http://mpdf1.com/manual/index.php?tid=151&searchstring=setfooter
  607. $this->pdf->SetFooter($footer);
  608. }
  609. /**
  610. * @param array $course_data
  611. */
  612. public function set_header($course_data)
  613. {
  614. $this->pdf->defaultheaderfontsize = 10; // in pts
  615. $this->pdf->defaultheaderfontstyle = 'BI'; // blank, B, I, or BI
  616. $this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer
  617. if (!empty($course_data['code'])) {
  618. $teacher_list = CourseManager::getTeacherListFromCourse(
  619. $course_data['real_id']
  620. );
  621. $teachers = '';
  622. if (!empty($teacher_list)) {
  623. foreach ($teacher_list as $teacher) {
  624. $teachers[]= $teacher['firstname'].' '.$teacher['lastname'];
  625. }
  626. if (count($teachers) > 1) {
  627. $teachers = get_lang('Teachers').': '.implode(', ', $teachers);
  628. } else {
  629. $teachers = get_lang('Teacher').': '.implode('', $teachers);
  630. }
  631. // Do not show the teacher list see BT#4080 only the current teacher name
  632. $user_info = api_get_user_info();
  633. $teachers = $user_info['complete_name'];
  634. }
  635. $left_content = '';
  636. $center_content = '';
  637. $right_content = $teachers;
  638. $header = array(
  639. 'odd' => array(
  640. 'L' => array(
  641. 'content' => $left_content,
  642. 'font-size' => 10,
  643. 'font-style' => 'B',
  644. 'font-family' => 'serif',
  645. 'color'=>'#000000'
  646. ),
  647. 'C' => array(
  648. 'content' => $center_content,
  649. 'font-size' => 10,
  650. 'font-style' => 'B',
  651. 'font-family' => 'serif',
  652. 'color'=>'#000000'
  653. ),
  654. 'R' => array(
  655. 'content' => $right_content,
  656. 'font-size' => 10,
  657. 'font-style' => 'B',
  658. 'font-family' => 'serif',
  659. 'color'=>'#000000'
  660. ),
  661. 'line' => 1,
  662. ),
  663. 'even' => array(
  664. 'L' => array(
  665. 'content' => $left_content,
  666. 'font-size' => 10,
  667. 'font-style' => 'B',
  668. 'font-family' => 'serif',
  669. 'color'=>'#000000'
  670. ),
  671. 'C' => array(
  672. 'content' => $center_content,
  673. 'font-size' => 10,
  674. 'font-style' => 'B',
  675. 'font-family' => 'serif',
  676. 'color'=>'#000000'
  677. ),
  678. 'R' => array(
  679. 'content' => $right_content,
  680. 'font-size' => 10,
  681. 'font-style' => 'B',
  682. 'font-family' => 'serif',
  683. 'color'=>'#000000'
  684. ),
  685. 'line' => 1,
  686. ),
  687. );
  688. $this->pdf->SetHeader($header);// ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
  689. }
  690. }
  691. /**
  692. * @param string $header html content
  693. */
  694. public function set_custom_header($header)
  695. {
  696. $this->custom_header = $header;
  697. }
  698. /**
  699. * @param string $footer html content
  700. */
  701. public function set_custom_footer($footer)
  702. {
  703. $this->custom_footer = $footer;
  704. }
  705. /**
  706. * Pre-formats a PDF to the right size and, if not stated otherwise, with
  707. * header, footer and watermark (if any)
  708. * @param array $course_data General course information (to fill headers)
  709. * @param bool $complete Whether we want headers, footers and watermark or not
  710. */
  711. public function format_pdf($course_data, $complete = true)
  712. {
  713. if ($complete === false) {
  714. error_log('Asked with no decoration');
  715. }
  716. $course_code = null;
  717. if (!empty($course_data)) {
  718. $course_code = $course_data['code'];
  719. }
  720. /*$pdf->SetAuthor('Documents Chamilo');
  721. $pdf->SetTitle('title');
  722. $pdf->SetSubject('Exported from Chamilo Documents');
  723. $pdf->SetKeywords('Chamilo Documents');
  724. */
  725. // TODO: To be read from the html document.
  726. $this->pdf->directionality = api_get_text_direction();
  727. $this->pdf->useOnlyCoreFonts = true;
  728. // Use different Odd/Even headers and footers and mirror margins
  729. $this->pdf->mirrorMargins = 1;
  730. // Add decoration only if not stated otherwise
  731. if ($complete) {
  732. // Adding watermark
  733. if (api_get_setting(
  734. 'document.pdf_export_watermark_enable'
  735. ) == 'true'
  736. ) {
  737. $watermark_file = self::get_watermark($course_code);
  738. if ($watermark_file) {
  739. //http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
  740. $this->pdf->SetWatermarkImage($watermark_file);
  741. $this->pdf->showWatermarkImage = true;
  742. } else {
  743. $watermark_file = self::get_watermark(null);
  744. if ($watermark_file) {
  745. $this->pdf->SetWatermarkImage($watermark_file);
  746. $this->pdf->showWatermarkImage = true;
  747. }
  748. }
  749. if ($course_code) {
  750. $watermark_text = api_get_course_setting('pdf_export_watermark_text');
  751. if (empty($watermark_text)) {
  752. $watermark_text = api_get_setting('pdf_export_watermark_text');
  753. }
  754. } else {
  755. $watermark_text = api_get_setting('pdf_export_watermark_text');
  756. }
  757. if (!empty($watermark_text)) {
  758. $this->pdf->SetWatermarkText(strcode2utf($watermark_text),0.1);
  759. $this->pdf->showWatermarkText = true;
  760. }
  761. }
  762. if (empty($this->custom_header)) {
  763. self::set_header($course_data);
  764. } else {
  765. $this->pdf->SetHTMLHeader($this->custom_header,'E');
  766. $this->pdf->SetHTMLHeader($this->custom_header,'O');
  767. }
  768. if (empty($this->custom_footer)) {
  769. self::set_footer();
  770. } else {
  771. $this->pdf->SetHTMLFooter($this->custom_footer);
  772. }
  773. }
  774. }
  775. }