MultiCell with bullets

Informations

Author: Sean Benoit
License: FPDF

Description

This script allows to output a bulleted list with a single call to MultiCellBltArray(). Parameters are passed through an array.

Source

<?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;
    }
}
?>

Example

<?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();
?>
View the result here.

Download

ZIP | TGZ