Index

This class prints an index from the created bookmarks

Informations

Author: Min's
License: FPDF

Description

This class prints an index from the created bookmarks. It therefore requires the Bookmarks extension.

Source

<?php require('bookmark.php'); class PDF_Index extends PDF_Bookmark { function CreateIndex(){ //Index title $this->SetFontSize(20); $this->Cell(0, 5, 'Index', 0, 1, 'C'); $this->SetFontSize(15); $this->Ln(10); $size=sizeof($this->outlines); $PageCellSize=$this->GetStringWidth('p. '.$this->outlines[$size-1]['p'])+2; for ($i=0;$i<$size;$i++){ //Offset $level=$this->outlines[$i]['l']; if($level>0) $this->Cell($level*8); //Caption $str=$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'); } } } ?>

Example

<?php require('createindex.php'); $pdf=new PDF_Index(); $pdf->SetFont('Arial', '', 15); //Page 1 $pdf->AddPage(); $pdf->Bookmark('Section 1'); $pdf->Cell(0, 6, 'Section 1', 0, 1); $pdf->Bookmark('Subsection 1', 1, -1); $pdf->Cell(0, 6, 'Subsection 1'); $pdf->Ln(50); $pdf->Bookmark('Subsection 2', 1, -1); $pdf->Cell(0, 6, 'Subsection 2'); //Page 2 $pdf->AddPage(); $pdf->Bookmark('Section 2'); $pdf->Cell(0, 6, 'Section 2', 0, 1); $pdf->Bookmark('Subsection 1', 1, -1); $pdf->Cell(0, 6, 'Subsection 1'); //Index $pdf->AddPage(); $pdf->Bookmark('Index'); $pdf->CreateIndex(); $pdf->Output(); ?>
View the result here.

Download

ZIP | TGZ

Quelle

http://www.fpdf.org/en/script/script13.php, zuletzt abgerufen 02.10.2016 15:03