pdf.lib.php 35 KB

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