_custom_footer = array($font, $style, $size, $content);
}
function Footer()
{
if(!$this->_numberingFooter)
return;
$this->SetY(-15);
$this->SetFont($this->_custom_footer[0], $this->_custom_footer[1], $this->_custom_footer[2]);
$this->SetTextColor(128);
$t = str_replace('{current}', $this->PageNo(), $this->_custom_footer[3]);
$this->Cell(0, 10, $t, 0, 0, 'C');
if(!$this->_numbering)
$this->_numberingFooter=false;
}
// Veuves & Orphelines
/////////////////////////////////////////////////////////////////////////////////////////////
var $_in_block = false;
var $_need_a_break = false;
var $_break_buffer = '';
var $_break_y = 0;
var $_break_objects_buffer = array();
var $_no_record = false; // Don't record subactions
function AcceptPageBreak()
{
// We can't buffer more than one page
if ($this->_need_a_break && $this->y > ($this->h + $this->_break_y))
{
$this->EndBlock();
return false;
}
if ($this->_in_block)
{
$this->_need_a_break = true;
return false;
}
else
{
return parent::AcceptPageBreak();
}
}
function BeginBlock()
{
$this->EndBlock();
$this->_break_y = $this->y;
$this->_in_block = true;
}
function EndBlock()
{
$this->_in_block = false;
if ($this->_need_a_break)
{
$this->_need_a_break = false;
// erease buffer from current page
$this->pages[$this->page] = substr($this->pages[$this->page], 0, -(strlen($this->_break_buffer)));
$this->AddPage();
foreach ($this->_break_objects_buffer as $obj)
{
call_user_func_array(array($this, $obj[0]), $obj[1]);
}
}
$this->_break_y = 0;
$this->_break_buffer = '';
$this->_break_objects_buffer = array();
}
function _out($s)
{
parent::_out($s);
$this->_break_buffer .= $s . "\n";
}
// extending FPDF methods to add a buffer
function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
if ($this->_in_block && !$this->_no_record)
{
$this->_break_objects_buffer[] = array(__FUNCTION__, func_get_args());
}
parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
}
function SetFont($family, $style='', $size=0)
{
if ($this->_in_block && !$this->_no_record)
{
$this->_break_objects_buffer[] = array(__FUNCTION__, func_get_args());
}
parent::SetFont($family, $style, $size);
}
function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
{
if ($this->_in_block && !$this->_no_record)
{
$this->_break_objects_buffer[] = array(__FUNCTION__, func_get_args());
}
$this->_no_record = true;
parent::MultiCell($w, $h, $txt, $border, $align, $fill);
$this->_no_record = false;
}
function Write($h, $txt, $link='')
{
if ($this->_in_block && !$this->_no_record)
{
$this->_break_objects_buffer[] = array(__FUNCTION__, func_get_args());
}
$this->_no_record = true;
parent::Write($h, $txt, $link);
$this->_no_record = false;
}
// Signets ////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
var $OutlineRoot;
function _putbookmarks()
{
if (empty($this->_toc))
return;
$lru = array();
$level = 0;
$nb = count($this->_toc);
foreach($this->_toc as $i=>$o)
{
if($o['level']>0)
{
$parent = $lru[$o['level']-1];
// Set parent and last pointers
$this->_toc[$i]['parent'] = $parent;
$this->_toc[$parent]['last'] = $i;
if($o['level'] > $level)
{
//Level increasing: set first pointer
$this->_toc[$parent]['first']=$i;
}
}
else
$this->_toc[$i]['parent'] = $nb;
if($o['level'] <= $level && $i > 0)
{
//Set prev and next pointers
$prev = $lru[$o['level']];
$this->_toc[$prev]['next']=$i;
$this->_toc[$i]['prev']=$prev;
}
$lru[$o['level']] = $i;
$level = $o['level'];
}
//Outline items
$n = $this->n+1;
foreach($this->_toc as $i=>$o)
{
$this->_newobj();
$this->_out('<_textstring($o['text']));
$this->_out('/Parent '.($n+$o['parent']).' 0 R');
if(isset($o['prev']))
$this->_out('/Prev '.($n+$o['prev']).' 0 R');
if(isset($o['next']))
$this->_out('/Next '.($n+$o['next']).' 0 R');
if(isset($o['first']))
$this->_out('/First '.($n+$o['first']).' 0 R');
if(isset($o['last']))
$this->_out('/Last '.($n+$o['last']).' 0 R');
$this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]',1+2*$o['page'], 0));
$this->_out('/Count 0>>');
$this->_out('endobj');
}
//Outline root
$this->_newobj();
$this->OutlineRoot = $this->n;
$this->_out('<_out('/Last '.($n+$lru[0]).' 0 R>>');
$this->_out('endobj');
}
function _putresources()
{
parent::_putresources();
$this->_putbookmarks();
}
function _putcatalog()
{
parent::_putcatalog();
if(!empty($this->_toc))
{
$this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
$this->_out('/PageMode /UseOutlines');
}
}
// TOC, Summary and Bookmarks /////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
var $_toc = array();
var $_numbering = false;
var $_numberingFooter = false;
var $_numPageNum = 1;
function AddPage($orientation='', $format='')
{
parent::AddPage($orientation,$format);
if($this->_numbering)
{
$this->_numPageNum++;
}
}
function StartPageNums()
{
$this->_numbering = true;
$this->_numberingFooter = true;
}
function StopPageNums()
{
$this->_numbering = false;
}
function PageNo()
{
return $this->_numPageNum;
}
function AddSummary($text, $level=0)
{
$link = $this->AddLink();
$this->SetLink($link, 0);
$this->_toc[] = array(
'text' => $text,
'level' => $level,
'page' => $this->page,
'page_nb' => $this->PageNo(),
'link' => $link,
);
}
function MovePage($from, $to)
{
$to--;
$row_from = $this->pages[$from];
array_splice($this->pages, $to, 0, 0);
$this->pages[$to] = $row_from;
unset($this->pages[$from]);
$this->pages = array_values($this->pages);
// Can't preserve array keys :(
for ($i = count($this->pages) - 1; $i >= 0; $i--)
{
if (isset($this->pages[$i]))
{
$this->pages[$i + 1] = $this->pages[$i];
}
}
unset($this->pages[0]);
$pl = array();
foreach ($this->PageLinks as $k=>$v)
{
if ($k == $from)
{
$pl[$to + 1] = $v;
}
elseif ($k > $to && $k < $from)
{
$pl[$k + 1] = $v;
}
else
{
$pl[$k] = $v;
}
}
ksort($pl);
$this->PageLinks = $pl;
unset($pl);
// Moving link pointers
foreach ($this->links as $k => $v)
{
if ($v[0] >= $to && $v[0] < $from)
{
#echo "L {$v[0]} -> ".($v[0]+1)."
";
$this->links[$k][0] += 1;
}
elseif ($v[0] == $from)
{
#echo "!! $v[0] -> $to
";
$this->links[$k][0] = $to;
}
}
return true;
}
function RenderSummary($options = array())
{
$base = array(
'on_page' => -1,
'font' => 'Courier',
'title_label' => 'Table of contents',
'title_size' => 20,
'text_size' => 12,
);
foreach ($base as $key=>$v)
{
if (!isset($options[$key]))
$options[$key] = $v;
}
if ($options['on_page'] == -1)
$options['on_page'] = $this->page + 1;
//make toc at end
$this->StopPageNums();
$this->AddPage();
$toc_start = $this->page;
$this->SetFont($options['font'],'B', $options['title_size']);
$this->Cell(0, 5, $options['title_label'], 0, 1, 'C');
$this->Ln(10);
foreach($this->_toc as $k=>&$t)
{
// Offset
if($t['level'] > 0)
{
$this->Cell($t['level'] * 8);
$weight = '';
}
else
{
$weight = 'B';
}
$t['text'] = (strlen($t['text']) < 70) ? $t['text'] : substr($t['text'], 0, 70);
$this->SetFont($options['font'], $weight, $options['text_size']);
$strsize = $this->GetStringWidth($t['text']);
$this->Cell($strsize + 2, $this->FontSize + 2, $t['text'], 0, 0, 'L', false, $t['link']);
$this->SetFont($options['font'], '', $options['text_size']);
$PageCellSize = $this->GetStringWidth($t['page_nb']) + 2;
$w = $this->w - $this->lMargin - $this->rMargin - $PageCellSize - ($t['level'] * 8) - ($strsize + 2);
$nb = $w / $this->GetStringWidth('.');
$dots = str_repeat('.', $nb);
//Filling dots
$this->Cell($w, $this->FontSize+2, $dots, 0, 0, 'R', false, $t['link']);
//Page number
$this->Cell($PageCellSize, $this->FontSize + 2, $t['page_nb'], 0, 1, 'R', false, $t['link']);
}
$toc_end = $this->page;
$rel = $toc_end - $toc_start + 1;
for ($i = $toc_start; $i <= $toc_end; $i++)
{
$b = ($i - $toc_start) + $options['on_page'];
#echo "Moving $i to $b
";
$this->MovePage($i, $b);
}
foreach ($this->_toc as &$t)
{
if ($t['page'] >= $options['on_page'])
{
$t['page'] = $t['page'] + $rel;
}
}
array_unshift($this->_toc, array(
'text' => $options['title_label'],
'page' => $options['on_page'],
'page_nb'=> $options['on_page'],
'level' => 0,
)
);
}
// WriteHTML ////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
var $B = 0;
var $I = 0;
var $U = 0;
var $HREF = '';
var $PRE = false;
function WriteHTML($html)
{
$this->B = $this->U = $this->I = 0;
$this->HREF = '';
//remove all unsupported tags
$html=strip_tags($html,"
- ");
$html = preg_replace('![\n\r]!', '', $html);
$a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
$skip = false;
foreach($a as $i=>$e)
{
if (!$skip)
{
if($i%2==0)
{
if($this->HREF)
{
$this->PutLink($this->HREF, $e);
$skip = true;
}
else
{
$this->Write(6,html_entity_decode($e));
}
}
else
{
//Tag
$e = trim($e);
if (preg_match('!^/\s*(\w+)!', $e, $match))
{
$this->CloseTag(strtoupper($match[1]));
}
else
{
//Extract attributes
$a2=explode(' ',$e);
$tag=strtoupper(array_shift($a2));
$attr=array();
foreach($a2 as $v)
{
if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
{
$attr[strtoupper($a3[1])]=$a3[2];
}
}
$this->OpenTag($tag, $attr);
}
}
} else {
$this->HREF='';
$skip=false;
}
}
}
function OpenTag($tag,$attr)
{
//Opening tag
switch($tag){
case 'STRONG':
case 'B':
$this->SetStyle('B',true);
break;
case 'H1':
$this->Ln(8);
$this->SetFontSize(22);
break;
case 'H2':
$this->Ln(8);
$this->SetFontSize(18);
$this->SetStyle('B',true);
break;
case 'H3':
$this->Ln(8);
$this->SetFontSize(16);
$this->SetStyle('B',true);
break;
case 'H4':
case 'H5':
case 'H6':
$this->Ln(8);
$this->SetFontSize(12);
$this->SetStyle('B',true);
break;
case 'PRE':
$this->SetFont('Courier','',11);
$this->SetFontSize(11);
$this->SetStyle('B',false);
$this->SetStyle('I',false);
$this->PRE=true;
break;
case 'BLOCKQUOTE':
$this->SetStyle('I',true);
$this->Ln(3);
break;
case 'I':
case 'EM':
$this->SetStyle('I',true);
break;
case 'U':
$this->SetStyle('U',true);
break;
case 'A':
$this->HREF=$attr['HREF'];
break;
case 'IMG':
if(isset($attr['SRC'])) {
$this->Image($attr['SRC'], $this->GetX(), $this->GetY());
$this->Ln(3);
}
break;
case 'LI':
$this->Ln();
$this->Write(6,' - ');
break;
case 'BR':
$this->Ln();
break;
case 'P':
$this->Ln(8);
break;
}
}
function CloseTag($tag)
{
//Closing tag
switch ($tag)
{
case 'H1':
case 'H2':
case 'H3':
case 'H4':
case 'H5':
case 'H6':
$this->Ln(8);
$this->SetFont('Times','',12);
$this->SetFontSize(12);
$this->SetStyle('U',false);
$this->SetStyle('B',false);
break;
case 'PRE':
$this->SetFont('Times','',12);
$this->SetFontSize(12);
$this->PRE=false;
break;
case 'BLOCKQUOTE':
$this->SetStyle('I',false);
$this->Ln(3);
break;
case 'STRONG':
case 'B':
$this->SetStyle('B',false);
break;
case 'EM':
case 'I':
$this->SetStyle('I',false);
break;
case 'A':
$this->HREF = '';
break;
}
}
function SetStyle($tag, $enable)
{
if ($enable)
$this->{$tag} += 1;
else
$this->{$tag} -= 1;
$style='';
foreach(array('B','I','U') as $s)
{
if ($this->{$s} > 0)
{
$style .= $s;
}
}
$this->SetFont('', $style);
}
function PutLink($URL,$txt)
{
//Put a hyperlink
$this->SetTextColor(0,0,255);
$this->SetStyle('U', true);
$this->Write(6, html_entity_decode($txt), $URL);
$this->SetStyle('U', false);
$this->SetTextColor(0, 0, 0);
if ($URL != $txt)
{
$this->Write(6, ' (');
$this->SetStyle('I', true);
$this->Write(6, $URL, $URL);
$this->SetStyle('I', false);
$this->Write(6, ')');
}
}
// Temporary fix
function _escape($str)
{
$str = utf8_decode($str);
return parent::_escape($str);
}
}
?>