Entiendo, se pueden utilizar scripts que estan en la pagina de fpdf, la que postee alla arriba, para resolverlo.
Aqui te dejo un codigo para que lo implementes:
Código PHP:
Ver original<?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;
for ($i = 0; $i < $nbbytes; $i++)
{
$len++;
else
{
$len++;
$i++;
}
}
return $len;
}
else
}
}
?>
En total hay cuatro metodos en el codigo que coloque arriba:
CellFitScale()
CellFitScaleForce()
CellFitSpace()
CellFitSpaceForce()
Creo que el mas adecuado seria el CellFitSpace(), para resolver tu problema.
Ejemplo de uso:
Código PHP:
Ver original<?php
require('cellfit.php');
$txt_short = 'This text is short enough.';
$txt_long = 'This text is way too long.';
$pdf->CellFitSpace(0,10,$txt_short,1,1);
$pdf->CellFitSpace(0,10,$txt_long,1,1,'',1);
$pdf->Output();
?>
Espero te sirva.
Saludos
Fuente:
Script Fit text to cell