- Move the start/end table logic into functions

- If a multi-line table row spills off the bottom of the page, start a new page
				/
  and then print that row.
This commit is contained in:
dave 2008-01-26 20:41:33 +00:00
parent 8980275627
commit 8b38f44501

227
lpdf.php
View File

@ -567,47 +567,24 @@ class lpdf
}
function addTable($table,$align="center")
function addTableStart(&$table, $xpos_of_table, $table_width)
{
//if we get a table passed in that doesnt look like a table (not an array) then just return doing nothing
if(!is_array($table)) return;
if(is_array($table['header']))
{
if(is_array($table['header'])) {
$table_cols=count($table['header']);
$height['tableheader']=0.2;
}
else
{
$height_header=0.2;
} else {
$table_cols=count($table['data']);
$height['tableheader']=0;
$height_header=0;
}
$height['tabledata']=0.18;
$this->yloc-=$height['tableheader'];
$this->yloc-=$height_header;
$top_of_table=$this->yloc;
$table_width=array_sum($table['widths']);
$table_padding=0.03;
$allow_multiline = false;
if(is_array($table['option'])) {
$allow_multiline = ($table['option']['allow_multiline'] == true) ? true : false;
}
switch($align)
{
case "center"; $xpos_of_table=($this->page_width-$table_width)/2; break;
case "left"; $xpos_of_table=$this->page_margin; break;
case "right"; $xpos_of_table=$this->page_width-$this->page_margin-$table_width; break;
}
//draw the top line of the table (above the table header)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos_of_table+$table_width),$this->loc($this->yloc+$height['tableheader']));
pdf_stroke($this->pdf);
//draw the top line of the table (above the table header)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc+$height_header));
pdf_lineto($this->pdf,$this->loc($xpos_of_table+$table_width),$this->loc($this->yloc+$height_header));
pdf_stroke($this->pdf);
//do the header first
if(is_array($table['header']))
@ -625,34 +602,98 @@ class lpdf
$head=$table['header'][$c];
$width=$table['widths'][$c];
pdf_show_boxed($this->pdf,$head,$this->loc($xpos),$this->loc($this->yloc),$this->loc($width),$this->loc($height['tableheader']),"center",null);
pdf_show_boxed($this->pdf,$head,$this->loc($xpos),$this->loc($this->yloc),$this->loc($width),$this->loc($height_header),"center",null);
$xpos+=$width;
}
pdf_setfont($this->pdf,$this->normalfont,10);
}
return $top_of_table;
}
function addTableEnd(&$table, $xpos_of_table, $top_of_table)
{
if(is_array($table['header'])) {
$table_cols=count($table['header']);
$height_header=0.2;
} else {
$table_cols=count($table['data']);
$height_header=0;
}
//now draw all the vertical lines
$xpos=$xpos_of_table;
for($c=0;$c<$table_cols;$c++)
{
$width=$table['widths'][$c];
//draw the line below the table data)
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height_header));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$xpos+=$width;
}
//and the final line on the right side of the table:
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height_header));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc));
pdf_stroke($this->pdf);
}
function addTable($table,$align="center")
{
//if we get a table passed in that doesnt look like a table (not an array) then just return doing nothing
if(!is_array($table)) return;
if(is_array($table['header'])) {
$table_cols=count($table['header']);
} else {
$table_cols=count($table['data']);
}
$line_height=0.18;
$table_width=array_sum($table['widths']);
$table_padding=0.03;
$allow_multiline = false;
if(is_array($table['option'])) {
$allow_multiline = ($table['option']['allow_multiline'] == true) ? true : false;
}
switch($align)
{
case "center"; $xpos_of_table=($this->page_width-$table_width)/2; break;
case "left"; $xpos_of_table=$this->page_margin; break;
case "right"; $xpos_of_table=$this->page_width-$this->page_margin-$table_width; break;
}
$top_of_table = $this->addTableStart($table, $xpos_of_table, $table_width);
//now do the data in the table
if($table['data'])
{
pdf_setfont($this->pdf,$this->normalfont,10);
foreach($table['data'] AS $dataline)
{
$this->yloc-=$height['tabledata'];
// $this->yloc-=$line_height;
$xpos=$xpos_of_table;
$extralinestomove=0;
/* Fit first */
$col_width = array();
$col_height = 1;
for($c=0;$c<$table_cols;$c++)
{
$width=$table['widths'][$c];
$textstr=trim($dataline[$c]);
$h=1;
$try=0;
$h = $col_height;
$last_notfit = 0;
//echo "allow=$allow_multiline\n";
while(1) {
// echo "h=$h, width=$width, text=[$textstr]\n";
$notfit=pdf_show_boxed($this->pdf,$textstr,
$this->loc($xpos+$table_padding),$this->loc($this->yloc-($h-1)*$height['tabledata']),
$this->loc($width-2*$table_padding),$this->loc($height['tabledata']*$h),
$this->loc($xpos+$table_padding),$this->loc($this->yloc-($h)*$line_height),
$this->loc($width-2*$table_padding),$this->loc($line_height*$h),
$table['dataalign'][$c],'blind');
// echo " nofit=$notfit\n";
@ -685,28 +726,45 @@ class lpdf
/* Increase the height and try again */
$h++;
}
$col_width[$c] = $width;
if($h > $col_height) $col_height = $h;
}
/* If this entry goes off the bottom of the
* page, start a new page, and then blindly
* dump this entry on it (but try to squeeze on
* as much as possible) */
if($this->yloc - ($line_height * $col_height) < 0.75)
{
$this->addTableEnd($table, $xpos_of_table, $top_of_table);
$this->newPage($this->page_width,$this->page_height);
$top_of_table = $this->addTableStart($table, $xpos_of_table, $table_width);
}
/* Do it for real */
for($c=0;$c<$table_cols;$c++)
{
$width = $col_width[$c];
$h = $col_height * $line_height;
$textstr=trim($dataline[$c]);
/* Do it for real */
pdf_show_boxed($this->pdf,$textstr,
$this->loc($xpos+$table_padding),$this->loc($this->yloc-($h-1)*$height['tabledata']),
$this->loc($width-2*$table_padding),$this->loc($height['tabledata']*$h),
$this->loc($xpos+$table_padding),$this->loc($this->yloc-$h),
$this->loc($width-2*$table_padding),$this->loc($h),
$table['dataalign'][$c],null);
if($h-1>$extralinestomove) $extralinestomove=$h-1;
//put a little "..." at the end of the field
if($notfit)
{
pdf_setfont($this->pdf,$this->normalfont,8);
pdf_show_boxed($this->pdf,"...",$this->loc($xpos+$width-0.10),$this->loc($this->yloc-0.05),$this->loc(0.10),$this->loc($height['tabledata']),$table['dataalign'][$c],null);
pdf_show_boxed($this->pdf,"...",$this->loc($xpos+$width-0.10),$this->loc($this->yloc-0.05),$this->loc(0.10),$this->loc($line_height),$table['dataalign'][$c],null);
pdf_setfont($this->pdf,$this->normalfont,10);
}
$xpos+=$width;
}
$this->yloc -= $height['tabledata']*$extralinestomove;
$this->yloc -= $line_height*$col_height;
//draw the line below the table data)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc));
@ -715,76 +773,15 @@ class lpdf
if($this->yloc<1.1)
{
//now draw all the vertical lines
$xpos=$xpos_of_table;
for($c=0;$c<$table_cols;$c++)
{
$width=$table['widths'][$c];
//draw the line below the table data)
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$xpos+=$width;
}
//and the final line on the right side of the table:
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$this->addTableEnd($table, $xpos_of_table, $top_of_table);
$this->newPage($this->page_width,$this->page_height);
$this->yloc-=$height['tableheader'];
$top_of_table=$this->yloc;
//draw the top line of the table (above the table header)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos_of_table+$table_width),$this->loc($this->yloc+$height['tableheader']));
pdf_stroke($this->pdf);
//draw the top line of the table (below the table header)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc));
pdf_lineto($this->pdf,$this->loc($xpos_of_table+$table_width),$this->loc($this->yloc));
pdf_stroke($this->pdf);
//do the header first
if($table['header'])
{
pdf_setfont($this->pdf,$this->headerfont,12);
$xpos=$xpos_of_table;
for($c=0;$c<$table_cols;$c++)
{
$head=$table['header'][$c];
$width=$table['widths'][$c];
pdf_show_boxed($this->pdf,$head,$this->loc($xpos),$this->loc($this->yloc),$this->loc($width),$this->loc($height['tableheader']),"center",null);
$xpos+=$width;
}
}
pdf_setfont($this->pdf,$this->normalfont,10);
$top_of_table = $this->addTableStart($table, $xpos_of_table, $table_width);
}
}
}
//now draw all the vertical lines
$xpos=$xpos_of_table;
for($c=0;$c<$table_cols;$c++)
{
$width=$table['widths'][$c];
//draw the line below the table data)
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$xpos+=$width;
}
//and the final line on the right side of the table:
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc));
pdf_stroke($this->pdf);
/* Finish the table */
$this->addTableEnd($table, $xpos_of_table, $top_of_table);
// print the total in th etable at the bottom of the table
if($table['total'] != 0) {