Browse Source

fix styles and images when export a html to pdf from documents

Juan Carlos Raña 14 years ago
parent
commit
e37afb4505
2 changed files with 34 additions and 7 deletions
  1. 1 3
      main/document/document.inc.php
  2. 33 4
      main/document/export_htmldoc2pdf.php

+ 1 - 3
main/document/document.inc.php

@@ -341,9 +341,7 @@ function build_edit_icons($curdirpath, $type, $path, $visibility, $id, $is_templ
 		} else {
 			$modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;remove_as_template='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/wizard_gray_small.gif" border="0" title="'.get_lang('RemoveAsTemplate').'" alt=""'.get_lang('RemoveAsTemplate').'" /></a>';
 		}
-		////////// commented code until implementation is completed (Feature #1768)
-		//$modify_icons .= '<a href="export_htmldoc2pdf.php?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;file='.urlencode($path).$req_gid.'"><img src="../img/file_pdf_small.gif" border="0" title="'.get_lang('Export2PDF').'" alt="" /></a>';
-		////////////
+		$modify_icons .= '<a href="export_htmldoc2pdf.php?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;file='.urlencode($path).$req_gid.'"><img src="../img/file_pdf_small.gif" border="0" title="'.get_lang('Export2PDF').'" alt="" /></a>';
 	}
 
 	return $modify_icons;

+ 33 - 4
main/document/export_htmldoc2pdf.php

@@ -3,6 +3,9 @@
 
 require_once '../inc/global.inc.php';
 
+api_protect_course_script();
+api_block_anonymous_users();
+
 $file = Security::remove_XSS($_GET['file']);
 $file_info = pathinfo($file);
 $filename = $file_info['basename'];
@@ -12,12 +15,38 @@ if (!($extension == 'html' || $extension == 'htm')) {
 	exit;
 }
 
+if($extension == 'html'){
+	$head_title_filename =basename($file,'.html');
+}elseif($extension == 'htm'){
+	$head_title_filename =basename($file,'.htm');
+}
+
 define('_MPDF_PATH', api_get_path(LIBRARY_PATH).'mpdf/');
 require_once _MPDF_PATH.'mpdf.php';
 
 $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
 
 $document_html = @file_get_contents($filepath.$file);
+
+//clean styles and javascript document
+$clean_search = array(
+	'@<script[^>]*?>.*?</script>@si',
+	'@<style[^>]*?>.*?</style>@siU'
+	);
+$document_html = preg_replace($clean_search, '', $document_html);
+
+//absolute path for frames.css //TODO: necessary?
+$absolute_css_path=api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
+$document_html=str_replace('href="./css/frames.css"',$absolute_css_path,$document_html);
+
+//replace relative path by absolute path for resources
+$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
+$document_html= str_replace('../','',$document_html);
+$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
+$document_html= str_replace('src="',$src_http_www,$document_html);
+$document_html= str_replace('temp_template_path', 'src="/chamilo/main/default_course_document/', $document_html);// restore src templates
+
+//
 api_set_encoding_html($document_html, 'UTF-8');	// The library mPDF expects UTF-8 encoded input data.
 
 $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');	// TODO: Maybe it is better idea the title to be passed through
@@ -43,14 +72,14 @@ $pdf->defaultfooterfontsize = 12;	// in pts
 $pdf->defaultfooterfontstyle = B;	// blank, B, I, or BI
 $pdf->defaultfooterline = 1;		// 1 to include line below header/above footer
 
-$pdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
-$pdf->SetFooter('{PAGENO}');		// defines footer for Odd and Even Pages - placed at Outer margin
+$pdf->SetHeader($head_title_filename.'|||');
+$pdf->SetFooter('||{PAGENO}');		// defines footer for Odd and Even Pages - placed at Outer margin
 
 $pdf->SetAuthor('Documents Chamilo');
 $pdf->SetTitle($title);
 $pdf->SetSubject('Exported from Chamilo Documents');
 $pdf->SetKeywords('Chamilo Documents');
 
-$pdf->WriteHTML($document_html);
+$pdf->WriteHTML($document_html,2);
 
-$pdf->Output(replace_dangerous_char($title.'.pdf'), 'D');
+$pdf->Output(replace_dangerous_char($title.'.pdf'), 'D');