/    Sign up×
Community /Pin to ProfileBookmark

How to make text wrap within cell when outputing pdf?

Dear all,

I am totally new in programming, PHP & Mysql, thanks for my luck that I can search a scripts in FPDF.org for PDF output in table format. However as the no. of columns over 30 originally (some are taken out ), it is hard to show within an A4’s width. Could anyone can help to modify it so that text could be showed in wrap and output to become pdf and text not overlapped.

Here comes the mysql_table.php:

[code=php]
<?php
require(‘fpdf.php’);

class PDF_MySQL_Table extends FPDF
{
var $ProcessingTable=false;
var $aCols=array();
var $TableX;
var $HeaderColor;
var $RowColors;
var $ColorIndex;
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 Header()
{
//Print the table header if necessary
if($this->ProcessingTable)
$this->TableHeader();
}

function TableHeader()
{
$this->SetFont(‘Arial’,’B’,6);
$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)
{
//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 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;
}

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 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’]=’C’;
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’,”,5);
$this->ColorIndex=0;
$this->ProcessingTable=true;
while($row=mysql_fetch_array($res))
$this->Row($row);
$this->ProcessingTable=false;
$this->cMargin=$cMargin;
$this->aCols=array();
}
}
?>
[/code]

And the pdf output script:

[code=php]
//Output volume statistics to pdf
function outputpdfvs(){
require(‘includes/mysql_table.php’);
class PDF extends PDF_MySQL_Table
{
function Header()
{
//Title
$this->SetFont(‘Arial’,”,12);
$this->Cell(0,6,’Volume Statistic Report in 2006′,0,1,’L’);
$this->Ln(2);
//Ensure table header is output
parent::Header();
}
function Footer() {
// Check if Footer for this page already exists (do the same for Header())
if(!$this->footerset[$this->page]) {
$this->SetY(-15);
//Page number
$this->Cell(0,10,’Page ‘.$this->PageNo().”,0,0,’C’);
// set footerset
$this->footerset[$this->page] = 1;
}
}
}

//Connect to database
mysql_connect(“localhost”, “ars”, “reporting”);
mysql_select_db(‘report’);
$pdf=new PDF();
$pdf->Open();
$pdf->AddPage(L);
$pdf->AddCol(‘dsn’,7,’Item’,’C’);
$pdf->AddCol(‘actid’,9,’Act. ID’,’C’);
$pdf->AddCol(‘activities’,176,’Activity’);
$pdf->AddCol(‘jan’,7,’Jan’,’C’);
$pdf->AddCol(‘feb’,7,’Feb’,’C’);
$pdf->AddCol(‘mar’,7,’Mar’,’C’);
$pdf->AddCol(‘apr’,7,’Arp’,’C’);
$pdf->AddCol(‘may’,7,’May’,’C’);
$pdf->AddCol(‘june’,7,’June’,’C’);
$pdf->AddCol(‘july’,7,’July’,’C’);
$pdf->AddCol(‘aug’,7,’Aug’,’C’);
$pdf->AddCol(‘sept’,7,’Sept’,’C’);
$pdf->AddCol(‘oct’,7,’Oct’,’C’);
$pdf->AddCol(‘nov’,7,’Nov’,’C’);
$pdf->AddCol(‘decem’,7,’Dec’,’C’);
$pdf->AddCol(‘total06′,9,’Total’,’C’);
$pdf->AddCol(‘mean06′,9,’Mean’,’C’);
$prop=array(‘HeaderColor’=>array(240,220,100),
‘color1’=>array(255,240,220),
‘color2’=>array(255,255,255),
‘padding’=>1);
$pdf->SetWidths(array(30,50,30,40));
srand(microtime()*1000000);
if(ereg(‘z’,$_SESSION[“accesslevel”])){
$pdf->Table(“select dsn, costcentre, actid, activities, jan, feb, mar, apr, may, june, july, aug, sept, oct, nov, decem, total06, mean06 from monthlydata”,$prop);
}else{
$pdf->Table(“select dsn, costcentre, actid, activities, jan, feb, mar, apr, may, june, july, aug, sept, oct, nov, decem, total06, mean06 from monthlydata where costcentre='” . $_SESSION[“costcentre”] . “‘”,$prop);
}
$pdf->Output();
}
[/code]

Thank you very much!

Leo

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @hkcentralize spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...