Word wrap in pdf table using fpdf in Php - OneNightStudy.Com

Wednesday, 25 May 2016

Word wrap in pdf table using fpdf in Php

Word wrap in the pdf table create lot of problems. I got the best solution for that for fpdf library.

These is the code for word wrap in the pdf:

class PDF_MC_Table extends FPDF
{
var $widths;
var $aligns;

function SetWidths($w)
{
    //Set the array of column widths
    $this->widths=$w;
}

function SetAligns($a)
{
    //Set the array of column alignments
    $this->aligns=$a;
}

function Row($data)
{
    //Calculate the height of the row
   
    $data = array_values($data);
     //print_r($data);die;
    $nb=0;
    for($i=0;$i<count($data);$i++){
        
        $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
    }
    $h=5*$nb;
    //Issue a page break first if needed
    $this->CheckPageBreak($h);
    //Draw the cells of the row
    for($i=0;$i<count($data);$i++)
    {
        $w=$this->widths[$i];
        if(is_numeric($data[$i]) && $i>0){
            $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'R';
            $data[$i] = number_format((float)$data[$i], 2, '.', '');
        }else{
            $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
        }
        //Save the current position
        $x=$this->GetX();
        $y=$this->GetY();
        //Draw the border
        $this->Rect($x,$y,$w,$h);
        //Print the text
        $this->MultiCell($w,5,$data[$i],0,$a);
        //Put the position to the right of the cell
        $this->SetXY($x+$w,$y);
    }
    //Go to the next line
    $this->Ln($h);
}

function CheckPageBreak($h)
{
    //If the height h would cause an overflow, add a new page immediately
    if($this->GetY()+$h>$this->PageBreakTrigger)
        $this->AddPage($this->CurOrientation);
}

function NbLines($w,$txt)
{
    //Computes the number of lines a MultiCell of width w will take
    $cw=&$this->CurrentFont['cw'];
    if($w==0)
        $w=$this->w-$this->rMargin-$this->x;
    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
    $s=str_replace("\r",'',$txt);
    $nb=strlen($s);
    if($nb>0 and $s[$nb-1]=="\n")
        $nb--;
    $sep=-1;
    $i=0;
    $j=0;
    $l=0;
    $nl=1;
    while($i<$nb)
    {
        $c=$s[$i];
        if($c=="\n")
        {
            $i++;
            $sep=-1;
            $j=$i;
            $l=0;
            $nl++;
            continue;
        }
        if($c==' ')
            $sep=$i;
        $l+=$cw[$c];
        if($l>$wmax)
        {
            if($sep==-1)
            {
                if($i==$j)
                    $i++;
            }
            else
                $i=$sep+1;
            $sep=-1;
            $j=$i;
            $l=0;
            $nl++;
        }
        else
            $i++;
    }
    return $nl;
}
}

$pdf=new PDF_MC_Table();

$pdf->AddPage();

$pdf->SetFont( 'Arial', 'B', 18 ); //set font to Arial, Bold, and 16 Size 
$pdf->Cell( 0, 10,'Heading Text', 2, 2, 'L' ); //heading for the pdf if required


//Table with 20 rows and 4 columns

$count = count($data[0]); //table data we got from mysql query

for($i=1;$i<=$count;$i++){  //some width setting do as per as requirement
if($i ==1)
$width[] = 20;
else
$width[] = 40;
}
foreach($header as $key=>$col){  //table header array will be there in the $header array
    $pdf->SetFont('Arial', 'B', 12);
    $pdf->Cell($width[$key],7,$col,1);
}
$pdf->Ln();
$pdf->SetFont('Arial','',12);
$pdf->SetWidths($width);
//srand(microtime()*1000000);
foreach($data as $val){  //table data we got from mysql query is in $data variable

    $pdf->Row($val);
}
$pdf->Output("export_$table_name.pdf", "D");

In case of any query please comment in the comment box.

1 comment:

  1. Thanks for sharing,. I enjoy reading of this article a lot. I love to do PSD to Cratejoy conversion in programming languages.

    ReplyDelete