pdf.lib.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. <?php
  2. /* See license terms in /license.txt */
  3. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  4. /**
  5. * Class PDF.
  6. *
  7. * @package chamilo.library
  8. */
  9. class PDF
  10. {
  11. /** @var mPDF */
  12. public $pdf;
  13. public $custom_header = [];
  14. public $custom_footer = [];
  15. public $params = [];
  16. public $template;
  17. /**
  18. * Creates the mPDF object.
  19. *
  20. * @param string $pageFormat format A4 A4-L see
  21. * http://mpdf1.com/manual/index.php?tid=184&searchstring=format
  22. * @param string $orientation orientation "P" = Portrait "L" = Landscape
  23. * @param array $params
  24. * @param Template $template
  25. */
  26. public function __construct(
  27. $pageFormat = 'A4',
  28. $orientation = 'P',
  29. $params = [],
  30. $template = null
  31. ) {
  32. $this->template = $template;
  33. /* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=mPDF */
  34. if (!in_array($orientation, ['P', 'L'])) {
  35. $orientation = 'P';
  36. }
  37. //left, right, top, bottom, margin_header, margin footer
  38. $params['left'] = isset($params['left']) ? $params['left'] : 15;
  39. $params['right'] = isset($params['right']) ? $params['right'] : 15;
  40. $params['top'] = isset($params['top']) ? $params['top'] : 30;
  41. $params['bottom'] = isset($params['bottom']) ? $params['bottom'] : 30;
  42. $params['margin_footer'] = isset($params['margin_footer']) ? $params['margin_footer'] : 8;
  43. $this->params['filename'] = isset($params['filename']) ? $params['filename'] : api_get_local_time();
  44. $this->params['pdf_title'] = isset($params['pdf_title']) ? $params['pdf_title'] : '';
  45. $this->params['course_info'] = isset($params['course_info']) ? $params['course_info'] : api_get_course_info();
  46. $this->params['session_info'] = isset($params['session_info']) ? $params['session_info'] : api_get_session_info(api_get_session_id());
  47. $this->params['course_code'] = isset($params['course_code']) ? $params['course_code'] : api_get_course_id();
  48. $this->params['add_signatures'] = isset($params['add_signatures']) ? $params['add_signatures'] : [];
  49. $this->params['show_real_course_teachers'] = isset($params['show_real_course_teachers']) ? $params['show_real_course_teachers'] : false;
  50. $this->params['student_info'] = isset($params['student_info']) ? $params['student_info'] : false;
  51. $this->params['show_grade_generated_date'] = isset($params['show_grade_generated_date']) ? $params['show_grade_generated_date'] : false;
  52. $this->params['show_teacher_as_myself'] = isset($params['show_teacher_as_myself']) ? $params['show_teacher_as_myself'] : true;
  53. $localTime = api_get_local_time();
  54. $this->params['pdf_date'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_TIME_FORMAT_LONG);
  55. $this->params['pdf_date_only'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_FORMAT_LONG);
  56. $this->pdf = new mPDF(
  57. 'UTF-8',
  58. $pageFormat,
  59. '',
  60. '',
  61. $params['left'],
  62. $params['right'],
  63. $params['top'],
  64. $params['bottom'],
  65. 8,
  66. 8,
  67. $orientation
  68. );
  69. $this->pdf->margin_footer = $params['margin_footer'];
  70. // Default value is 96 set in the mpdf library file config.php
  71. $value = api_get_configuration_value('pdf_img_dpi');
  72. if (!empty($value)) {
  73. $this->pdf->img_dpi = (int) $value;
  74. }
  75. }
  76. /**
  77. * Export the given HTML to PDF, using a global template.
  78. *
  79. * @uses \export/table_pdf.tpl
  80. *
  81. * @param string $content
  82. * @param bool|false $saveToFile
  83. * @param bool|false $returnHtml
  84. * @param bool $addDefaultCss (bootstrap/default/base.css)
  85. *
  86. * @return string
  87. */
  88. public function html_to_pdf_with_template(
  89. $content,
  90. $saveToFile = false,
  91. $returnHtml = false,
  92. $addDefaultCss = false
  93. ) {
  94. if (empty($this->template)) {
  95. $tpl = new Template('', false, false, false, false, true, false);
  96. } else {
  97. $tpl = $this->template;
  98. }
  99. // Assignments
  100. $tpl->assign('pdf_content', $content);
  101. // Showing only the current teacher/admin instead the all teacher list name see BT#4080
  102. $teacher_list = null;
  103. if (isset($this->params['show_real_course_teachers']) &&
  104. $this->params['show_real_course_teachers']
  105. ) {
  106. if (isset($this->params['session_info']) &&
  107. !empty($this->params['session_info'])
  108. ) {
  109. $teacher_list = SessionManager::getCoachesByCourseSessionToString(
  110. $this->params['session_info']['id'],
  111. $this->params['course_info']['real_id']
  112. );
  113. } else {
  114. $teacher_list = CourseManager::getTeacherListFromCourseCodeToString(
  115. $this->params['course_code']
  116. );
  117. }
  118. } else {
  119. $user_info = api_get_user_info();
  120. if ($this->params['show_teacher_as_myself']) {
  121. $teacher_list = $user_info['complete_name'];
  122. }
  123. }
  124. $tpl->assign('pdf_course', $this->params['course_code']);
  125. $tpl->assign('pdf_course_info', $this->params['course_info']);
  126. $tpl->assign('pdf_session_info', $this->params['session_info']);
  127. $tpl->assign('pdf_date', $this->params['pdf_date']);
  128. $tpl->assign('pdf_date_only', $this->params['pdf_date_only']);
  129. $tpl->assign('pdf_teachers', $teacher_list);
  130. $tpl->assign('pdf_title', $this->params['pdf_title']);
  131. $tpl->assign('pdf_student_info', $this->params['student_info']);
  132. $tpl->assign('show_grade_generated_date', $this->params['show_grade_generated_date']);
  133. $tpl->assign('add_signatures', $this->params['add_signatures']);
  134. // Getting template
  135. $tableTemplate = $tpl->get_template('export/table_pdf.tpl');
  136. $html = $tpl->fetch($tableTemplate);
  137. $html = api_utf8_encode($html);
  138. if ($returnHtml) {
  139. return $html;
  140. }
  141. $css_file = api_get_path(SYS_CSS_PATH).'themes/'.$tpl->theme.'/print.css';
  142. if (!file_exists($css_file)) {
  143. $css_file = api_get_path(SYS_CSS_PATH).'print.css';
  144. }
  145. $css = file_get_contents($css_file);
  146. 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. $addDefaultCss
  156. );
  157. }
  158. /**
  159. * Converts HTML files to PDF.
  160. *
  161. * @param mixed $html_file_array could be an html file path or an array
  162. * with paths example:
  163. * /var/www/myfile.html or array('/myfile.html','myotherfile.html') or
  164. * even an indexed array with both 'title' and 'path' indexes
  165. * for each element like
  166. * array(
  167. * 0 => array('title'=>'Hello','path'=>'file.html'),
  168. * 1 => array('title'=>'Bye','path'=>'file2.html')
  169. * );
  170. * @param string $pdf_name pdf name
  171. * @param string $course_code (if you are using html that are located
  172. * in the document tool you must provide this)
  173. * @param bool $print_title add title
  174. * @param bool $complete_style show header and footer if true
  175. * @param bool $addStyle
  176. * @param string $mainTitle
  177. *
  178. * @return false|null
  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. $mainTitle = ''
  188. ) {
  189. if (empty($html_file_array)) {
  190. return false;
  191. }
  192. if (is_array($html_file_array)) {
  193. if (count($html_file_array) == 0) {
  194. return false;
  195. }
  196. } else {
  197. if (!file_exists($html_file_array)) {
  198. return false;
  199. }
  200. // Converting the string into an array
  201. $html_file_array = [$html_file_array];
  202. }
  203. if (!empty($course_code)) {
  204. $course_data = api_get_course_info($course_code);
  205. } else {
  206. $course_data = api_get_course_info();
  207. }
  208. // Clean styles and javascript document
  209. $clean_search = [
  210. '@<script[^>]*?>.*?</script>@si',
  211. '@<style[^>]*?>.*?</style>@si',
  212. ];
  213. // Formatting the pdf
  214. self::format_pdf($course_data, $complete_style);
  215. $counter = 1;
  216. foreach ($html_file_array as $file) {
  217. // Add a page break per file
  218. $page_break = '<pagebreak>';
  219. if ($counter == count($html_file_array)) {
  220. $page_break = '';
  221. }
  222. // if the array provided contained subarrays with 'title' entry,
  223. // then print the title in the PDF
  224. if (is_array($file) && isset($file['title'])) {
  225. $html_title = $file['title'];
  226. $file = $file['path'];
  227. } else {
  228. //we suppose we've only been sent a file path
  229. $html_title = basename($file);
  230. }
  231. $counter++;
  232. if (empty($file) && !empty($html_title)) {
  233. // this is a chapter, print title & skip the rest
  234. if ($counter === 2 && !empty($mainTitle)) {
  235. $this->pdf->WriteHTML(
  236. '<html><body><h2 style="text-align: center">'.$mainTitle.'</h2></body></html>'
  237. );
  238. }
  239. if ($print_title) {
  240. $this->pdf->WriteHTML(
  241. '<html><body><h3>'.$html_title.'</h3></body></html>'.$page_break
  242. );
  243. }
  244. continue;
  245. } else {
  246. if ($counter === 2 && !empty($mainTitle)) {
  247. $this->pdf->WriteHTML(
  248. '<html><body><h2 style="text-align: center">'.$mainTitle.'</h2></body></html>'
  249. );
  250. }
  251. }
  252. if (!file_exists($file)) {
  253. //the file doesn't exist, skip
  254. continue;
  255. }
  256. if ($addStyle) {
  257. $css_file = api_get_path(SYS_CSS_PATH).'/print.css';
  258. $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
  259. $this->pdf->WriteHTML($css, 1);
  260. }
  261. // it's not a chapter but the file exists, print its title
  262. if ($print_title) {
  263. $this->pdf->WriteHTML(
  264. '<html><body><h3>'.$html_title.'</h3></body></html>'
  265. );
  266. }
  267. $file_info = pathinfo($file);
  268. $extension = $file_info['extension'];
  269. if (in_array($extension, ['html', 'htm'])) {
  270. $dirName = $file_info['dirname'];
  271. $filename = $file_info['basename'];
  272. $filename = str_replace('_', ' ', $filename);
  273. if ($extension === 'html') {
  274. $filename = basename($filename, '.html');
  275. } elseif ($extension === 'htm') {
  276. $filename = basename($filename, '.htm');
  277. }
  278. $document_html = @file_get_contents($file);
  279. $document_html = preg_replace($clean_search, '', $document_html);
  280. //absolute path for frames.css //TODO: necessary?
  281. $absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
  282. $document_html = str_replace('href="./css/frames.css"', $absolute_css_path, $document_html);
  283. if (!empty($course_data['path'])) {
  284. $document_html = str_replace('../', '', $document_html);
  285. // Fix app/upload links convert web to system paths
  286. $document_html = str_replace(
  287. api_get_path(WEB_UPLOAD_PATH),
  288. api_get_path(SYS_UPLOAD_PATH),
  289. $document_html
  290. );
  291. }
  292. $document_html = self::fixImagesPaths($document_html, $course_data, $dirName);
  293. // The library mPDF expects UTF-8 encoded input data.
  294. api_set_encoding_html($document_html, 'UTF-8');
  295. // TODO: Maybe it is better idea the title to be passed through
  296. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');
  297. // $_GET[] too, as it is done with file name.
  298. // At the moment the title is retrieved from the html document itself.
  299. //echo $document_html;exit;
  300. if (empty($title)) {
  301. $title = $filename; // Here file name is expected to contain ASCII symbols only.
  302. }
  303. if (!empty($document_html)) {
  304. $this->pdf->WriteHTML($document_html.$page_break);
  305. }
  306. } elseif (in_array($extension, ['jpg', 'jpeg', 'png', 'gif'])) {
  307. // Images
  308. $image = Display::img($file);
  309. $this->pdf->WriteHTML('<html><body>'.$image.'</body></html>'.$page_break);
  310. }
  311. }
  312. if (empty($pdf_name)) {
  313. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  314. } else {
  315. $pdf_name = api_replace_dangerous_char($pdf_name);
  316. $output_file = $pdf_name.'.pdf';
  317. }
  318. // F to save the pdf in a file
  319. $this->pdf->Output($output_file, 'D');
  320. exit;
  321. }
  322. /**
  323. * Converts an html string to PDF.
  324. *
  325. * @param string $document_html valid html
  326. * @param string $css CSS content of a CSS file
  327. * @param string $pdf_name pdf name
  328. * @param string $course_code course code
  329. * (if you are using html that are located in the document tool you must provide this)
  330. * @param string $outputMode the MPDF output mode can be:
  331. * @param bool $saveInFile
  332. * @param string $fileToSave
  333. * @param bool $returnHtml
  334. * @param bool $addDefaultCss
  335. * @param bool $completeHeader
  336. *
  337. * 'I' (print on standard output),
  338. * 'D' (download file) (this is the default value),
  339. * 'F' (save to local file) or
  340. * 'S' (return as a string)
  341. *
  342. * @throws MpdfException
  343. *
  344. * @return string Web path
  345. */
  346. public function content_to_pdf(
  347. $document_html,
  348. $css = '',
  349. $pdf_name = '',
  350. $course_code = null,
  351. $outputMode = 'D',
  352. $saveInFile = false,
  353. $fileToSave = null,
  354. $returnHtml = false,
  355. $addDefaultCss = false,
  356. $completeHeader = true
  357. ) {
  358. $urlAppend = api_get_configuration_value('url_append');
  359. if (empty($document_html)) {
  360. return false;
  361. }
  362. // clean styles and javascript document
  363. $clean_search = [
  364. '@<script[^>]*?>.*?</script>@si',
  365. '@<style[^>]*?>.*?</style>@siU',
  366. ];
  367. // Formatting the pdf
  368. $course_data = api_get_course_info($course_code);
  369. self::format_pdf($course_data, $completeHeader);
  370. $document_html = preg_replace($clean_search, '', $document_html);
  371. //absolute path for frames.css //TODO: necessary?
  372. $absolute_css_path = api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/frames.css';
  373. $document_html = str_replace('href="./css/frames.css"', 'href="'.$absolute_css_path.'"', $document_html);
  374. $document_html = str_replace('../../', '', $document_html);
  375. $document_html = str_replace('../', '', $document_html);
  376. $document_html = str_replace(
  377. (empty($urlAppend) ? '' : $urlAppend.'/').'courses/'.$course_code.'/document/',
  378. '',
  379. $document_html
  380. );
  381. if (!empty($course_data['path'])) {
  382. $document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
  383. $doc = new DOMDocument();
  384. @$doc->loadHTML($document_html);
  385. //Fixing only images @todo do the same thing with other elements
  386. $elements = $doc->getElementsByTagName('img');
  387. $protocol = api_get_protocol();
  388. $replaced = [];
  389. if (!empty($elements)) {
  390. foreach ($elements as $item) {
  391. $old_src = $item->getAttribute('src');
  392. if (in_array($old_src, $replaced)) {
  393. continue;
  394. }
  395. if (strpos($old_src, $protocol) === false) {
  396. if (strpos($old_src, '/main/default_course_document') === false) {
  397. if (strpos($old_src, '/main/inc/lib/') === false &&
  398. strpos($old_src, '/app/upload/') === false
  399. ) {
  400. $old_src_fixed = str_replace(
  401. api_get_path(REL_COURSE_PATH).$course_data['path'].'/document/',
  402. '',
  403. $old_src
  404. );
  405. $old_src_fixed = str_replace(
  406. 'courses/'.$course_data['path'].'/document/',
  407. '',
  408. $old_src_fixed
  409. );
  410. $new_path = $document_path.$old_src_fixed;
  411. $document_html = str_replace($old_src, $new_path, $document_html);
  412. $replaced[] = $old_src;
  413. }
  414. }
  415. }
  416. }
  417. }
  418. }
  419. // Use sys path to correct export images
  420. $document_html = str_replace(
  421. api_get_path(WEB_CODE_PATH).'img/',
  422. api_get_path(SYS_CODE_PATH).'img/',
  423. $document_html
  424. );
  425. $document_html = str_replace(api_get_path(WEB_ARCHIVE_PATH), api_get_path(SYS_ARCHIVE_PATH), $document_html);
  426. // The library mPDF expects UTF-8 encoded input data.
  427. api_set_encoding_html($document_html, 'UTF-8');
  428. // At the moment the title is retrieved from the html document itself.
  429. if ($returnHtml) {
  430. return "<style>$css</style>".$document_html;
  431. }
  432. if (!empty($css)) {
  433. try {
  434. $this->pdf->WriteHTML($css, 1);
  435. } catch (MpdfException $e) {
  436. error_log($e);
  437. }
  438. }
  439. if ($addDefaultCss) {
  440. $basicStyles = [
  441. api_get_path(SYS_PATH).'web/assets/bootstrap/dist/css/bootstrap.min.css',
  442. api_get_path(SYS_PATH).'web/css/base.css',
  443. api_get_path(SYS_PATH).'web/css/themes/'.api_get_visual_theme().'/default.css',
  444. ];
  445. foreach ($basicStyles as $style) {
  446. $cssContent = file_get_contents($style);
  447. try {
  448. $this->pdf->WriteHTML($cssContent, 1);
  449. } catch (MpdfException $e) {
  450. error_log($e);
  451. }
  452. }
  453. }
  454. try {
  455. $this->pdf->WriteHTML($document_html);
  456. } catch (MpdfException $e) {
  457. error_log($e);
  458. }
  459. if (empty($pdf_name)) {
  460. $output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
  461. } else {
  462. $pdf_name = api_replace_dangerous_char($pdf_name);
  463. $output_file = $pdf_name.'.pdf';
  464. }
  465. if ($outputMode == 'F') {
  466. $output_file = api_get_path(SYS_ARCHIVE_PATH).$output_file;
  467. }
  468. if ($saveInFile) {
  469. $fileToSave = !empty($fileToSave) ? $fileToSave : api_get_path(SYS_ARCHIVE_PATH).uniqid();
  470. $this->pdf->Output(
  471. $fileToSave,
  472. $outputMode
  473. ); // F to save the pdf in a file
  474. } else {
  475. $this->pdf->Output(
  476. $output_file,
  477. $outputMode
  478. ); // F to save the pdf in a file
  479. }
  480. if ($outputMode != 'F') {
  481. exit;
  482. }
  483. return $output_file;
  484. }
  485. /**
  486. * Gets the watermark from the platform or a course.
  487. *
  488. * @param string course code (optional)
  489. * @param mixed web path of the watermark image, false if there is nothing to return
  490. *
  491. * @return string
  492. */
  493. public static function get_watermark($course_code = null)
  494. {
  495. $web_path = false;
  496. $urlId = api_get_current_access_url_id();
  497. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  498. $course_info = api_get_course_info($course_code);
  499. // course path
  500. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.$urlId.'_pdf_watermark.png';
  501. if (file_exists($store_path)) {
  502. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/'.$urlId.'_pdf_watermark.png';
  503. }
  504. } else {
  505. // course path
  506. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.$urlId.'_pdf_watermark.png';
  507. if (file_exists($store_path)) {
  508. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.$urlId.'_pdf_watermark.png';
  509. }
  510. }
  511. return $web_path;
  512. }
  513. /**
  514. * Deletes the watermark from the Platform or Course.
  515. *
  516. * @param string $course_code course code (optional)
  517. * @param mixed web path of the watermark image, false if there is nothing to return
  518. *
  519. * @return bool
  520. */
  521. public function delete_watermark($course_code = null)
  522. {
  523. $urlId = api_get_current_access_url_id();
  524. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  525. $course_info = api_get_course_info($course_code);
  526. // course path
  527. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.$urlId.'_pdf_watermark.png';
  528. } else {
  529. // course path
  530. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.$urlId.'_pdf_watermark.png';
  531. }
  532. if (file_exists($store_path)) {
  533. unlink($store_path);
  534. return true;
  535. }
  536. return false;
  537. }
  538. /**
  539. * Uploads the pdf watermark in the main/default_course_document directory or in the course directory.
  540. *
  541. * @param string $filename filename
  542. * @param string $source_file path of the file
  543. * @param string $course_code
  544. *
  545. * @return mixed web path of the file if sucess, false otherwise
  546. */
  547. public function upload_watermark($filename, $source_file, $course_code = null)
  548. {
  549. $urlId = api_get_current_access_url_id();
  550. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  551. $course_info = api_get_course_info($course_code);
  552. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path']; // course path
  553. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/pdf_watermark.png';
  554. } else {
  555. $store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images'; // course path
  556. $web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.$urlId.'_pdf_watermark.png';
  557. }
  558. $course_image = $store_path.'/'.$urlId.'_pdf_watermark.png';
  559. if (file_exists($course_image)) {
  560. @unlink($course_image);
  561. }
  562. $my_image = new Image($source_file);
  563. $result = $my_image->send_image($course_image, -1, 'png');
  564. if ($result) {
  565. $result = $web_path;
  566. }
  567. return $result;
  568. }
  569. /**
  570. * Returns the default header.
  571. */
  572. public function get_header($course_code = null)
  573. {
  574. /*$header = api_get_setting('pdf_export_watermark_text');
  575. if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
  576. $header = api_get_course_setting('pdf_export_watermark_text');
  577. }
  578. return $header;*/
  579. }
  580. /**
  581. * Sets the PDF footer.
  582. */
  583. public function set_footer()
  584. {
  585. $this->pdf->defaultfooterfontsize = 12; // in pts
  586. $this->pdf->defaultfooterfontstyle = 'B'; // blank, B, I, or BI
  587. $this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer
  588. $view = new Template('', false, false, false, true, false, false);
  589. $template = $view->get_template('export/pdf_footer.tpl');
  590. $footerHTML = $view->fetch($template);
  591. $this->pdf->SetHTMLFooter($footerHTML, 'E'); //Even pages
  592. $this->pdf->SetHTMLFooter($footerHTML, 'O'); //Odd pages
  593. }
  594. /**
  595. * Sets the PDF header.
  596. *
  597. * @param array $courseInfo
  598. */
  599. public function set_header($courseInfo)
  600. {
  601. $this->pdf->defaultheaderfontsize = 10; // in pts
  602. $this->pdf->defaultheaderfontstyle = 'BI'; // blank, B, I, or BI
  603. $this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer
  604. $userId = api_get_user_id();
  605. if (!empty($courseInfo['code'])) {
  606. $teacher_list = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
  607. $teachers = '';
  608. if (!empty($teacher_list)) {
  609. foreach ($teacher_list as $teacher) {
  610. if ($teacher['user_id'] != $userId) {
  611. continue;
  612. }
  613. // Do not show the teacher list see BT#4080 only the current teacher name
  614. $teachers = api_get_person_name($teacher['firstname'], $teacher['lastname']);
  615. }
  616. }
  617. $organization = ChamiloApi::getPlatformLogo('', [], true);
  618. // Use custom logo image.
  619. $pdfLogo = api_get_setting('pdf_logo_header');
  620. if ($pdfLogo === 'true') {
  621. $visualTheme = api_get_visual_theme();
  622. $img = api_get_path(SYS_CSS_PATH).'themes/'.$visualTheme.'/images/pdf_logo_header.png';
  623. if (file_exists($img)) {
  624. //$img = api_get_path(WEB_CSS_PATH).'themes/'.$visualTheme.'/images/pdf_logo_header.png';
  625. $organization = "<img src='$img'>";
  626. }
  627. }
  628. $view = new Template('', false, false, false, true, false, false);
  629. $view->assign('teacher_name', $teachers);
  630. $view->assign('organization', $organization);
  631. $template = $view->get_template('export/pdf_header.tpl');
  632. $headerHTML = $view->fetch($template);
  633. $this->pdf->SetHTMLHeader($headerHTML, 'E');
  634. $this->pdf->SetHTMLHeader($headerHTML, 'O');
  635. }
  636. }
  637. /**
  638. * @param string $header html content
  639. */
  640. public function set_custom_header($header)
  641. {
  642. $this->custom_header = $header;
  643. }
  644. /**
  645. * @param array $footer html content
  646. */
  647. public function set_custom_footer($footer)
  648. {
  649. $this->custom_footer = $footer;
  650. }
  651. /**
  652. * Pre-formats a PDF to the right size and, if not stated otherwise, with
  653. * header, footer and watermark (if any).
  654. *
  655. * @param array $courseInfo General course information (to fill headers)
  656. * @param bool $complete Whether we want headers, footers and watermark or not
  657. */
  658. public function format_pdf($courseInfo, $complete = true)
  659. {
  660. $courseCode = null;
  661. if (!empty($courseInfo)) {
  662. $courseCode = $courseInfo['code'];
  663. }
  664. /*$pdf->SetAuthor('Documents Chamilo');
  665. $pdf->SetTitle('title');
  666. $pdf->SetSubject('Exported from Chamilo Documents');
  667. $pdf->SetKeywords('Chamilo Documents');
  668. */
  669. // TODO: To be read from the html document.
  670. $this->pdf->directionality = api_get_text_direction();
  671. $this->pdf->useOnlyCoreFonts = true;
  672. // Use different Odd/Even headers and footers and mirror margins
  673. $this->pdf->mirrorMargins = 1;
  674. // Add decoration only if not stated otherwise
  675. if ($complete) {
  676. // Adding watermark
  677. if (api_get_setting('pdf_export_watermark_enable') == 'true') {
  678. $watermark_file = self::get_watermark($courseCode);
  679. if ($watermark_file) {
  680. //http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
  681. $this->pdf->SetWatermarkImage($watermark_file);
  682. $this->pdf->showWatermarkImage = true;
  683. } else {
  684. $watermark_file = self::get_watermark(null);
  685. if ($watermark_file) {
  686. $this->pdf->SetWatermarkImage($watermark_file);
  687. $this->pdf->showWatermarkImage = true;
  688. }
  689. }
  690. if ($courseCode) {
  691. $watermark_text = api_get_course_setting('pdf_export_watermark_text');
  692. if (empty($watermark_text)) {
  693. $watermark_text = api_get_setting('pdf_export_watermark_text');
  694. }
  695. } else {
  696. $watermark_text = api_get_setting('pdf_export_watermark_text');
  697. }
  698. if (!empty($watermark_text)) {
  699. $this->pdf->SetWatermarkText(
  700. strcode2utf($watermark_text),
  701. 0.1
  702. );
  703. $this->pdf->showWatermarkText = true;
  704. }
  705. }
  706. if (empty($this->custom_header)) {
  707. self::set_header($courseInfo);
  708. } else {
  709. $this->pdf->SetHTMLHeader($this->custom_header, 'E');
  710. $this->pdf->SetHTMLHeader($this->custom_header, 'O');
  711. }
  712. if (empty($this->custom_footer)) {
  713. self::set_footer();
  714. } else {
  715. $this->pdf->SetHTMLFooter($this->custom_footer);
  716. }
  717. }
  718. }
  719. /**
  720. * Generate a PDF file from $html in SYS_APP_PATH.
  721. *
  722. * @param string $html PDF content
  723. * @param string $fileName File name
  724. * @param string $dest Optional. Directory to move file
  725. *
  726. * @return string The PDF path
  727. */
  728. public function exportFromHtmlToFile($html, $fileName, $dest = null)
  729. {
  730. $this->template = $this->template ?: new Template('', false, false, false, false, false, false);
  731. $cssFile = api_get_path(SYS_CSS_PATH).'themes/'.$this->template->theme.'/print.css';
  732. if (!file_exists($cssFile)) {
  733. $cssFile = api_get_path(SYS_CSS_PATH).'print.css';
  734. }
  735. $pdfPath = self::content_to_pdf(
  736. $html,
  737. file_get_contents($cssFile),
  738. $fileName,
  739. $this->params['course_code'],
  740. 'F'
  741. );
  742. if (!$dest) {
  743. return $pdfPath;
  744. }
  745. move($pdfPath, $dest);
  746. return $dest.basename($pdfPath);
  747. }
  748. /**
  749. * Create a PDF and save it into the documents area.
  750. *
  751. * @param string $htmlContent HTML Content
  752. * @param string $fileName The file name
  753. * @param int $courseId The course ID
  754. * @param int $sessionId Optional. The session ID
  755. */
  756. public function exportFromHtmlToDocumentsArea(
  757. $htmlContent,
  758. $fileName,
  759. $courseId,
  760. $sessionId = 0
  761. ) {
  762. $userId = api_get_user_id();
  763. $courseInfo = api_get_course_info_by_id($courseId);
  764. $courseDirectory = api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/document/';
  765. $docPath = $this->exportFromHtmlToFile(
  766. $htmlContent,
  767. $fileName,
  768. $courseDirectory
  769. );
  770. $docId = add_document(
  771. $courseInfo,
  772. str_replace($courseDirectory, '/', $docPath),
  773. 'file',
  774. filesize($docPath),
  775. $fileName,
  776. null,
  777. false,
  778. true,
  779. null,
  780. $sessionId,
  781. $userId
  782. );
  783. api_item_property_update(
  784. $courseInfo,
  785. TOOL_DOCUMENT,
  786. $docId,
  787. 'DocumentAdded',
  788. $userId
  789. );
  790. Display::addFlash(Display::return_message(get_lang('ItemAdded')));
  791. }
  792. /**
  793. * @param string $theme
  794. *
  795. * @throws MpdfException
  796. */
  797. public function setBackground($theme)
  798. {
  799. $themeName = empty($theme) ? api_get_visual_theme() : $theme;
  800. $themeDir = \Template::getThemeDir($themeName);
  801. $customLetterhead = $themeDir.'images/letterhead.png';
  802. $urlPathLetterhead = api_get_path(SYS_CSS_PATH).$customLetterhead;
  803. $urlWebLetterhead = '#FFFFFF';
  804. $fullPage = false;
  805. if (file_exists($urlPathLetterhead)) {
  806. $fullPage = true;
  807. $urlWebLetterhead = 'url('.api_get_path(WEB_CSS_PATH).$customLetterhead.')';
  808. }
  809. if ($fullPage) {
  810. $this->pdf->SetDisplayMode('fullpage');
  811. $this->pdf->SetDefaultBodyCSS('background', $urlWebLetterhead);
  812. $this->pdf->SetDefaultBodyCSS('background-image-resize', '6');
  813. }
  814. }
  815. /**
  816. * Fix images source paths to allow export to pdf.
  817. *
  818. * @param string $documentHtml
  819. * @param array $courseInfo
  820. * @param string $dirName
  821. *
  822. * @return string
  823. */
  824. private static function fixImagesPaths($documentHtml, array $courseInfo, $dirName = '')
  825. {
  826. $documentHtml = '<?xml encoding="utf-8" ?>'.$documentHtml;
  827. $doc = new DOMDocument();
  828. @$doc->loadHTML($documentHtml);
  829. $elements = $doc->getElementsByTagName('img');
  830. if (empty($elements)) {
  831. return $doc->saveHTML();
  832. }
  833. $protocol = api_get_protocol();
  834. $sysCodePath = api_get_path(SYS_CODE_PATH);
  835. $sysCoursePath = api_get_path(SYS_COURSE_PATH);
  836. $sysUploadPath = api_get_path(SYS_UPLOAD_PATH);
  837. $documentPath = $courseInfo ? $sysCoursePath.$courseInfo['path'].'/document/' : '';
  838. /** @var \DOMElement $element */
  839. foreach ($elements as $element) {
  840. $src = $element->getAttribute('src');
  841. $src = trim($src);
  842. if (strpos($src, $protocol) !== false) {
  843. continue;
  844. }
  845. // It's a reference to a file in the system, do not change it
  846. if (file_exists($src)) {
  847. continue;
  848. }
  849. if (strpos($src, '/main/default_course_document') === 0) {
  850. $element->setAttribute(
  851. 'src',
  852. str_replace('/main/default_course_document', $sysCodePath.'default_course_document', $src)
  853. );
  854. continue;
  855. }
  856. if (strpos($src, '/main/img') === 0) {
  857. $element->setAttribute(
  858. 'src',
  859. str_replace('/main/img/', $sysCodePath.'img/', $src)
  860. );
  861. continue;
  862. }
  863. if (strpos($src, '/app/upload/') === 0) {
  864. $element->setAttribute(
  865. 'src',
  866. str_replace('/app/upload/', $sysUploadPath, $src)
  867. );
  868. continue;
  869. }
  870. if (empty($courseInfo)) {
  871. continue;
  872. }
  873. if (api_get_path(REL_PATH) != '/') {
  874. $oldSrcFixed = str_replace(
  875. api_get_path(REL_PATH).'courses/'.$courseInfo['path'].'/document/',
  876. '',
  877. $src
  878. );
  879. // Try with the dirname if exists
  880. if ($oldSrcFixed == $src) {
  881. if (file_exists($dirName.'/'.$src)) {
  882. $documentPath = '';
  883. $oldSrcFixed = $dirName.'/'.$src;
  884. }
  885. }
  886. } else {
  887. if (strpos($src, 'courses/'.$courseInfo['path'].'/document/') !== false) {
  888. $oldSrcFixed = str_replace('courses/'.$courseInfo['path'].'/document/', '', $src);
  889. } else {
  890. // Try with the dirname if exists
  891. if (file_exists($dirName.'/'.$src)) {
  892. $documentPath = '';
  893. $oldSrcFixed = $dirName.'/'.$src;
  894. } else {
  895. $documentPath = '';
  896. $oldSrcFixed = $src;
  897. }
  898. }
  899. }
  900. $element->setAttribute('src', $documentPath.$oldSrcFixed);
  901. }
  902. return $doc->saveHTML();
  903. }
  904. }