fpdf
PHP powered
home was ist fpdf? downloads & add-ons dokumentation faq forum impressum links kontakt
 

Downloads > Add-On

 
         
 

Fit text to cell

Informations

Author: Patrick Benny
License: Freeware

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 [, int fill [, mixed link [, int scale [, int force]]]]]]]]])

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

scale

0 = character spacing
1 = horizontal scaling

force

0 = only space/scale if necessary (not when text is short enough to fit)
1 = 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=0, $link='', $scale=0, $force=1)
    {
        //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 == 1));
        if ($fit)
        {
            switch ($scale)
            {

                //Character spacing
                case 0:
                    //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));
                    break;

                //Horizontal scaling
                case 1:
                    //Calculate horizontal scaling
                    $horiz_scale=$ratio*100.0;
                    //Set horizontal scaling
                    $this->_out(sprintf('BT %.2f Tz ET', $horiz_scale));
                    break;

            }
            //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==0 ? '0 Tc' : '100 Tz').' ET');
    }

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

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

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

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

    //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
define
('FPDF_FONTPATH', 'font/');
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
 






 
         
         
 
 
fpdf.de © Copyright 2004-2006 carrib internet solutions - Beachten Sie bitte die Nutzungsbedingungen
Eine Verwendung der auf fpdf.de befindlichen Inhalte (Texte, Grafiken) ist nur mit Zustimmung des Betreibers zulässig.