Zum Inhalt

MultiCell with bullets: Aufzählungsliste

MultiCellBltArray($w, $h, $blt_array, $border, $align, $fill) gibt eine ganze Liste auf einmal aus. Das Feld enthält das Aufzählungszeichen, Einzug, Abstand, Schriftgrösse und die Texte der Listenpunkte.

Nummer56
OriginaltitelMultiCell with bullets
AutorSean Benoit
LizenzFPDF (frei, auch für kommerzielle Nutzung)
Quellefpdf.org, Skript 56 (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 extends FPDF
{
    /************************************************************
    *                                                           *
    *    MultiCell with bullet (array)                          *
    *                                                           *
    *    Requires an array with the following  keys:            *
    *                                                           *
    *        Bullet -> String or Number                         *
    *        Margin -> Number, space between bullet and text    *
    *        Indent -> Number, width from current x position    *
    *        Spacer -> Number, calls Cell(x), spacer=x          *
    *        Text -> Array, items to be bulleted                *
    *                                                           *
    ************************************************************/

    function MultiCellBltArray($w, $h, $blt_array, $border=0, $align='J', $fill=false)
    {
        if (!is_array($blt_array))
        {
            die('MultiCellBltArray requires an array with the following keys: bullet,margin,text,indent,spacer');
            exit;
        }
                
        //Save x
        $bak_x = $this->x;
        
        for ($i=0; $i<sizeof($blt_array['text']); $i++)
        {
            //Get bullet width including margin
            $blt_width = $this->GetStringWidth($blt_array['bullet'] . $blt_array['margin'])+$this->cMargin*2;
            
            // SetX
            $this->SetX($bak_x);
            
            //Output indent
            if ($blt_array['indent'] > 0)
                $this->Cell($blt_array['indent']);
            
            //Output bullet
            $this->Cell($blt_width,$h,$blt_array['bullet'] . $blt_array['margin'],0,'',$fill);
            
            //Output text
            $this->MultiCell($w-$blt_width,$h,$blt_array['text'][$i],$border,$align,$fill);
            
            //Insert a spacer between items if not the last item
            if ($i != sizeof($blt_array['text'])-1)
                $this->Ln($blt_array['spacer']);
            
            //Increment bullet if it's a number
            if (is_numeric($blt_array['bullet']))
                $blt_array['bullet']++;
        }
    
        //Restore x
        $this->x = $bak_x;
    }
}
?>

Beispiel

So wird die Erweiterung aufgerufen:

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

$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Times','',12);

$column_width = $pdf->GetPageWidth()-30;
$sample_text = 'This is bulleted text. The text is indented and the bullet appears at the first line only. This list is built with a single call to MultiCellBltArray().';

//Test1
$test1 = array();
$test1['bullet'] = chr(149);
$test1['margin'] = ' ';
$test1['indent'] = 0;
$test1['spacer'] = 0;
$test1['text'] = array();
for ($i=0; $i<5; $i++)
{
    $test1['text'][$i] = $sample_text;
}
$pdf->SetX(10);
$pdf->MultiCellBltArray($column_width-$pdf->GetX(),6,$test1);
$pdf->Ln(10);

//Test 2
$test2 = array();
$test2['bullet'] = '>';
$test2['margin'] = ' ';
$test2['indent'] = 5;
$test2['spacer'] = 5;
$test2['text'] = array();
for ($i=0; $i<5; $i++)
{
    $test2['text'][$i] = $sample_text;
}
$pdf->SetX(20);
$pdf->MultiCellBltArray($column_width-$pdf->GetX(),6,$test2);
$pdf->Ln(10);

//Test 3
$test3 = array();
$test3['bullet'] = 1;
$test3['margin'] = ')     ';
$test3['indent'] = 10;
$test3['spacer'] = 10;
$test3['text'] = array();
for ($i=0; $i<5; $i++)
{
    $test3['text'][$i] = $sample_text;
}
$pdf->SetX(30);
$pdf->MultiCellBltArray($column_width-$pdf->GetX(),6,$test3);

$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.