Converting the pdf.lib.php class from static to obj + fixing wiki to pdf method see BT#1670 & #1909

skala
Julio Montoya 14 years ago
parent 993845fca7
commit f5a9f061f3
  1. 5
      main/inc/lib/document.lib.php
  2. 321
      main/inc/lib/pdf.lib.php
  3. 3
      main/newscorm/learnpath.class.php
  4. 1
      main/wiki/export_mpdf.php
  5. 27
      main/wiki/index.php
  6. 108
      main/wiki/wiki.inc.php

@ -1758,10 +1758,11 @@ class DocumentManager {
public function export_to_pdf($document_id, $course_code) {
require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
$course_data = api_get_course_info($course_code);
$course_data = api_get_course_info($course_code);
$document_data = self::get_document_data_by_id($document_id, $course_code);
$file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$document_data['path'];
PDF::html_to_pdf($file_path, $document_data['title'], $course_code);
$pdf = new PDF();
$pdf->html_to_pdf($file_path, $document_data['title'], $course_code);
}
}
//end class DocumentManager

@ -7,21 +7,25 @@ define('_MPDF_PATH', api_get_path(LIBRARY_PATH).'mpdf/');
require_once _MPDF_PATH.'mpdf.php';
class PDF {
private function __construct() {
}
var $pdf;
public function __construct() {
$this->pdf = $pdf = new mPDF('UTF-8', 'A4', '', '', 30, 20, 27, 25, 16, 13, 'P');
}
/**
* Converts an html file to a pdf
* @param mixed could be an html path or an array with html paths
* @param string course code
* @return string the pdf path
* Converts an html file to PDF
* @param mixed could be an html file path or an array with paths example: /var/www/myfile.html or array('/myfile.html','myotherfile.html')
* @param string pdf name
* @param string course code (if you are using html that are located in the document tool you must provide this)
* @return
*/
public function html_to_pdf($html_file_array, $pdf_name = '', $course_code = null) {
if (empty($html_file_array)) {
return false;
}
}
if (is_array($html_file_array)) {
if (count($html_file_array) == 0)
return false;
@ -42,94 +46,10 @@ class PDF {
'@<script[^>]*?>.*?</script>@si',
'@<style[^>]*?>.*?</style>@siU'
);
//mPDF($codepage='win-1252',$format='A4',$default_font_size=0,$default_font='',$mgl=15,$mgr=15,$mgt=16,$mgb=16,$mgh=9,$mgf=9, $orientation='P')
$pdf = new mPDF('UTF-8', 'A4', '', '', 30, 20, 27, 25, 16, 13, 'P');
// $pdf->SetBasePath($basehref);
//Formatting the pdf
self::format_pdf($course_code);
$pdf->directionality = api_get_text_direction(); // TODO: To be read from the html document.
$pdf->useOnlyCoreFonts = true;
$pdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins
$pdf->defaultheaderfontsize = 10; // in pts
$pdf->defaultheaderfontstyle = BI; // blank, B, I, or BI
$pdf->defaultheaderline = 1; // 1 to include line below header/above footer
$my_header = self::get_header($course_code);
$pdf->SetHeader($my_header);// ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
$pdf->defaultfooterfontsize = 12; // in pts
$pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
$pdf->defaultfooterline = 1; // 1 to include line below header/above footer
//@todo remove this and use a simpler way
$footer = array (
'odd' => array (
'L' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'C' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'R' => array (
'content' => '{PAGENO}',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'line' => 1,
),
'even' => array (
'L' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'C' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'R' => array (
'content' => '{PAGENO}',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'line' => 1,
),
);
$pdf->SetFooter($footer); // defines footer for Odd and Even Pages - placed at Outer margin http://mpdf1.com/manual/index.php?tid=151&searchstring=setfooter
if (api_get_setting('pdf_export_watermark_enable') == 'true') {
$watermark_file = self::get_watermark($course_code);
if (!empty($watermark_file)) {
//http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
$pdf->SetWatermarkImage($watermark_file);
$pdf->showWatermarkImage = true;
}
}
/*$pdf->SetAuthor('Documents Chamilo');
$pdf->SetTitle('title');
$pdf->SetSubject('Exported from Chamilo Documents');
$pdf->SetKeywords('Chamilo Documents');
*/
foreach ($html_file_array as $html_file) {
if (!file_exists($html_file)) {
continue;
@ -193,9 +113,9 @@ class PDF {
// At the moment the title is retrieved from the html document itself.
if (empty($title)) {
$title = $filename; // Here file name is expected to contain ASCII symbols only.
}
}
//var_dump($document_html);
$pdf->WriteHTML($document_html,2);
$this->pdf->WriteHTML($document_html,2);
}
if (empty($pdf_name)) {
@ -203,10 +123,106 @@ class PDF {
} else {
$output_file = $pdf_name.'.pdf';
}
$result = $pdf->Output($output_file, 'D'); /// F to save the pdf in a file
$result = $this->pdf->Output($output_file, 'D'); /// F to save the pdf in a file
exit;
}
/**
* Converts an html string to PDF
* @param string valid html
* @param string pdf name
* @param string course code (if you are using html that are located in the document tool you must provide this)
* @return
*/
public function content_to_pdf($document_html, $css = '', $pdf_name = '', $course_code = null) {
if (empty($document_html)) {
return false;
}
//clean styles and javascript document
$clean_search = array (
'@<script[^>]*?>.*?</script>@si',
'@<style[^>]*?>.*?</style>@siU'
);
//Formatting the pdf
self::format_pdf($course_code);
if (!empty($course_code)) {
$course_data = api_get_course_info($course_code);
}
$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);
//$document_html=str_replace('<link rel="stylesheet" http://my.chamilo.net/main/css/chamilo/frames.css type="text/css" />','', $document_html);
$document_html= str_replace('../../','',$document_html);
$document_html= str_replace('../','',$document_html);
$document_html= str_replace('courses/'.$course_code.'/document/','',$document_html);
if (!empty($course_data['path'])) {
$document_path = api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
$doc = new DOMDocument();
$result = @$doc->loadHTML($document_html);
//Fixing only images @todo do the same thing with other elements
$elements = $doc->getElementsByTagName('img');
$replace_img_elements = array();
if (!empty($elements)) {
foreach($elements as $item) {
$old_src = $item->getAttribute('src');
//$old_src= str_replace('../','',$old_src);
if (strpos($old_src, 'http') === false) {
if (strpos($old_src, '/main/default_course_document') === false) {
if (strpos($old_src, '/main/inc/lib/') === false) {
$document_html= str_replace($old_src, $document_path.$old_src, $document_html);
//var_dump($old_src, $document_path.$old_src);
}
}
}
}
}
}
//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('src="/', 'temp_template_path', $document_html);// before save src templates not apply
//$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply
//$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
//$document_html= str_replace('src="',$src_http_www, $document_html);
//$document_html= str_replace('temp_template_path', 'src="/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
// $_GET[] too, as it is done with file name.
// At the moment the title is retrieved from the html document itself.
/* if (empty($title)) {
$title = $filename; // Here file name is expected to contain ASCII symbols only.
}*/
if (!empty($css)) {
$this->pdf->WriteHTML($css, 1);
}
$this->pdf->WriteHTML($document_html,2);
if (empty($pdf_name)) {
$output_file = 'pdf_'.date('Y-m-d-his').'.pdf';
} else {
$output_file = $pdf_name.'.pdf';
}
$result = $this->pdf->Output($output_file, 'D'); /// F to save the pdf in a file
exit;
}
/**
* Gets the watermark from the platform or a course
* @param string course code (optional)
@ -258,8 +274,7 @@ class PDF {
} else {
$store_path = api_get_path(SYS_CODE_PATH).'default_course_document'; // course path
$web_path = api_get_path(WEB_CODE_PATH).'default_course_document/'.api_get_current_access_url_id().'_pdf_watermark.png';
}
}
$course_image = $store_path.'/'.api_get_current_access_url_id().'_pdf_watermark.png';
$extension = strtolower(substr(strrchr($filename, '.'), 1));
$result = false;
@ -296,15 +311,113 @@ class PDF {
* Returns the default header
*/
public function get_header($course_code = null) {
$header = '';
$header = '';
if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') {
$header = api_get_course_setting('pdf_export_watermark_text');
} else {
$header = api_get_setting('pdf_export_watermark_text');
}
return $header;
return $header;
}
public function set_footer() {
$this->pdf->defaultfooterfontsize = 12; // in pts
$this->pdf->defaultfooterfontstyle = B; // blank, B, I, or BI
$this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer
//@todo remove this and use a simpler way
$footer = array (
'odd' => array (
'L' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'C' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'R' => array (
'content' => '{PAGENO}',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'line' => 1,
),
'even' => array (
'L' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'C' => array (
'content' => '',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'R' => array (
'content' => '{PAGENO}',
'font-size' => 10,
'font-style' => 'B',
'font-family' => 'serif',
'color'=>'#000000'
),
'line' => 1,
),
);
}
$this->pdf->SetFooter($footer); // defines footer for Odd and Even Pages - placed at Outer margin http://mpdf1.com/manual/index.php?tid=151&searchstring=setfooter
}
public function set_header($course_code) {
// $pdf->SetBasePath($basehref);
$this->pdf->defaultheaderfontsize = 10; // in pts
$this->pdf->defaultheaderfontstyle = BI; // blank, B, I, or BI
$this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer
$my_header = self::get_header($course_code);
$this->pdf->SetHeader($my_header);// ('{DATE j-m-Y}|{PAGENO}/{nb}|'.$title);
}
public function format_pdf($course_code) {
/*$pdf->SetAuthor('Documents Chamilo');
$pdf->SetTitle('title');
$pdf->SetSubject('Exported from Chamilo Documents');
$pdf->SetKeywords('Chamilo Documents');
*/
$this->pdf->directionality = api_get_text_direction(); // TODO: To be read from the html document.
$this->pdf->useOnlyCoreFonts = true;
$this->pdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins
//Adding watermark
if (api_get_setting('pdf_export_watermark_enable') == 'true') {
$watermark_file = self::get_watermark($course_code);
if (!empty($watermark_file)) {
//http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark
$this->pdf->SetWatermarkImage($watermark_file);
$this->pdf->showWatermarkImage = true;
}
}
self::set_header($course_code);
self::set_footer();
}
}
?>

@ -8384,7 +8384,8 @@ EOD;
}
}
require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
$result = PDF::html_to_pdf($files_to_export, '', $this->cc);
$pdf = new PDF();
$result = $pdf->html_to_pdf($files_to_export, '', $this->cc);
return $result;
}

