Zum Inhalt

Table of contents: Inhaltsverzeichnis

Während des Schreibens melden Sie mit TOC_Entry($txt, $level) die Überschriften an. Am Ende erzeugt insertTOC($location) das Verzeichnis und schiebt es an die gewünschte Seite, meist an den Anfang. Die Seitenzahlen stimmen, weil das Verzeichnis erst entsteht, wenn alle Seiten bekannt sind.

Nummer73
OriginaltitelTable of contents
AutorRichard Bondi
LizenzFPDF (frei, auch für kommerzielle Nutzung)
Quellefpdf.org, Skript 73 (englisch, mit Archiv zum Herunterladen)

Quelltext

Speichern Sie die Klasse in einer eigenen Datei neben fpdf.php. Kommentare und Bezeichner stehen wie im Original auf Englisch.

PHP
<?php
require('fpdf.php');

class PDF_TOC extends FPDF {
    protected $_toc=array();
    protected $_numbering=false;
    protected $_numberingFooter=false;
    protected $_numPageNum=1;

    function AddPage($orientation='', $size='', $rotation=0) {
        parent::AddPage($orientation,$size,$rotation);
        if($this->_numbering)
            $this->_numPageNum++;
    }

    function startPageNums() {
        $this->_numbering=true;
        $this->_numberingFooter=true;
    }

    function stopPageNums() {
        $this->_numbering=false;
    }

    function numPageNo() {
        return $this->_numPageNum;
    }

    function TOC_Entry($txt, $level=0) {
        $this->_toc[]=array('t'=>$txt,'l'=>$level,'p'=>$this->numPageNo());
    }

    function insertTOC( $location=1,
                        $labelSize=20,
                        $entrySize=10,
                        $tocfont='Times',
                        $label='Table of Contents'
                        ) {
        //make toc at end
        $this->stopPageNums();
        $this->AddPage();
        $tocstart=$this->page;

        $this->SetFont($tocfont,'B',$labelSize);
        $this->Cell(0,5,$label,0,1,'C');
        $this->Ln(10);

        foreach($this->_toc as $t) {

            //Offset
            $level=$t['l'];
            if($level>0)
                $this->Cell($level*8);
            $weight='';
            if($level==0)
                $weight='B';
            $str=$t['t'];
            $this->SetFont($tocfont,$weight,$entrySize);
            $strsize=$this->GetStringWidth($str);
            $this->Cell($strsize+2,$this->FontSize+2,$str);

            //Filling dots
            $this->SetFont($tocfont,'',$entrySize);
            $PageCellSize=$this->GetStringWidth($t['p'])+2;
            $w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-($strsize+2);
            $nb=$w/$this->GetStringWidth('.');
            $dots=str_repeat('.',(int)$nb);
            $this->Cell($w,$this->FontSize+2,$dots,0,0,'R');

            //Page number
            $this->Cell($PageCellSize,$this->FontSize+2,$t['p'],0,1,'R');
        }

        //Grab it and move to selected location
        $n=$this->page;
        $n_toc = $n - $tocstart + 1;
        $last = array();

        //store toc pages
        for($i = $tocstart;$i <= $n;$i++)
            $last[]=$this->pages[$i];

        //move pages
        for($i=$tocstart-1;$i>=$location-1;$i--)
            $this->pages[$i+$n_toc]=$this->pages[$i];

        //Put toc pages at insert point
        for($i = 0;$i < $n_toc;$i++)
            $this->pages[$location + $i]=$last[$i];
    }

    function Footer() {
        if(!$this->_numberingFooter)
            return;
        //Go to 1.5 cm from bottom
        $this->SetY(-15);
        //Select Arial italic 8
        $this->SetFont('Arial','I',8);
        $this->Cell(0,7,$this->numPageNo(),0,0,'C'); 
        if(!$this->_numbering)
            $this->_numberingFooter=false;
    }
}
?>

Beispiel

So wird die Erweiterung aufgerufen:

PHP
<?php
require('toc.php');

$pdf= new PDF_TOC(); 
$pdf->SetFont('Times','',12);
$pdf->AddPage();
$pdf->Cell(0,5,'Cover',0,1,'C');
$pdf->AddPage();
$pdf->startPageNums();
$pdf->Cell(0,5,'TOC1',0,1,'L');
$pdf->TOC_Entry('TOC1', 0);
$pdf->Cell(0,5,'TOC1.1',0,1,'L');
$pdf->TOC_Entry('TOC1.1', 1);
$pdf->AddPage();
$pdf->Cell(0,5,'TOC2',0,1,'L');
$pdf->TOC_Entry('TOC2', 0);
$pdf->AddPage();
for($i=3;$i<=80;$i++){
    $pdf->Cell(0,5,'TOC'.$i,0,1,'L');
    $pdf->TOC_Entry('TOC'.$i, 0);
}
$pdf->stopPageNums();
$pdf->AddPage();
$pdf->Cell(0,5,'Unnumbered page',0,1,'L');
//Generate and insert TOC at page 2
$pdf->insertTOC(2);
$pdf->Output();
?>
Ergebnis des Beispiels als PDF, erste Seite
Ergebnis: PDF ansehen

Einbau

Add-ons werden nicht installiert, sondern eingebunden: Datei neben fpdf.php legen, mit require laden und die eigene Klasse von der Klasse des Add-ons ableiten. Brauchen Sie mehrere Add-ons, leiten Sie sie in einer Kette voneinander ab. Mehr dazu unter Installation und in der FAQ.