'base_directory',
'K_PATH_URL' => 'base_url',
'K_PATH_FONTS' => 'fonts_directory',
'K_PATH_CACHE' => 'cache_directory',
'K_PATH_IMAGES' => 'image_directory',
'K_BLANK_IMAGE' => 'blank_image',
'K_SMALL_RATIO' => 'small_font_ratio',
);
/**
* Settings from our APPPATH/config/tcpdf.php file
*
* @var array
* @access private
*/
private $_config = array();
/**
* Base font size for answer PDF export
*
* @var int
* @access private
*/
private $_ibaseAnswerFontSize = 12;
/**
* Cell height for answer PDF export
*
* @var int
* @access private
*/
private $_iCellHeight = 6;
/**
* Survey Information (preventing from passing to methods every time)
*
* @var array
* @access private
*/
private $_aSurveyInfo = array();
/**
* Set _config for pdf
* @access public
* @param mixed $tcpdf
* @return
*/
public function setConfig($tcpdf) {
$this->_config=$tcpdf;
}
/**
* Initialize and configure TCPDF with the settings in our config file
*
*/
function __construct() {
# load the config file
require(APPPATH.'config/tcpdf'.EXT);
$this->_config = $tcpdf;
unset($tcpdf);
# set the TCPDF system constants
foreach($this->cfg_constant_map as $const => $cfgkey) {
if(!defined($const)) {
define($const, $this->_config[$cfgkey]);
#echo sprintf("Defining: %s = %s\n
", $const, $this->_config[$cfgkey]);
}
}
# initialize TCPDF
parent::__construct(
$this->_config['page_orientation'],
$this->_config['page_unit'],
$this->_config['page_format'],
$this->_config['unicode'],
$this->_config['encoding'],
$this->_config['enable_disk_cache']
);
# margin settings
$this->SetMargins($this->_config['margin_left'], $this->_config['margin_top'], $this->_config['margin_right']);
# header settings
$this->print_header = $this->_config['header_on'];
#$this->print_header = FALSE;
$this->setHeaderFont(array($this->_config['header_font'], '', $this->_config['header_font_size']));
$this->setHeaderMargin($this->_config['header_margin']);
$this->SetHeaderData();
//$this->SetHeaderData(
// $this->_config['header_logo'],
// $this->_config['header_logo_width'],
// $this->_config['header_title'],
// $this->_config['header_string']
//);
# footer settings
$this->print_footer = $this->_config['footer_on'];
$this->setFooterFont(array($this->_config['footer_font'], '', $this->_config['footer_font_size']));
$this->setFooterMargin($this->_config['footer_margin']);
# page break
$this->SetAutoPageBreak($this->_config['page_break_auto'], $this->_config['footer_margin']);
# cell settings
$this->cMargin = $this->_config['cell_padding'];
$this->setCellHeightRatio($this->_config['cell_height_ratio']);
# document properties
$this->author = $this->_config['author'];
$this->creator = $this->_config['creator'];
# font settings
#$this->SetFont($this->_config['page_font'], '', $this->_config['page_font_size']);
# image settings
$this->setImageScale($this->_config['image_scale']);
}
/**
*
* obsolete
* @param $text
* @param $format
* @return unknown_type
*/
function intopdf($text,$format='')
{
$text = $this->delete_html($text);
$oldformat = $this->FontStyle;
$this->SetFont('',$format,$this->FontSizePt);
$this->Write(5,$text);
$this->ln(5);
$this->SetFont('',$oldformat,$this->FontSizePt);
}
/**
*
* obsolete
* @param $text
* @return unknown_type
*/
function helptextintopdf($text)
{
$oldsize = $this->FontSizePt;
$this->SetFontSize($oldsize-2);
$this->Write(5,$this->delete_html($text));
$this->ln(5);
$this->SetFontSize($oldsize);
}
/**
*
* writes a big title in the page + description
* @param $title
* @param $description
* @return unknown_type
*/
function titleintopdf($title,$description='')
{
if(!empty($title))
{
$title = $this->delete_html($title);
$oldsize = $this->FontSizePt;
$this->SetFontSize($oldsize+4);
$this->Line(5,$this->y,($this->w-5),$this->y);
$this->ln(3);
$this->MultiCell('','',$title,'','C',0);
if(!empty($description) && isset($description))
{
$description = $this->delete_html($description);
$this->ln(7);
$this->SetFontSize($oldsize+2);
$this->MultiCell('','',$description,'','C',0);
$this->ln(2);
}
else
{
$this->ln(4);
}
$this->Line(5,$this->y,($this->w-5),$this->y);
$this->ln(5);
$this->SetFontSize($oldsize);
}
}
/**
*
* Creates a Table with equal cell width and Bold text. Used as Head for equalTable()
* @param $array(0=>)
* @return unknown_type
*/
function tablehead($array)
{
//$maxwidth = array();
$maxwidth = $this->getEqualWidth($array);
$oldStyle = $this->FontStyle;
$this->SetFont($this->FontFamily, 'B', $this->FontSizePt);
for($a=0;$aCell($maxwidth,4,$this->delete_html($array[$a][$b]),0,0,'L');
}
$this->ln();
}
$this->ln(5);
$this->SetFont($this->FontFamily, $oldStyle, $this->FontSizePt);
}
/**
*
* Creates a Table with equal cell width.
* @param $array - table array( 0=> array("td", "td", "td"),
* 1=> array("td", "td", "td"))
* @param $modulo - fills each second row with a light-grey for better visibility. Default is on turn off with 0
* @return unknown_type
*/
function equalTable($array, $modulo=1)
{
//$maxwidth = array();
$maxwidth = $this->getEqualWidth($array);
$this->SetFillColor(220, 220, 220);
for($a=0;$aCell($maxwidth,4,$this->delete_html($array[$a][$b]),0,0,'L',$fill);
}
$this->ln();
}
$this->ln(5);
}
/**
*
* creates a table using the full width of page
* @param $array - table array( 0=> array("td", "td", "td"),
* 1=> array("td", "td", "td"))
* @param $modulo - fills each second row with a light-grey for better visibility. Default is off, turn on with 1
* @return unknown_type
*/
function tableintopdf($array, $modulo=1 )
{
$maxwidth = array();
$maxwidth = $this->getFullWidth($array);
$this->SetFillColor(220, 220, 220);
for($a=0;$a";
$this->Cell($maxwidth[$b],4,$this->delete_html($array[$a][$b]),0,0,'L',$fill);
}
$this->ln();
}
$this->ln(5);
}
/**
*
* creates a table with a bold head using the full width of page
* @param $head - head array( 0=> array("th", "th", "th"))
* @param $table - table array( 0=> array("td", "td", "td"),
* 1=> array("td", "td", "td"))
* @param $modulo - fills each second row with a light-grey for better visibility. Default is on, turn off with 0
* @return unknown_type
*/
function headTable($head, $table, $modulo=1 )
{
$array = array_merge_recursive($head, $table);
//print_r($array);
$maxwidth = array();
$maxwidth = $this->getFullWidth($array);
$this->SetFillColor(220, 220, 220);
for($a=0;$aFontStyle;
$this->SetFont($this->FontFamily, 'B', $this->FontSizePt);
if ($maxwidth[$b] > 140) $maxwidth[$b]=130;
if ($maxwidth[$b] < 25) $maxwidth[$b]=25;
$this->MultiCell($maxwidth[$b],6,$this->delete_html($array[$a][$b]),0,'L',1,$bEndOfCell);
$this->SetFont($this->FontFamily, $oldStyle, $this->FontSizePt);
}
else
{
if ($a==1)
{
$this->SetFillColor(240, 240, 240);
}
//echo $maxwidth[$b]." max $b.Spalte
";
if ($maxwidth[$b] > 140) $maxwidth[$b]=130;
if ($b==0)
{
$iHeight=$this->getStringHeight($maxwidth[$b],$this->delete_html($array[$a][$b]));
$this->MultiCell($maxwidth[$b],$iHeight,$this->delete_html($array[$a][$b]),0,'L',$fill,$bEndOfCell);
}
else
{
$iLines=$this->getStringHeight($maxwidth[$b],$this->delete_html($array[$a][$b]));
if ($iLines>$iHeight) $iHeight=$iLines;
$this->MultiCell($maxwidth[$b],$iHeight,$this->delete_html($array[$a][$b]),0,'L',$fill,$bEndOfCell);
}
}
}
}
$this->ln(5);
}
function getminwidth($array)
{
$width = array();
for($i=0;$idelete_html($array[$i][$j]),1);
foreach($chars as $char)
{
$stringWidth = $stringWidth+$this->GetCharWidth($char);
//echo $stringWidth.": ".$char."
";
}
if($stringWidth!=0 && $stringWidth<8)
$stringWidth = $stringWidth*3;
if(!isset($width[$j])|| $stringWidth>$width[$j])
{
$width[$j] = $stringWidth;
}
}
}
return $width;
}
function getmaxwidth($array)
{
for($i=0;$i=0)
{
if(strlen($this->delete_html($array[($i-1)][$j])) < strlen($this->delete_html($array[$i][$j])))
{
$width[$j] = strlen($this->delete_html($array[$i][$j]));
}
}
else
{
$width[$j]=strlen($this->delete_html($array[$i][$j]));
}
}
}
return ($width);
}
/**
*
* Gets the width for columns in a table based on their Stringlength and the width of the page...
* @param $array
* @return array with column width
*/
function getFullWidth($array)
{
$maxlength = array();
$width = array();
$width = $this->getminwidth($array);
$margins = $this->getMargins();
$deadSpace = $margins['left']+$margins['right'];
$fullWidth = ($this->GetLineWidth()*1000)-$deadSpace;
$faktor = $fullWidth/array_sum($width);
for($i=0;$igetMargins();
$deadSpace = $margins['left']+$margins['right'];
$width = ($this->GetLineWidth()*1000)-$deadSpace;
$count = 0;
for($i=0;$i$count)
{
$count = sizeof($array[$i]);
}
}
}
if($count!=0)
return ($width/$count);
else
return FALSE;
}
function write_out($name)
{
$this->Output($name,"D");
}
function delete_html($text)
{
$text = html_entity_decode($text,null,'UTF-8');
$text = str_replace("\t",' ',$text);
return strip_tags($text);
}
/**
*
* Create Answer PDF document, set metadata and set title
* @param $aSurveyInfo - Survey Information (preventing from passing to methods every time)
* @param $aPdfLanguageSettings - Pdf language settings
* @param $sSiteName - LimeSurvey site name (header and metadata)
* @param $sSurveyName - Survey name (header, metadata and title),
* @param $sDefaultHeaderString - TCPDF header string
* @return unknown_type
*/
function initAnswerPDF($aSurveyInfo, $aPdfLanguageSettings, $sSiteName, $sSurveyName, $sDefaultHeaderString = '')
{
if (empty($sDefaultHeaderString))
$sDefaultHeaderString = $sSurveyName;
$this->_aSurveyInfo = $aSurveyInfo;
$this->SetAuthor($sSiteName);
$this->SetTitle($sSurveyName);
$this->SetSubject($sSurveyName);
$this->SetKeywords($sSurveyName);
$this->SetFont($aPdfLanguageSettings['pdffont']);
$this->_ibaseAnswerFontSize = $aPdfLanguageSettings['pdffontsize'];
$this->_iCellHeight = ceil($this->_ibaseAnswerFontSize / 2);
$this->setLanguageArray($aPdfLanguageSettings['lg']);
$this->addHeader($aPdfLanguageSettings, $sSiteName, $sDefaultHeaderString);
$this->AddPage();
$this->SetFillColor(220, 220, 220);
$this->addTitle($sSurveyName);
}
/**
*
* Add title to pdf
* @param $sTitle - Title
* @param $sSubtitle - Subtitle
* @return unknown_type
*/
function addTitle($sTitle, $sSubtitle="")
{
if(!empty($sTitle))
{
$this->ln(1);
$this->SetFontSize($this->_ibaseAnswerFontSize + 6);
$oPurifier = new CHtmlPurifier();
$sTitleHTML = html_entity_decode(stripJavaScript($oPurifier->purify($sTitle)),ENT_COMPAT);
$this->WriteHTMLCell(0, $this->_iCellHeight, $this->getX(), $this->getY(), $sTitleHTML, 0, 1, false, true, 'C');
if (!empty($sSubtitle))
{
$this->ln(1);
$this->SetFontSize($this->_ibaseAnswerFontSize + 2);
$sSubtitleHTML = html_entity_decode(stripJavaScript($oPurifier->purify($sSubtitle)),ENT_COMPAT);
$this->WriteHTMLCell(0, $this->_iCellHeight, $this->getX(), $this->getY(), $sSubtitleHTML, 0, 1, false, true, 'C');
}
$this->ln(6);
$this->SetFontSize($this->_ibaseAnswerFontSize);
}
}
/**
*
* Add header to pdf
* @param $aPdfLanguageSettings - Pdf language settings
* @param $sSiteName - LimeSurvey site name (header and metadata)
* @param $sDefaultHeaderString - TCPDF header string
* @return unknown_type
*/
function addHeader($aPdfLanguageSettings, $sSiteName, $sDefaultHeaderString)
{
$sLogoFileName = Yii::app()->getConfig('pdflogofile');
if (Yii::app()->getConfig('pdfshowheader')=='Y' && file_exists(K_PATH_IMAGES.$sLogoFileName))
{
$sHeaderTitle = Yii::app()->getConfig('pdfheadertitle');
if ($sHeaderTitle == '') $sHeaderTitle = $sSiteName;
$sHeaderString = Yii::app()->getConfig('pdfheaderstring');
if ($sHeaderString == '') $sHeaderString = $sDefaultHeaderString;
$this->SetHeaderData($sLogoFileName, Yii::app()->getConfig('pdflogowidth'), $sHeaderTitle, $sHeaderString);
$this->SetHeaderFont(Array($aPdfLanguageSettings['pdffont'], '', $this->_ibaseAnswerFontSize - 2));
$this->SetFooterFont(Array($aPdfLanguageSettings['pdffont'], '', $this->_ibaseAnswerFontSize - 2));
}
}
/**
*
* Add GID text to PDF
* @param $sFname - Answer field text
* @param $bAllowBreakPage - Allow break cell in two pages
* @return unknown_type
*/
function addGidAnswer($sFname, $bAllowBreakPage=false)
{
$oPurifier = new CHtmlPurifier();
$sAnswerHTML = html_entity_decode(stripJavaScript($oPurifier->purify($sFname)),ENT_COMPAT);
$sData['thissurvey']=$this->_aSurveyInfo;
$sAnswerHTML = templatereplace($sAnswerHTML, array() , $sData, '', $this->_aSurveyInfo['anonymized']=="Y",NULL, array(), true);
$startPage = $this->getPage();
$this->startTransaction();
$this->ln(6);
$this->SetFontSize($this->_ibaseAnswerFontSize + 2);
$this->WriteHTMLCell(0, $this->_iCellHeight, $this->getX(), $this->getY(), $sAnswerHTML, 0, 1, false, true, 'L');
$this->ln(2);
if ($this->getPage() != $startPage && !$bAllowBreakPage)
{
$this->rollbackTransaction(true);
$this->AddPage();
$this->addGidAnswer($sFname,true); // Second param = true avoid an endless loop if a cell is longer than a page
}
else
{
$this->commitTransaction();
}
}
/**
*
* Add answer to PDF
*
* @param $sQuestion - Question field text array
* @param $sResponse - Answer field text array
* @param $bReplaceExpressions - Try to replace LimeSurvey Expressions. This is false when exporting answers PDF from admin GUI
* because we can not interpret expressions so just purify.
* TODO: Find a universal valid method to interpret expressions
* @param $bAllowBreakPage - Allow break cell in two pages
* @return unknown_type
*/
function addAnswer($sQuestion, $sResponse, $bReplaceExpressions=true, $bAllowBreakPage=false)
{
$oPurifier = new CHtmlPurifier();
$sQuestionHTML = str_replace('-oth-','',$sQuestion); // Copied from Writer::stripTagsFull. Really necessary?
$sQuestionHTML = html_entity_decode(stripJavaScript($oPurifier->purify($sQuestionHTML)),ENT_COMPAT);
if ($bReplaceExpressions)
{
$sData['thissurvey']=$this->_aSurveyInfo;
$sQuestionHTML = templatereplace($sQuestionHTML, array() , $sData, '', $this->_aSurveyInfo['anonymized']=="Y",NULL, array(), true);
}
$sResponse = flattenText($sResponse, false, true, 'UTF-8', false);
$startPage = $this->getPage();
$this->startTransaction();
$this->SetFontSize($this->_ibaseAnswerFontSize);
$this->WriteHTMLCell(0, $this->_iCellHeight, $this->getX(), $this->getY(), $sQuestionHTML, 1, 1, true, true, 'L');
$this->MultiCell(0, $this->_iCellHeight, $sResponse, 1, 'L', 0, 1, '', '', true);
$this->ln(2);
if ($this->getPage() != $startPage && !$bAllowBreakPage)
{
$this->rollbackTransaction(true);
$this->AddPage();
$this->addAnswer($sQuestion,$sResponse,$bReplaceExpressions,true); // "Last param = true" prevents an endless loop if a cell is longer than a page
}
else
{
$this->commitTransaction();
}
}
}