export_htmldoc2pdf.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once '../inc/global.inc.php';
  4. api_protect_course_script();
  5. api_block_anonymous_users();
  6. $file = Security::remove_XSS($_GET['file']);
  7. $file_info = pathinfo($file);
  8. $dirname = str_replace("\\", '/', $file_info['dirname']);
  9. $filename = $file_info['basename'];
  10. $filename =str_replace('_',' ',$filename);
  11. $extension = $file_info['extension'];
  12. if (!($extension == 'html' || $extension == 'htm')) {
  13. exit;
  14. }
  15. if($extension == 'html'){
  16. $filename =basename($filename,'.html');
  17. }elseif($extension == 'htm'){
  18. $filename =basename($filename,'.htm');
  19. }
  20. define('_MPDF_PATH', api_get_path(LIBRARY_PATH).'mpdf/');
  21. require_once _MPDF_PATH.'mpdf.php';
  22. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  23. $document_html = @file_get_contents($filepath.$file);
  24. //clean styles and javascript document
  25. $clean_search = array(
  26. '@<script[^>]*?>.*?</script>@si',
  27. '@<style[^>]*?>.*?</style>@siU'
  28. );
  29. $document_html = preg_replace($clean_search, '', $document_html);
  30. //absolute path for frames.css //TODO: necessary?
  31. $absolute_css_path=api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
  32. $document_html=str_replace('href="./css/frames.css"',$absolute_css_path,$document_html);
  33. //replace relative path by absolute path for resources
  34. $document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
  35. $document_html= str_replace('../','',$document_html);
  36. $src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  37. $document_html= str_replace('src="',$src_http_www,$document_html);
  38. $document_html= str_replace('temp_template_path', 'src="/chamilo/main/default_course_document/', $document_html);// restore src templates
  39. //
  40. api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data.
  41. $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through
  42. // $_GET[] too, as it is done with file name.
  43. // At the moment the title is retrieved from the html document itself.
  44. if (empty($title)) {
  45. $title = $filename; // Here file name is expected to contain ASCII symbols only.
  46. }
  47. $pdf = new mPDF('UTF-8', 'A4', '', '', 30, 20, 27, 25, 16, 13, 'P');
  48. $pdf->SetBasePath($basehref);
  49. $pdf->directionality = api_get_text_direction(); // TODO: To be read from the html document.
  50. $pdf->useOnlyCoreFonts = true;
  51. $pdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins
  52. $pdf->defaultheaderfontsize = 10; // in pts
  53. $pdf->defaultheaderfontstyle = B; // blank, B, I, or BI
  54. $pdf->defaultheaderline = 1; // 1 to include line below header/above footer
  55. $pdf->defaultfooterfontsize = 12; // in pts
  56. $pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
  57. $pdf->defaultfooterline = 1; // 1 to include line below header/above footer
  58. $pdf->SetHeader($filename.'|||');// ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
  59. $pdf->SetFooter('||{PAGENO}'); // defines footer for Odd and Even Pages - placed at Outer margin
  60. $pdf->SetAuthor('Documents Chamilo');
  61. $pdf->SetTitle($title);
  62. $pdf->SetSubject('Exported from Chamilo Documents');
  63. $pdf->SetKeywords('Chamilo Documents');
  64. $pdf->WriteHTML($document_html,2);
  65. $pdf->Output(replace_dangerous_char($title.'.pdf'), 'D');