pdf.lib.php 38 KB

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