pdf.lib.php 35 KB

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