Ver Mensaje Individual
  #4 (permalink)  
Antiguo 04/05/2016, 14:09
Avatar de kip13
kip13
 
Fecha de Ingreso: agosto-2011
Mensajes: 167
Antigüedad: 12 años, 8 meses
Puntos: 13
Respuesta: reporte pdf delimitar tamaño campo

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
  1. <?php
  2. require('fpdf.php');
  3.  
  4. class FPDF_CellFit extends FPDF {
  5.  
  6.     //Cell with horizontal scaling if text is too wide
  7.     function CellFit($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $scale=false, $force=true)
  8.     {
  9.         //Get string width
  10.         $str_width=$this->GetStringWidth($txt);
  11.  
  12.         //Calculate ratio to fit cell
  13.         if($w==0)
  14.             $w = $this->w-$this->rMargin-$this->x;
  15.         $ratio = ($w-$this->cMargin*2)/$str_width;
  16.  
  17.         $fit = ($ratio < 1 || ($ratio > 1 && $force));
  18.         if ($fit)
  19.         {
  20.             if ($scale)
  21.             {
  22.                 //Calculate horizontal scaling
  23.                 $horiz_scale=$ratio*100.0;
  24.                 //Set horizontal scaling
  25.                 $this->_out(sprintf('BT %.2F Tz ET',$horiz_scale));
  26.             }
  27.             else
  28.             {
  29.                 //Calculate character spacing in points
  30.                 $char_space=($w-$this->cMargin*2-$str_width)/max($this->MBGetStringLength($txt)-1,1)*$this->k;
  31.                 //Set character spacing
  32.                 $this->_out(sprintf('BT %.2F Tc ET',$char_space));
  33.             }
  34.             //Override user alignment (since text will fill up cell)
  35.             $align='';
  36.         }
  37.  
  38.         //Pass on to Cell method
  39.         $this->Cell($w,$h,$txt,$border,$ln,$align,$fill,$link);
  40.  
  41.         //Reset character spacing/horizontal scaling
  42.         if ($fit)
  43.             $this->_out('BT '.($scale ? '100 Tz' : '0 Tc').' ET');
  44.     }
  45.  
  46.     //Cell with horizontal scaling only if necessary
  47.     function CellFitScale($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
  48.     {
  49.         $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,false);
  50.     }
  51.  
  52.     //Cell with horizontal scaling always
  53.     function CellFitScaleForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
  54.     {
  55.         $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,true,true);
  56.     }
  57.  
  58.     //Cell with character spacing only if necessary
  59.     function CellFitSpace($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
  60.     {
  61.         $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,false);
  62.     }
  63.  
  64.     //Cell with character spacing always
  65.     function CellFitSpaceForce($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
  66.     {
  67.         //Same as calling CellFit directly
  68.         $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,false,true);
  69.     }
  70.  
  71.     //Patch to also work with CJK double-byte text
  72.     function MBGetStringLength($s)
  73.     {
  74.         if($this->CurrentFont['type']=='Type0')
  75.         {
  76.             $len = 0;
  77.             $nbbytes = strlen($s);
  78.             for ($i = 0; $i < $nbbytes; $i++)
  79.             {
  80.                 if (ord($s[$i])<128)
  81.                     $len++;
  82.                 else
  83.                 {
  84.                     $len++;
  85.                     $i++;
  86.                 }
  87.             }
  88.             return $len;
  89.         }
  90.         else
  91.             return strlen($s);
  92.     }
  93.  
  94. }
  95. ?>

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
  1. <?php
  2. require('cellfit.php');
  3.  
  4. $txt_short = 'This text is short enough.';
  5. $txt_long = 'This text is way too long.';
  6.  
  7. $pdf->CellFitSpace(0,10,$txt_short,1,1);
  8. $pdf->CellFitSpace(0,10,$txt_long,1,1,'',1);
  9.  
  10. $pdf->Output();
  11. ?>

Espero te sirva.

Saludos

Fuente: Script Fit text to cell