Zum Inhalt

Fit text to cell: Text in die Zelle einpassen

CellFit() hat die Parameter von Cell() und zwei weitere: $scale wählt zwischen waagrechtem Stauchen und Zeichenabstand, $force bestimmt, ob auch kurzer Text auf die Breite gedehnt wird. Dazu gibt es vier Kurzformen wie CellFitScale(). Sehr nützlich für Tabellen mit Namen unbekannter Länge.

Nummer62
OriginaltitelFit text to cell
AutorPatrick Benny
LizenzFPDF (frei, auch für kommerzielle Nutzung)
Quellefpdf.org, Skript 62 (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 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(strlen($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);
    }
}
?>

Beispiel

So wird die Erweiterung aufgerufen:

PHP
<?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(20);

$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(20);

$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();
?>
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.