 |
Downloads > Add-On |
 |
|
 |
| |
|
|
|
|
| |
Extended Cell functions
InformationsAuthor: Pivkin Vladimir License: Freeware
DescriptionThis class provides two extended versions of the Cell method, one which prints horizontal text:
Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, int fill [, mixed link]]]]]]])
and one for vertical:
VCell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, int fill]]]]]])
These methods allow multiline text with the delimiter "\n".
If the cell contains a single line and its length exceeds the size of the cell, the text will be
compressed to fit.
Changes in parameters:
border: indicates if borders must be drawn around the cell. The value can be
either a number:
- 0: no border
- >0: frame of the corresponding width
or a string containing some or all of the following characters (in any order):
- L: left
- T: top
- R: right
- B: bottom
or for bold border:
- l: left
- t: top
- r: right
- b: bottom
Default value: 0.
For VCell():
align: allows to center or align the text. Possible values are:
- U: top
- C: center (default value)
- D: bottom
Source
<?php require('fpdf.php');
class CellPDF extends FPDF { function VCell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0) { //Output a cell $k=$this->k; if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak()) { $x=$this->x; $ws=$this->ws; if($ws>0) { $this->ws=0; $this->_out('0 Tw'); } $this->AddPage($this->CurOrientation); $this->x=$x; if($ws>0) { $this->ws=$ws; $this->_out(sprintf('%.3f Tw', $ws*$k)); } } if($w==0) $w=$this->w-$this->rMargin-$this->x; $s=''; // begin change Cell function if($fill==1 or $border>0) { if($fill==1) $op=($border>0) ? 'B' : 'f'; else $op='S'; if ($border>1) { $s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ', $border, $this->x*$k, ($this->h-$this->y)*$k, $w*$k, -$h*$k, $op); } else $s=sprintf('%.2f %.2f %.2f %.2f re %s ', $this->x*$k, ($this->h-$this->y)*$k, $w*$k, -$h*$k, $op); } if(is_string($border)) { $x=$this->x; $y=$this->y; if(is_int(strpos($border, 'L'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k, ($this->h-$y)*$k, $x*$k, ($this->h-($y+$h))*$k); else if(is_int(strpos($border, 'l'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x*$k, ($this->h-$y)*$k, $x*$k, ($this->h-($y+$h))*$k); if(is_int(strpos($border, 'T'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-$y)*$k); else if(is_int(strpos($border, 't'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-$y)*$k); if(is_int(strpos($border, 'R'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', ($x+$w)*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); else if(is_int(strpos($border, 'r'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', ($x+$w)*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); if(is_int(strpos($border, 'B'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k, ($this->h-($y+$h))*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); else if(is_int(strpos($border, 'b'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x*$k, ($this->h-($y+$h))*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); } if(trim($txt)!='') { $cr=substr_count($txt, "\n"); if ($cr>0) { // Multi line $txts = explode("\n", $txt); $lines = count($txts); for($l=0;$l<$lines;$l++) { $txt=$txts[$l]; $w_txt=$this->GetStringWidth($txt); if ($align=='U') $dy=$this->cMargin+$w_txt; elseif($align=='D') $dy=$h-$this->cMargin; else $dy=($h+$w_txt)/2; $txt=str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt))); if($this->ColorFlag) $s.='q '.$this->TextColor.' '; $s.=sprintf('BT 0 1 -1 0 %.2f %.2f Tm (%s) Tj ET ', ($this->x+.5*$w+(.7+$l-$lines/2)*$this->FontSize)*$k, ($this->h-($this->y+$dy))*$k, $txt); if($this->ColorFlag) $s.='Q '; } } else { // Single line $w_txt=$this->GetStringWidth($txt); $Tz=100; if ($w_txt>$h-2*$this->cMargin) { $Tz=($h-2*$this->cMargin)/$w_txt*100; $w_txt=$h-2*$this->cMargin; } if ($align=='U') $dy=$this->cMargin+$w_txt; elseif($align=='D') $dy=$h-$this->cMargin; else $dy=($h+$w_txt)/2; $txt=str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt))); if($this->ColorFlag) $s.='q '.$this->TextColor.' '; $s.=sprintf('q BT 0 1 -1 0 %.2f %.2f Tm %.2f Tz (%s) Tj ET Q ', ($this->x+.5*$w+.3*$this->FontSize)*$k, ($this->h-($this->y+$dy))*$k, $Tz, $txt); if($this->ColorFlag) $s.='Q '; } } // end change Cell function if($s) $this->_out($s); $this->lasth=$h; if($ln>0) { //Go to next line $this->y+=$h; if($ln==1) $this->x=$this->lMargin; } else $this->x+=$w; }
function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='') { //Output a cell $k=$this->k; if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak()) { $x=$this->x; $ws=$this->ws; if($ws>0) { $this->ws=0; $this->_out('0 Tw'); } $this->AddPage($this->CurOrientation); $this->x=$x; if($ws>0) { $this->ws=$ws; $this->_out(sprintf('%.3f Tw', $ws*$k)); } } if($w==0) $w=$this->w-$this->rMargin-$this->x; $s=''; // begin change Cell function 12.08.2003 if($fill==1 or $border>0) { if($fill==1) $op=($border>0) ? 'B' : 'f'; else $op='S'; if ($border>1) { $s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ', $border, $this->x*$k, ($this->h-$this->y)*$k, $w*$k, -$h*$k, $op); } else $s=sprintf('%.2f %.2f %.2f %.2f re %s ', $this->x*$k, ($this->h-$this->y)*$k, $w*$k, -$h*$k, $op); } if(is_string($border)) { $x=$this->x; $y=$this->y; if(is_int(strpos($border, 'L'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k, ($this->h-$y)*$k, $x*$k, ($this->h-($y+$h))*$k); else if(is_int(strpos($border, 'l'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x*$k, ($this->h-$y)*$k, $x*$k, ($this->h-($y+$h))*$k); if(is_int(strpos($border, 'T'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-$y)*$k); else if(is_int(strpos($border, 't'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-$y)*$k); if(is_int(strpos($border, 'R'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', ($x+$w)*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); else if(is_int(strpos($border, 'r'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', ($x+$w)*$k, ($this->h-$y)*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); if(is_int(strpos($border, 'B'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k, ($this->h-($y+$h))*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); else if(is_int(strpos($border, 'b'))) $s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ', $x*$k, ($this->h-($y+$h))*$k, ($x+$w)*$k, ($this->h-($y+$h))*$k); } if (trim($txt)!='') { $cr=substr_count($txt, "\n"); if ($cr>0) { // Multi line $txts = explode("\n", $txt); $lines = count($txts); //$dy=($h-2*$this->cMargin)/$lines; for($l=0;$l<$lines;$l++) { $txt=$txts[$l]; $w_txt=$this->GetStringWidth($txt); if($align=='R') $dx=$w-$w_txt-$this->cMargin; elseif($align=='C') $dx=($w-$w_txt)/2; else $dx=$this->cMargin;
$txt=str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt))); if($this->ColorFlag) $s.='q '.$this->TextColor.' '; $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET ', ($this->x+$dx)*$k, ($this->h-($this->y+.5*$h+(.7+$l-$lines/2)*$this->FontSize))*$k, $txt); if($this->underline) $s.=' '.$this->_dounderline($this->x+$dx, $this->y+.5*$h+.3*$this->FontSize, $txt); if($this->ColorFlag) $s.='Q '; if($link) $this->Link($this->x+$dx, $this->y+.5*$h-.5*$this->FontSize, $w_txt, $this->FontSize, $link); } } else { // Single line $w_txt=$this->GetStringWidth($txt); $Tz=100; if ($w_txt>$w-2*$this->cMargin) { // Need compression $Tz=($w-2*$this->cMargin)/$w_txt*100; $w_txt=$w-2*$this->cMargin; } if($align=='R') $dx=$w-$w_txt-$this->cMargin; elseif($align=='C') $dx=($w-$w_txt)/2; else $dx=$this->cMargin; $txt=str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt))); if($this->ColorFlag) $s.='q '.$this->TextColor.' '; $s.=sprintf('q BT %.2f %.2f Td %.2f Tz (%s) Tj ET Q ', ($this->x+$dx)*$k, ($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k, $Tz, $txt); if($this->underline) $s.=' '.$this->_dounderline($this->x+$dx, $this->y+.5*$h+.3*$this->FontSize, $txt); if($this->ColorFlag) $s.='Q '; if($link) $this->Link($this->x+$dx, $this->y+.5*$h-.5*$this->FontSize, $w_txt, $this->FontSize, $link); } } // end change Cell function 12.08.2003 if($s) $this->_out($s); $this->lasth=$h; if($ln>0) { //Go to next line $this->y+=$h; if($ln==1) $this->x=$this->lMargin; } else $this->x+=$w; }
}
|
Example
<?php define('FPDF_FONTPATH', 'font/'); require('cellpdf.php');
$pdf=new CellPDF(); $pdf->Open(); $pdf->AddPage(); $pdf->SetFont('Arial', '', 12);
$pdf->VCell(15, 50, "Text at\nbottom", 1, 0, 'D'); $pdf->VCell(10, 50, 'Centered text', 2, 0, 'C'); $pdf->VCell(15, 50, "Text\non top", 1, 0, 'U');
$pdf->Cell(50, 50, "Text on\nthe left", 'lbtR', 0, 'L'); $pdf->Cell(50, 50, 'This line is very long and gets compressed', 'LtRb', 0, 'C'); $pdf->Cell(50, 50, "Text on\nthe right", 'Ltrb', 0, 'R');
$pdf->Output(); ?>
|
View the result here.
DownloadZIP | TGZ
| |