Jump to content

Recommended Posts

im kinda new to php, i'm having a problem with fpdf, im pulling data from a database and displays it in a pdf table. my problem is

when ever the text is too long it does not wrap.

 

here is the link and the code below

 

http://localhost/www/downs-Test/Display_fpdf.php

 

 

 

<?php
require ('fpdf/fpdf.php');
 
class PDF_MySQL_Table extends FPDF
{
var $ProcessingTable=false;
var $aCols=array();
var $TableX;
var $HeaderColor;
var $RowColors;
var $ColorIndex;
 
function Header()
{
    //Print the table header if necessary
    if($this->ProcessingTable)
       $this->TableHeader();
}
 
function TableHeader()
{
    $this->SetFont('Arial','B',9);
    $this->SetX($this->TableX);
    $fill=!empty($this->HeaderColor);
    if($fill)
        $this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],6,$col['c'],1,0,'C',$fill);
    $this->Ln();
}
 
function Row($data)
{
 
    $this->SetX($this->TableX);
    $ci=$this->ColorIndex;
    $fill=!empty($this->RowColors[$ci]);
    if($fill)
        $this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
    foreach($this->aCols as $col)
 
//$temp=$data[$col['f']];
    //$this->WordWrap($temp,50);
 
 
    $this->Cell($col['w'],6,$data[$col['f']],1,0,$col['a'],$fill);
    $this->Ln();
    $this->ColorIndex=1-$ci;
 
}
 
 
function CalcWidths($width,$align)
{
    //Compute the widths of the columns
    $TableWidth=0;
    foreach($this->aCols as $i=>$col)
    {
        $w=$col['w'];
        if($w==-1)
            $w=$width/count($this->aCols);
        elseif(substr($w,-1)=='%')
            $w=$w/100*$width;
        $this->aCols[$i]['w']=$w;
        $TableWidth+=$w;
    }
    //Compute the abscissa of the table
    if($align=='C')
        $this->TableX=max(($this->w-$TableWidth)/2,0);
    elseif($align=='R')
        $this->TableX=max($this->w-$this->rMargin-$TableWidth,0);
    else
        $this->TableX=$this->lMargin;
}
 
function AddCol($field=-1,$width=-1,$caption='',$align='L')
{
    //Add a column to the table
    if($field==-1)
        $field=count($this->aCols);
    $this->aCols[]=array('f'=>$field,'c'=>$caption,'w'=>$width,'a'=>$align);
}
 
function Table($query,$prop=array())
{
    //Issue query
    $res=mysql_query($query) or die('Error: '.mysql_error()."<BR>Query: $query");
    //Add all columns if none was specified
    if(count($this->aCols)==0)
    {
        $nb=mysql_num_fields($res);
        for($i=0;$i<$nb;$i++)
            $this->AddCol();
    }
    //Retrieve column names when not specified
    foreach($this->aCols as $i=>$col)
    {
        if($col['c']=='')
        {
            if(is_string($col['f']))
                $this->aCols[$i]['c']=ucfirst($col['f']);
            else
                $this->aCols[$i]['c']=ucfirst(mysql_field_name($res,$col['f']));
        }
    }
    //Handle properties
    if(!isset($prop['width']))
        $prop['width']=0;
    if($prop['width']==0)
        $prop['width']=$this->w-$this->lMargin-$this->rMargin;
    if(!isset($prop['align']))
        $prop['align']='L';
    if(!isset($prop['padding']))
        $prop['padding']=$this->cMargin;
    $cMargin=$this->cMargin;
    $this->cMargin=$prop['padding'];
    if(!isset($prop['HeaderColor']))
        $prop['HeaderColor']=array();
    $this->HeaderColor=$prop['HeaderColor'];
    if(!isset($prop['color1']))
        $prop['color1']=array();
    if(!isset($prop['color2']))
        $prop['color2']=array();
    $this->RowColors=array($prop['color1'],$prop['color2']);
    //Compute column widths
    $this->CalcWidths($prop['width'],$prop['align']);
    //Print header
    $this->TableHeader();
    //Print rows
    $this->SetFont('Arial','',7);
    $this->ColorIndex=0;
    $this->ProcessingTable=true;
    while($row=mysql_fetch_array($res))
        $this->Row($row);
    $this->ProcessingTable=false;
    $this->cMargin=$cMargin;
    $this->aCols=array();
}
 
function WordWrap(&$text, $maxwidth)
{
    $text = trim($text);
    if ($text==='')
        return 0;
    $space = $this->GetStringWidth(' ');
    $lines = explode("n", $text);
    $text = '';
    $count = 0;
 
    foreach ($lines as $line)
    {
        $words = preg_split('/ +/', $line);
        $width = 0;
 
        foreach ($words as $word)
        {
            $wordwidth = $this->GetStringWidth($word);
            if ($wordwidth > $maxwidth)
            {
                // Word is too long, we cut it
                for($i=0; $i<strlen($word); $i++)
                {
                    $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                    if($width + $wordwidth <= $maxwidth)
                    {
                        $width += $wordwidth;
                        $text .= substr($word, $i, 1);
                    }
                    else
                    {
                        $width = $wordwidth;
                        $text = rtrim($text)."n".substr($word, $i, 1);
                        $count++;
                    }
                }
            }
            elseif($width + $wordwidth <= $maxwidth)
            {
                $width += $wordwidth + $space;
                $text .= $word.' ';
            }
            else
            {
                $width = $wordwidth + $space;
                $text = rtrim($text)."n".$word.' ';
                $count++;
            }
        }
        $text = rtrim($text)."n";
        $count++;
    }
    $text = rtrim($text);
    return $count;
}
}
 
 
 
