Fit text to cell

Informations

Author: Patrick Benny
License: FPDF

Description

This method is an extension of Cell() allowing to output text with either character spacing or horizontal scaling.

CellFit(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link [, boolean scale [, boolean force]]]]]]]]])

The first 8 parameters are the same as Cell(). The additional parameters are:

scale

false: character spacing
true: horizontal scaling

force

false: only space/scale if necessary (not when text is short enough to fit)
true: always space/scale

The following four methods are also provided for convenience, allowing all combinations of scale/force, and using only the 8 parameters of Cell():

CellFitScale()
CellFitScaleForce()
CellFitSpace()
CellFitSpaceForce()

Source

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

class FPDF_CellFit extends FPDF {

    //Cell with horizontal scaling if text is too wide
    function CellFit($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $scale=false, $force=true)
    {
        //Get string width
        $str_width=$this->GetStringWidth($txt);

        //Calculate ratio to fit cell
        if($w==0)
            $w = $this->w-$this->rMargin-$this->x;
        $ratio = ($w-$this->cMargin*2)/$str_width;

        $fit = ($ratio < 1 || ($ratio > 1 && $force));
        if ($fit)
        {
            if ($scale)
            {
                //Calculate horizontal scaling
                $horiz_scale=$ratio*100.0;
                //Set horizontal scaling
                $this->_out(sprintf('BT %.2F Tz ET',$horiz_scale));
            }
            else
            {
                //Calculate character spacing in points
                $char_space=($w-$this->cMargin*2-$str_width)/max($this->MBGetStringLength($txt)-1,1)*$this->k;
                //Set character spacing
                $this->_out(sprintf('BT %.2F Tc ET',$char_space));
            }
            //Override user alignment (since text will fill up cell)
            $align='';
        }

        //Pass on to Cell method
        $this->Cell($w,$h,$txt,$border,$ln,$align,$fill,$link);

        //Reset character spacing/horizontal scaling
        if ($fit)
            $this->_out('BT '.($scale ? '100 Tz' : '0 Tc').' ET');
    }

    //Cell with horizontal scaling only if necessary
    function CellFitScale($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
    {
        $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,false);
    }

    //Cell with horizontal scaling always
    function CellFitScaleForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
    {
        $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,true);
    }

    //Cell with character spacing only if necessary
    function CellFitSpace($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
    {
        $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,false);
    }

    //Cell with character spacing always
    function CellFitSpaceForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
    {
        //Same as calling CellFit directly
        $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,true);
    }

    //Patch to also work with CJK double-byte text
    function MBGetStringLength($s)
    {
        if($this->CurrentFont['type']=='Type0')
        {
            $len = 0;
            $nbbytes = strlen($s);
            for ($i = 0; $i < $nbbytes; $i++)
            {
                if (ord($s[$i])<128)
                    $len++;
                else
                {
                    $len++;
                    $i++;
                }
            }
            return $len;
        }
        else
            return strlen($s);
    }

}
?>

Example

<?php
require('cellfit.php');

$txt_short = 'This text is short enough.';
$txt_long = 'This text is way too long.';
for ($i = 1; $i <= 2; $i++)
    $txt_long.=' '.$txt_long;

$pdf = new FPDF_CellFit();
$pdf->AddPage();
$pdf->SetFillColor(0xff,0xff,0x99);

$pdf->SetFont('Arial','B',16);
$pdf->Write(10,'Cell');
$pdf->SetFont('');
$pdf->Ln();
$pdf->Cell(0,10,$txt_short,1,1);
$pdf->Cell(0,10,$txt_long,1,1);
$pdf->Ln();

$pdf->Line($pdf->x,$pdf->y,$pdf->w-$pdf->x-$pdf->rMargin,$pdf->y);
$pdf->Ln();

$pdf->SetFont('','B');
$pdf->Write(10,'CellFitScale');
$pdf->SetFont('');
$pdf->Write(10,' (horizontal scaling only if necessary)');
$pdf->Ln();
$pdf->CellFitScale(0,10,$txt_short,1,1);
$pdf->CellFitScale(0,10,$txt_long,1,1,'',1);
$pdf->Ln();

$pdf->SetFont('','B');
$pdf->Write(10,'CellFitScaleForce');
$pdf->SetFont('');
$pdf->Write(10,' (horizontal scaling always)');
$pdf->Ln();
$pdf->CellFitScaleForce(0,10,$txt_short,1,1,'',1);
$pdf->CellFitScaleForce(0,10,$txt_long,1,1,'',1);
$pdf->Ln();

$pdf->Line($pdf->x,$pdf->y,$pdf->w-$pdf->x-$pdf->rMargin,$pdf->y);
$pdf->Ln();

$pdf->SetFont('','B');
$pdf->Write(10,'CellFitSpace');
$pdf->SetFont('');
$pdf->Write(10,' (character spacing only if necessary)');
$pdf->Ln();
$pdf->CellFitSpace(0,10,$txt_short,1,1);
$pdf->CellFitSpace(0,10,$txt_long,1,1,'',1);
$pdf->Ln();

$pdf->SetFont('','B');
$pdf->Write(10,'CellFitSpaceForce');
$pdf->SetFont('');
$pdf->Write(10,' (character spacing always)');
$pdf->Ln();
$pdf->CellFitSpaceForce(0,10,$txt_short,1,1,'',1);
$pdf->CellFitSpaceForce(0,10,$txt_long,1,1,'',1);

$pdf->Output();
?>
View the result here.

Download

ZIP | TGZ