index.php
<?php
require('bookmark.php');
class PDF_Index extends PDF_Bookmark
{
/**
* This function create an index with the defined Bookmarks
* @param string $title the index title
* @param string $size the text size
*/
function CreateIndex($title='Index',$size=20){
//Index title
$this->SetFontSize($size);
$this->Cell(0,5,$title,0,1,'C');
$this->SetFontSize($size-5);
$this->Ln(10);
/*
* Calculate the size of the biggest page number to define the colonne size
*/
$outlines_size=sizeof($this->outlines);
$PageCellSize=$this->GetStringWidth('p. '.$this->outlines[$outlines_size-1]['p'])+2;
for ($i=0;$i<$outlines_size;$i++){
//Offset
$level=$this->outlines[$i]['l'];
if($level>0)
$this->Cell($level*8);
//Caption
$str=utf8_decode($this->outlines[$i]['t']);
$strsize=$this->GetStringWidth($str);
$avail_size=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-4;
while ($strsize>=$avail_size){
$str=substr($str,0,-1);
$strsize=$this->GetStringWidth($str);
}
$this->Cell($strsize+2,$this->FontSize+2,$str);
//Filling dots
$w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-($strsize+2);
$nb=$w/$this->GetStringWidth('.');
$dots=str_repeat('.',$nb);
$this->Cell($w,$this->FontSize+2,$dots,0,0,'R');
//Page number
$this->Cell($PageCellSize,$this->FontSize+2,'p. '.$this->outlines[$i]['p'],0,1,'R');
}
}
}
?>
bookmark.php
<?php
// Require the file present in the PDF_Index library
require('index.php');
// Create the new object of the PDF_Index class
$pdf=new PDF_Index();
// Set the font of the pdf
$pdf->SetFont('Arial','',15);
// Create the new page of the pdf
$pdf->AddPage();
// Create the bookmark with the name of Section 1 and it will be false
$pdf->Bookmark('Section 1', false);
// Print the cell of the pdf
$pdf->Cell(0,6,'Section 1',0,1);
// Create the new bookmark with the name of the Subsection 1 and it will be false and the level of the bookmark will be 1
$pdf->Bookmark('Subsection 1', false, 1, -1);
// Print the cell of the pdf
$pdf->Cell(0,6,'Subsection 1');
// Go to the next line
$pdf->Ln(50);
// Create the new bookmark with the name of the Subsection 2 and it will be false and the level of the bookmark will be 1
$pdf->Bookmark('Subsection 2', false, 1, -1);
// Print the cell of the pdf
$pdf->Cell(0,6,'Subsection 2');
// Create the new page of the pdf
$pdf->AddPage();
// Create the bookmark with the name of Section 2 and it will be false
$pdf->Bookmark('Section 2', false);
// Print the cell of the pdf
$pdf->Cell(0,6,'Section 2',0,1);
// Create the new bookmark with the name of the Subsection 1 and it will be false and the level of the bookmark will be 1
$pdf->Bookmark('Subsection 1', false, 1, -1);
// Print the cell of the pdf
$pdf->Cell(0,6,'Subsection 1');
// Create the new page of the pdf
$pdf->AddPage();
// Create the bookmark with the name of Index and it will be false
$pdf->Bookmark('Index', false);
// Create Index of the pdf
$pdf->CreateIndex();
// Output the pdf
$pdf->Output();
?>