@ -5,6 +5,7 @@
* Export html to pdf
* @author Juan Carlos Raña <herodoto@telefonica.net>, initial code, 2009
* @author Ivan Tcholakov <ivantcholakov@gmail.com>, 2010
* @deprecated now we use the pdf.lib.php library for all pdf export issues
*/
require '../inc/global.inc.php';

@ -54,10 +54,10 @@ function setFocus(){
// Database table definition
$tbl_wiki = Database::get_course_table(TABLE_WIKI);
$tbl_wiki_discuss = Database::get_course_table(TABLE_WIKI_DISCUSS);
$tbl_wiki_mailcue = Database::get_course_table(TABLE_WIKI_MAILCUE);
$tbl_wiki_conf = Database::get_course_table(TABLE_WIKI_CONF);
$tbl_wiki = Database::get_course_table(TABLE_WIKI);
$tbl_wiki_discuss = Database::get_course_table(TABLE_WIKI_DISCUSS);
$tbl_wiki_mailcue = Database::get_course_table(TABLE_WIKI_MAILCUE);
$tbl_wiki_conf = Database::get_course_table(TABLE_WIKI_CONF);
/*
Constants and variables
*/
@ -66,8 +66,8 @@ $tool_name = get_lang('ToolWiki');
$MonthsLong = array (get_lang("JanuaryLong"), get_lang("FebruaryLong"), get_lang("MarchLong"), get_lang("AprilLong"), get_lang("MayLong"), get_lang("JuneLong"), get_lang("JulyLong"), get_lang("AugustLong"), get_lang("SeptemberLong"), get_lang("OctoberLong"), get_lang("NovemberLong"), get_lang("DecemberLong"));
//condition for the session
$session_id = api_get_session_id();
$condition_session = api_get_session_condition($session_id);
$session_id = api_get_session_id();
$condition_session = api_get_session_condition($session_id);
/*
ACCESS
@ -116,6 +116,12 @@ if ($_SESSION['_gid'] OR $_GET['group_id']) {
$groupfilter='group_id=0';
}
if ($_POST['action']=='export_to_pdf' && isset($_POST['wiki_id']) ) {
export_to_pdf($_POST['wiki_id'], api_get_course_id());
}
Display::display_header($tool_name, 'Wiki');
$is_allowed_to_edit = api_is_allowed_to_edit(false,true);
@ -338,15 +344,12 @@ if ($_GET['action']=='deletewiki'){
if ($_GET['action']=='discuss' && $_POST['Submit']) {
Display::display_confirmation_message(get_lang('CommentAdded'));
Display::display_confirmation_message(get_lang('CommentAdded'));
}
/*
-----------------------------------------------------------
WIKI WRAPPER
-----------------------------------------------------------
*/
/* WIKI WRAPPER */
echo "<div id='wikiwrapper'>";

@ -1,17 +1,14 @@
<?php
/* For licensing terms, see /license.txt */
/**
*
* @Author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
* @Author Juan Carlos Raña <herodoto@telefonica.net>
* @author Juan Carlos Raña <herodoto@telefonica.net>
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium*
* @author Julio Montoya <gugli100@gmail.com> using the pdf.lib.php library
*
* @package chamilo.wiki
*/
/*
FUNCTIONS FOR WIKI
*/
/* FUNCTIONS FOR WIKI */
/**
@ -883,7 +880,8 @@ function display_wiki_entry($newtitle)
echo '<span style="float:right;padding-top:5px;">';
// Modified by Ivan Tcholakov, 28-JAN-2010.
//echo '<form name="form_export2PDF" method="post" action="export_html2pdf.php" target="_blank, fullscreen">';
echo '<form name="form_export2PDF" method="post" action="export_mpdf.php" target="_blank, fullscreen">';
/*echo '<form name="form_export2PDF" method="post" action="export_mpdf.php" target="_blank, fullscreen">';
//
echo '<input type=hidden name="titlePDF" value="'.api_htmlentities($title, ENT_QUOTES, $charset).'">';
$clean_pdf_content=trim(preg_replace("/\[\[|\]\]/", " ", $content));
@ -892,7 +890,14 @@ function display_wiki_entry($newtitle)
echo '<input type=hidden name="contentPDF" value="'.api_htmlentities($clean_pdf_content, ENT_QUOTES, $charset).'">';
echo '<input type="image" src="../img/wiki/wexport2pdf.gif" border ="0" title="'.get_lang('ExportToPDF').'" alt="'.get_lang('ExportToPDF').'" style=" border:none; margin-top: -6px">';
echo '</form>';
echo '</span>';
echo '</span>';*/
echo '<form name="form_export2PDF" method="post" action="index.php">';
echo '<input type="hidden" name="action" value="export_to_pdf">';
echo '<input type="hidden" name="wiki_id" value="'.$row['id'].'">';
echo '<input type="image" src="../img/wiki/wexport2pdf.gif" border ="0" title="'.get_lang('ExportToPDF').'" alt="'.get_lang('ExportToPDF').'" style=" border:none; margin-top: -6px">';
echo '</form>';
echo '</span>';
//page action: copy last version to doc area
if(api_is_allowed_to_edit(false,true) || api_is_platform_admin())
@ -1507,6 +1512,8 @@ function check_emailcue($id_or_ref, $type, $lastime='', $lastuser='')
$_clean['group_id']=(int)$_SESSION['_gid'];
$session_id=api_get_session_id();
$group_properties = GroupManager :: get_group_properties($_clean['group_id']);
$group_name= $group_properties['name'];
@ -1696,21 +1703,21 @@ function export2doc($wikiTitle, $wikiContents, $groupId)
$session_id=api_get_session_id();
$template =
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{LANGUAGE}" lang="{LANGUAGE}">
<head>
<title>{TITLE}</title>
<meta http-equiv="Content-Type" content="text/html; charset={ENCODING}" />
<style type="text/css" media="screen, projection">
/*<![CDATA[*/
{CSS}
/*]]>*/
</style>
</head>
<body dir="{TEXT_DIRECTION}">
{CONTENT}
</body>
</html>';
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{LANGUAGE}" lang="{LANGUAGE}">
<head>
<title>{TITLE}</title>
<meta http-equiv="Content-Type" content="text/html; charset={ENCODING}" />
<style type="text/css" media="screen, projection">
/*<![CDATA[*/
{CSS}
/*]]>*/
</style>
</head>
<body dir="{TEXT_DIRECTION}">
{CONTENT}
</body>
</html>';
$css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/default.css';
if (file_exists($css_file)) {
@ -1767,6 +1774,42 @@ function export2doc($wikiTitle, $wikiContents, $groupId)
// TODO: link to go document area
}
function export_to_pdf($id, $course_code) {
require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
$data = get_wiki_data($id);
$content_pdf = api_html_entity_decode($data['content'], ENT_QUOTES, api_get_system_encoding());
$title_pdf = api_html_entity_decode($data['title'], ENT_QUOTES, api_get_system_encoding());
$title_pdf = api_utf8_encode($title_pdf, api_get_system_encoding());
$content_pdf = api_utf8_encode($content_pdf, api_get_system_encoding());
$html='
<!-- defines the headers/footers - this must occur before the headers/footers are set -->
<!--mpdf
<pageheader name="odds" content-left="'.$title_pdf.'" header-style-left="color: #880000; font-style: italic;" line="1" />
<pagefooter name="odds" content-right="{PAGENO}/{nb}" line="1" />
<!-- set the headers/footers - they will occur from here on in the document -->
<!--mpdf
<setpageheader name="odds" page="odd" value="on" show-this-page="1" />
<setpagefooter name="odds" page="O" value="on" />
mpdf-->'.$content_pdf;
$css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/print.css';
if (file_exists($css_file)) {
$css = @file_get_contents($css_file);
} else {
$css = '';
}
$pdf = new PDF();
$pdf->content_to_pdf($html, $css, $title_pdf, $course_code);
exit;
}
/**
* Function prevent double post (reload or F5)
@ -2108,4 +2151,21 @@ function two_digits($number)
return ($number < 10) ? '0'.$number : $number;
}
/**
* Get wiki information
* @param int wiki id
* @return array wiki data
*/
function get_wiki_data($id) {
global $tbl_wiki;
$id = intval($id);
$sql='SELECT * FROM '.$tbl_wiki.' WHERE id = '.$id.' ';
$result=Database::query($sql);
$data = array();
while ($row=Database::fetch_array($result,'ASSOC')) {
$data = $row;
}
return $data;
}
?>
Loading…
Cancel
Save