?>
 
 
 
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
include_once("config.php");
require('Table_cell.php');
//require('Word_warp.php');
 
 
class PDF extends PDF_MySQL_Table
{
function Header()
{
    //Title
    $this->SetFont('Arial','',18);
    $this->Cell(0,6,'Maintenance Downs List',0,1,'C');
    $this->Ln(10);
    //Ensure table header is output
    parent::Header();
}
}
 
$con = mysql_connect($host,$user,$password);  
 
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("employees", $con);
 
$pdf=new PDF ();
$pdf->AddPage("L");
//First table: put all columns automatically
//$pdf->Table("SELECT CName, EDesc, IssDate, Fault, PtsReq, PtsOrd, PtsIn, Commt, EDate, AB, WOrder, vLocal, TOrder, iCost,
 // PurchOrd, PurchDate, PurchReq, ReqDate FROM tblDowns ORDER BY CName");
//$pdf->AddPage();
//Second table: specify 18 columns
$pdf->AddCol('CName',28,'','C');
$pdf->AddCol('EDesc',25,'Equip Desc','C');
$pdf->AddCol('WOrder',15,'Call #','C');
$pdf->AddCol('TOrder',15,'Equip #','C');
$pdf->AddCol('IssDate',18,'Issue Date','C');
$pdf->AddCol('AB',10,'A B','C');
$pdf->AddCol('Fault',40,'Fault / Issue','C');
$pdf->AddCol('vLocal',15,'E/V LOC','C');
$pdf->AddCol('PtsReq',17,'Parts Req','C');
$pdf->AddCol('PtsOrd',17,'Parts Ord','C');
$pdf->AddCol('PtsIn',17,'Parts In','C');
$pdf->AddCol('PurchReq',16,'Pur Req#','C');
$pdf->AddCol('ReqDate',15,'Pur Req','C');
$pdf->AddCol('PurchOrd',15,'Pur Ord#','C');
$pdf->AddCol('PurchDate',15,'Pur Ord','C');
//$pdf->AddCol('iCost',15,'Item Cost','1');
//$pdf->AddCol(7,12,'Comments','1');
//$pdf->AddCol(7,12,'Entry Date','1');
 
$prop=array('HeaderColor'=>array(255,150,100),
            'color1'=>array(210,245,255),
            'color2'=>array(255,255,210),
            'padding'=>1);
 
 
//$pdf->Table("SELECT CName, EDesc, IssDate, Fault, PtsReq, PtsOrd, PtsIn, Commt, EDate, AB, WOrder, vLocal, TOrder, iCost,
  //PurchOrd, PurchDate, PurchReq, ReqDate FROM tblDowns ORDER BY CName ASC",$prop);
 
 
$pdf->Table("SELECT CName, EDesc, IssDate, Fault, PtsReq, PtsOrd, PtsIn, Commt, EDate, AB, WOrder, vLocal, TOrder, iCost,
  PurchOrd, PurchDate, PurchReq, ReqDate FROM tblDowns ORDER BY CName ASC",$prop);
$pdf->Output();
?>
</body>
</html>

 

Edited by cyberRobot
Surrounded code with [code][/code] tags
Link to comment
https://forums.phpfreaks.com/topic/300294-text-does-not-wrap-in-table-cell/
Share on other sites

hi ginerjm, I know man a lot of code, I tried MultiCell function but it doesn't work dynamically see im pulling data from a database and display it 

so as the text is displayed in the table, the cell suppose to adjust to suit the length text as the text wrap.

 

here is the link below

 

http://devonism.com/downs-Test/Display_fpdf.php

You use the multicell function for just this purpose! Of course you have to save the current y position and do a sety to get back to the "top" line again after each one.

 

PS - your output might end up more readable if you didn't center all the columns. Especially text ones.

Edited by ginerjm

hey ginerjm, thanks for your reply man, lots of respect to you man, I removed the center columns, I used the Multicell function, see my code below,

the thing is my table data <td/>  keep over lapping my table head data <th/> 

 

<?php
//include_once("setting.php");
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
 
$pdf->SetFont('Arial','',16);
 
$starter = $pdf->GetX();
$get_x   = $pdf->GetX();
$get_y   = $pdf->GetY();
 
$lebar = 40;
$tinggi =7;
 
$pdf->Cell(40,7,'Hi1',1);
$pdf->Cell(40,7,'Hi2',1);
$pdf->Cell(40,7,'Hi3',1);
$pdf->Ln();
 
$pdf->SetFont('Helvetica', '', 10);
 
//$pdf->Cell(20,5,'Hi4',0,1,'L',true);
 
$pdf->MultiCell($lebar,$tinggi,'this is good hhhhhhhhh for hhhhhhhhhh you',1);
$get_x+=$lebar;
$pdf->SetXY($get_x, $get_y);
 
$pdf->MultiCell($lebar,$tinggi,'this is good hhhhhhhhh for hhhhhhhhhh you',1);
$get_x+=$lebar;
$pdf->SetXY($get_x, $get_y);
 
$pdf->MultiCell($lebar,$tinggi,'this is good hhhhhhhhh for hhhhhhhhhh you',1);
$get_x+=$lebar;
$pdf->SetXY($get_x, $get_y);
$pdf->Output();
?>
 
 
 
 
OUTPUT
 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.