pdf.lib.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 $course_data
  598. */
  599. public function set_header($course_data)
  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($course_data['code'])) {
  606. $teacher_list = CourseManager::get_teacher_list_from_course_code($course_data['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 $course_data 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($course_data, $complete = true)
  659. {
  660. $course_code = null;
  661. if (!empty($course_data)) {
  662. $course_code = $course_data['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($course_code);
  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 ($course_code) {
  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($course_data);
  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. * Fix images source paths to allow export to pdf.
  794. *
  795. * @param string $documentHtml
  796. * @param array $courseInfo
  797. * @param string $dirName
  798. *
  799. * @return string
  800. */
  801. private static function fixImagesPaths($documentHtml, array $courseInfo, $dirName = '')
  802. {
  803. $documentHtml = '<?xml encoding="utf-8" ?>'.$documentHtml;
  804. $doc = new DOMDocument();
  805. @$doc->loadHTML($documentHtml);
  806. $elements = $doc->getElementsByTagName('img');
  807. if (empty($elements)) {
  808. return $doc->saveHTML();
  809. }
  810. $protocol = api_get_protocol();
  811. $sysCodePath = api_get_path(SYS_CODE_PATH);
  812. $sysCoursePath = api_get_path(SYS_COURSE_PATH);
  813. $sysUploadPath = api_get_path(SYS_UPLOAD_PATH);
  814. $documentPath = $courseInfo ? $sysCoursePath.$courseInfo['path'].'/document/' : '';
  815. /** @var \DOMElement $element */
  816. foreach ($elements as $element) {
  817. $src = $element->getAttribute('src');
  818. $src = trim($src);
  819. if (strpos($src, $protocol) !== false) {
  820. continue;
  821. }
  822. // It's a reference to a file in the system, do not change it
  823. if (file_exists($src)) {
  824. continue;
  825. }
  826. if (strpos($src, '/main/default_course_document') === 0) {
  827. $element->setAttribute(
  828. 'src',
  829. str_replace('/main/default_course_document', $sysCodePath.'default_course_document', $src)
  830. );
  831. continue;
  832. }
  833. if (strpos($src, '/main/img') === 0) {
  834. $element->setAttribute(
  835. 'src',
  836. str_replace('/main/img/', $sysCodePath.'img/', $src)
  837. );
  838. continue;
  839. }
  840. if (strpos($src, '/app/upload/') === 0) {
  841. $element->setAttribute(
  842. 'src',
  843. str_replace('/app/upload/', $sysUploadPath, $src)
  844. );
  845. continue;
  846. }
  847. if (empty($courseInfo)) {
  848. continue;
  849. }
  850. if (api_get_path(REL_PATH) != '/') {
  851. $oldSrcFixed = str_replace(
  852. api_get_path(REL_PATH).'courses/'.$courseInfo['path'].'/document/',
  853. '',
  854. $src
  855. );
  856. // Try with the dirname if exists
  857. if ($oldSrcFixed == $src) {
  858. if (file_exists($dirName.'/'.$src)) {
  859. $documentPath = '';
  860. $oldSrcFixed = $dirName.'/'.$src;
  861. }
  862. }
  863. } else {
  864. if (strpos($src, 'courses/'.$courseInfo['path'].'/document/') !== false) {
  865. $oldSrcFixed = str_replace('courses/'.$courseInfo['path'].'/document/', '', $src);
  866. } else {
  867. // Try with the dirname if exists
  868. if (file_exists($dirName.'/'.$src)) {
  869. $documentPath = '';
  870. $oldSrcFixed = $dirName.'/'.$src;
  871. } else {
  872. $documentPath = '';
  873. $oldSrcFixed = $src;
  874. }
  875. }
  876. }
  877. $element->setAttribute('src', $documentPath.$oldSrcFixed);
  878. }
  879. return $doc->saveHTML();
  880. }
  881. }