pdf.lib.php 35 KB

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