Khalil DaNish
Khalil DaNish

Reputation: 557

Issue with wrapping long text using PHP FPDF

enter image description here

I have a report like above image the title columns text is too long and I want to do the text wrap on the this cell but it's not working and showing the data in vertical columns. I read the FPDF doc.. and tried a lot but still not worked this is the result I get with my code:

enter image description here

what can I do the to wrap the first column text?

function toPDF($data, $cols = NULL, $cols_event = NULL){
 global $action, $meta;  

if ($cols) {
    // translate column weighting to page width in mm
    $colno = 0;
    $max_col = count($cols);
    foreach ($cols as $col) {
        $colno += $col[0];
    }
    foreach ($cols as $key => $col) {
        $cols[$key][0] = (int)($col[0] / $colno * 196);
    }
} elseif($cols_event){
    $colno = 0;
    $max_col = count($cols_event);
    foreach ($cols_event as $col) {
        $colno += $col[0];
    }
    foreach ($cols_event as $key => $col) {
        $cols[$key][0] = (int)($col[0]);
    }
} else {
    $max_col = 0;
    foreach ($data as $line) {
        if (is_array($line)) $max_col = max(count($line), $max_col);
    }
    $width = $max_col == 0 ? 196 : 196 / $max_col;
    $cols = array();
    for ($cc = 0; $cc < $max_col; $cc++) {
        $cols[] = array($width, "");
    }
}


class PDF extends FPDF{

    function Header()
    {
    }

    function Footer()
    {
        //Go to 1.5 cm from bottom
        $this->SetY(-15);
        //Select Arial italic 8
        $this->SetFont('Arial', 'I', 8);
        //Print centered page number
        $this->Cell(0, 10, 'Page ' . ($this->page . ' of {nb}'), 0, 0, 'C');
    }

}

// $pdf = new PDF('P', 'mm', 'Letter');
$pdf = new PDF('P','mm',array(550,300));
$pdf->SetDisplayMode("real", "continuous");
$pdf->AliasNbPages();
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(200, 200, 200);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetLineWidth(.1);
$pdf->SetLeftMargin(10);
$pdf->AddPage();

$pdf->Cell(190,10, $meta['title'],"B",1,"C",0);

$first = true;
foreach ($data as $line) {
    if (is_array($line)) {
        for ($field = 0; $field < $max_col; $field++) {
            if (isset($line[$field])) {
                if(is_array($line[$field])) {
                    $pdf->Cell($cols[$field][0], 6, strip_tags($line[$field][0]), 1, 0, $cols[$field][1], 1);
                } else {
                    if($first) {
                        $pdf->Cell($cols[$field][0], 6, strip_tags($line[$field]), 1, 0, 'L', 1);
                    } else {
                        $pdf->MultiCell($cols[$field][0], 6, strip_tags($line[$field]));
                    }
                }
            } else {
                $pdf->Cell($cols[$field][0], 6, "", 1, 0, 'L', 1);
            }
        }
    }
    $pdf->Ln(6);
    $first = false;
}

$pdf->Output($action . ".pdf", "I");
}

Upvotes: 1

Views: 2002

Answers (2)

Khalil DaNish
Khalil DaNish

Reputation: 557

finally, I solved the issue by customizing the codes from FPDF example. here is the link http://www.fpdf.org/?go=script&id=3.

@Marco Messina Thanks you helped me alot

function toPDF($data, $cols = NULL){
global $action, $meta;

if ($cols) {
    // translate column weighting to page width in mm
    $colno = 0;
    $max_col = count($cols);
    foreach ($cols as $col) {
        $colno += $col[0];
    }
    foreach ($cols as $key => $col) {
        $cols[$key][0] = (int)($col[0] / $colno * 196);
    }
} else {
    $max_col = 0;
    foreach ($data as $line) {
        if (is_array($line)) $max_col = max(count($line), $max_col);
    }
    $width = $max_col == 0 ? 196 : 196 / $max_col;
    $cols = array();
    for ($cc = 0; $cc < $max_col; $cc++) {
        $cols[] = array($width, "");
    }
}

class PDF 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
        $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];
            $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('P','mm',array(550,500));
$pdf->SetDisplayMode("real", "continuous");
$pdf->AliasNbPages();
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(200, 200, 200);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetLineWidth(.1);
$pdf->SetLeftMargin(10);
$pdf->AddPage();

$pdf->SetWidths(array(50,25,25,35,35,35,35,35,35,20,10,20,20,10,10,10,10,35,35,35));
$pdf->SetAligns('L');
srand(microtime()*1000000);
for($i=0; $i < count($data); $i++)
$pdf->Row($data[$i]);
$pdf->Output();
}

Upvotes: 2

Marco Messina
Marco Messina

Reputation: 104

Hey I've been looking for a solution for this problem is I found something !. I thought that we use as a value the difference of a getY () (before multicell) and getY (after Multicell) we can set the right coordinates. There are some minor changes I had to make to make this work.

The only problem that now is only the edges. Instead the cells are set correctly.

  • I set three variables before the "foreach ($ data as $ line)" line:

    $y1 = $pdf->GetY();

    $i = 1;

    $y2 = 1;

  • I modified the code snippet where you had Multicell with this:

                    $pdf->SetLeftMargin(20);                    
                    $x = $pdf->GetX();
    
                    $y = $pdf->GetY();
    
                    $pdf->MultiCell($cols[$field][0], 6, strip_tags($line[$field]), "L T R");
    
                    $y2 = $pdf->GetY();
    
                    $i++;
    
                    if($y2 > $y1 ){
                        $y1 = $y2;  
                    }
    
                    if($i > 5){
                        $pdf->SetXY($x + $cols[$field][0], $y1 - 6);
                        $i = 1;                 
                    }else{
                        $pdf->SetXY($x + $cols[$field][0], $y);
                    }   
    

This is a photo of a test

Upvotes: 1

Related Questions