Skip to content

Commit

Permalink
New feature: tag based font style
Browse files Browse the repository at this point in the history
  • Loading branch information
dan committed Oct 12, 2017
1 parent 3a2f293 commit 3c6c84e
Show file tree
Hide file tree
Showing 17 changed files with 783 additions and 192 deletions.
50 changes: 50 additions & 0 deletions README.md
Expand Up @@ -72,6 +72,8 @@ Building and styling a table with easyTable is simple, clean and fast.

- [UTF8 Support](#fonts-and-utf8-support)

- [Tag based font style](#tag-based-font-style) which allows to mix different font families, font styles, font size and font color in the same cell! **_NEW FEATURE!!_**

# Comparisons

**easyTable vs kind-of-HTML-to-PDF**
Expand Down Expand Up @@ -791,6 +793,54 @@ And in your project:
//etc
//etc

# Tag Based Font Style

The new version of FPDF EasyTable can handle tag-based font styles at string level.

$table->easyCell('Project: EasyTable', 'font-family:lato; font-size:30; font-color:#00bfff;');

now we can do:

$table->easyCell('<b>Project:</b> <s "font-size:20; font-family:times">EasyTable</s>', 'font-family:lato; font-size:30; font-color:#00bfff;');

The font style set at the string level will over write any other font style set at the cell, row or table level.

Tags

**<s "fontstyle"></s>**

font-style is a semicolon separated string which can include: font-size, font-family, font-style; font-color;

Note: Remember to define every font your project needs.

$pdf->AddFont('MyFabFont','','my_font.php');
$pdf->AddFont('MyFabFont','B','my_font_bold.php');
$pdf->AddFont('MyFabFont','I','my_font_italic.php');
$pdf->AddFont('MyFabFont','BI','my_font_bolditalic.php');

font-color can be Hex color code or RGB color code.

**<b></b>**

Shortcut for
<s "font-style:B"></s>

**<i></i>**

Shortcut for
<s "font-style:I"></s>

**Tags can be nested**

When nested tags are used, the result is similar to the case in HTML documents.

<b>Helo <i>world</i></b>

<s "font-style:I; font-color#abc123; font-family:times">Hello <s "font-style:B; font-family:lato; font-size:20">world</s></s>

- Different font style can be applied to the letters of a word.

<b>H<i>e</i><s "font-family:myfont">ll<s "font-size">o</s></s></b>

# Get In Touch

Expand Down
Binary file modified basic_example.pdf
Binary file not shown.
26 changes: 16 additions & 10 deletions basic_example.php
Expand Up @@ -6,34 +6,40 @@
$pdf=new exFPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica','',10);


$pdf->AddFont('lato','','Lato-Regular.php');

$pdf->Write(6, 'Some writing...');

$pdf->Ln(5);

$pdf->Write(6, 'Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur quis fermentum nibh. Aenean eget tellus eu ligula hendrerit dapibus vitae at leo. Vivamus at ligula non purus iaculis eleifend. Integer eget risus non dui scelerisque consectetur. Quisque et leo ut ex lacinia malesuada dictum vitae diam. Integer eleifend in nibh in mattis. Aenean eu justo quis mauris tempus eleifend. Praesent malesuada turpis ut justo semper tempor. Integer varius, nisi non elementum molestie, leo arcu euismod velit, eu tempor ligula diam convallis sem. Sed ultrices hendrerit suscipit. Pellentesque volutpat a urna nec placerat. Etiam auctor dapibus leo nec ullamcorper. Nullam id placerat elit. Vivamus ut quam a metus tincidunt laoreet sit amet a ligula. Sed rutrum felis ipsum, sit amet finibus magna tincidunt id. Suspendisse vel urna interdum lacus luctus ornare. Curabitur ultricies nunc est, eget rhoncus orci vestibulum eleifend. In in consequat mi. Curabitur sodales magna at consequat molestie. Aliquam vulputate, neque varius maximus imperdiet, nisi orci accumsan risus, sit amet placerat augue ipsum eget elit. Quisque sodales orci non est tincidunt tincidunt. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In ut diam in dolor ultricies accumsan sit amet eu ex. Pellentesque aliquet scelerisque ullamcorper. Aenean porta enim eget nisl viverra euismod sed non eros. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque at imperdiet sem, non volutpat metus. Phasellus sed velit sed orci iaculis venenatis ac id risus.');
$pdf->Write(6, 'Integer eget risus non dui scelerisque consectetur. Integer eleifend in nibh in mattis. Aenean eu justo quis mauris tempus eleifend. Praesent malesuada turpis ut justo semper tempor. Integer varius, nisi non elementum molestie, leo arcu euismod velit, eu tempor ligula diam convallis sem. Sed ultrices hendrerit suscipit. Pellentesque volutpat a urna nec placerat. Etiam auctor dapibus leo nec ullamcorper. Nullam id placerat elit. Vivamus ut quam a metus tincidunt laoreet sit amet a ligula. Sed rutrum felis ipsum, sit amet finibus magna tincidunt id. Suspendisse vel urna interdum lacus luctus ornare. Curabitur ultricies nunc est, eget rhoncus orci vestibulum eleifend. In in consequat mi. Curabitur sodales magna at consequat molestie. Aliquam vulputate, neque varius maximus imperdiet, nisi orci accumsan risus, sit amet placerat augue ipsum eget elit. Quisque sodales orci non est tincidunt tincidunt. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In ut diam in dolor ultricies accumsan sit amet eu ex. Pellentesque aliquet scelerisque ullamcorper. Aenean porta enim eget nisl viverra euismod sed non eros. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque at imperdiet sem, non volutpat metus. Phasellus sed velit sed orci iaculis venenatis ac id risus.');

$pdf->Ln(10);

$pdf->AddFont('FontUTF8','','Arimo-Regular.php');
$pdf->AddFont('FontUTF8','B','Arimo-Bold.php');
$pdf->AddFont('FontUTF8','BI','Arimo-BoldItalic.php');
$pdf->AddFont('FontUTF8','I','Arimo-Italic.php');

$table=new easyTable($pdf, 3, 'border:1;');
$table=new easyTable($pdf, 3, 'border:1;font-size:12;');

$table->easyCell('Text 1', 'valign:T');
$table->easyCell('Text 2', 'bgcolor:#b3ccff;');
$table->easyCell('Text 3');
$table->easyCell('<b>This text is in bold</b> and <i>this is in italic</i> and this is normal', 'valign:T; bgcolor:#eee6ff;');
$table->easyCell("T<s 'font-color:#3399ff; font-style:B; font-size:19'>h</s><s 'font-color:#00cc00; font-style:I; font-size:21'>i</s>s <s 'font-color:#ff0000; font-style:BI; font-size:8'>is</s> a <s 'font-color:#0099ff; font-size:25'><b>c</b><i>r</i><bi>a</bi><b>z</b><i>y</i></s> text");
$table->easyCell("<s 'font-color:#4da6ff; font-style:B; font-size:18'><i>And can do it with Russian, just because <s 'font-size:25; font-color:#66ff99'>we can!</s></i></s>", 'font-style:I;');
$table->printRow();

$table->rowStyle('min-height:60; align:{C};'); // let's adjust the height of this row
$table->easyCell(iconv("UTF-8", 'KOI8-R', 'What about Russian? Вери порро номинати вел ех, еум те лаореет импедит, ест но ферри ириуре. Ет вис реяуе хомеро. Перфецто сцрипсерит вис еу, нам ин ассум пробатус. Фиерент импердиет аппеллантур меи но, граеце яуодси пертинациа вел ад, не при лудус оратио тациматес. Хис дебет дефинитионес цу.'), 'colspan:3; font-family:FontUTF8;');
$table->rowStyle('min-height:60; align:{C};font-size:18;'); // let's adjust the height of this row
$table->easyCell(iconv("UTF-8", 'KOI8-R', '<b>Вери</b> порро <i>номинати</i> вел ех, <b><i>еум</i></b> те лаореет импедит, <s "font-style:B;font-size:18; font-color:#3399ff">ест но ферри ириуре.</s> Ет вис реяуе хомеро. Перфецто сцрипсерит вис еу, нам ин ассум пробатус. Фиерент импердиет аппеллантур меи но, граеце яуодси пертинациа вел ад, не при лудус оратио тациматес. Хис дебет дефинитионес цу.
<s "font-family:times;font-size:20;">Set different font-families <s "font-family:lato;font-size:25;">in the same cell</s></s> '), 'colspan:4; font-family:FontUTF8; font-size:12;');

$table->printRow();

$table->endTable(4);

//-----------------------------------------



$pdf->Output();

?>
150 changes: 34 additions & 116 deletions easyTable.php
Expand Up @@ -2,8 +2,8 @@
/*********************************************************************
* FPDF easyTable *
* *
* Version: 1.02 *
* Date: 17-03-2017 *
* Version: 2.0 *
* Date: 12-10-2017 *
* Author: Dan Machado *
* Require exFPDF v1.01 *
**********************************************************************/
Expand All @@ -15,8 +15,6 @@ class easyTable{
const IMGPadding=0.5;
const PBThreshold=30;
static private $table_counter=false;
static private $hex=array('0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,
'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15);
static private $style=array('width'=>false, 'border'=>false, 'border-color'=>false,
'border-width'=>false, 'line-height'=>false,
'align'=>'', 'valign'=>'', 'bgcolor'=>false, 'split-row'=>false, 'l-margin'=>false,
Expand Down Expand Up @@ -57,92 +55,6 @@ private function get_available($colspan, $rowspan){
return $k;
}

private function is_rgb($str){
$a=true;
$tmp=explode(',', trim($str, ','));
foreach($tmp as $color){
if(!is_numeric($color) || $color<0 || $color>255){
$a=false;
break;
}
}
return $a;
}

private function is_hex($str){
$a=true;
$str=strtoupper($str);
$n=strlen($str);
if(($n==7 || $n==4) && $str[0]=='#'){
for($i=1; $i<$n; $i++){
if(!isset(self::$hex[$str[$i]])){
$a=false;
break;
}
}
}
else{
$a=false;
}
return $a;
}

private function hextodec($str){
$result=array();
$str=strtoupper(substr($str,1));
$n=strlen($str);
for($i=0; $i<3; $i++){
if($n==6){
$result[$i]=self::$hex[$str[2*$i]]*16+self::$hex[$str[2*$i+1]];
}
else{
$result[$i]=self::$hex[$str[$i]]*16+self::$hex[$str[$i]];
}
}
return $result;
}

private function set_color($str){
$result=array();
if($this->is_hex($str)){
$result=$this->hextodec($str);
}
elseif($this->is_rgb($str)){
$result=explode(',', trim($str, ','));
for($i=0; $i<3; $i++){
if(!isset($result[$i])){
$result[$i]=0;
}
}
}
return $result;
}

private function getColor($str){
$result=array(null, null, null);
$i=0;
$tmp=explode(' ', $str);
foreach($tmp as $c){
if(is_numeric($c)){
$result[$i]=$c*256;
$i++;
}
}
return $result;
}

private function resetColor($array, $p='F'){
if($p=='T'){
$this->pdf_obj->SetTextColor($array[0],$array[1],$array[2]);
}
elseif($p=='D'){
$this->pdf_obj->SetDrawColor($array[0],$array[1], $array[2]);
}
else{
$this->pdf_obj->SetFillColor($array[0],$array[1],$array[2]);
}
}

private function get_style($str, $c){
$result=self::$style;
if($c=='C'){
Expand Down Expand Up @@ -277,7 +189,7 @@ private function set_style($str, $c, $pos=''){
}
$color_settings=array('bgcolor', 'font-color', 'border-color');
foreach($color_settings as $setting){
if($sty[$setting]===false || !($this->is_hex($sty[$setting]) || $this->is_rgb($sty[$setting]))){
if($sty[$setting]===false || !($this->pdf_obj->is_hex($sty[$setting]) || $this->pdf_obj->is_rgb($sty[$setting]))){
if($c=='C' || $c=='R'){
$this->inherating($sty, $setting, $c);
}
Expand All @@ -286,7 +198,7 @@ private function set_style($str, $c, $pos=''){
}
}
else{
$sty[$setting]=$this->set_color($sty[$setting]);
$sty[$setting]=$sty[$setting];
}
}
$font_settings=array('font-family', 'font-style', 'font-size');
Expand Down Expand Up @@ -425,7 +337,7 @@ private function mk_border($i, $y, $split){
$h=$this->pdf_obj->PageBreak()-$y;
}
if($this->row_data[$i][1]['border-color']!=false){
$this->resetColor($this->row_data[$i][1]['border-color'], 'D');
$this->pdf_obj->resetColor($this->row_data[$i][1]['border-color'], 'D');
}
$a=array(1, 1, 1, 0);
$borders=array('L'=>3, 'T'=>0, 'R'=>1, 'B'=>2);
Expand All @@ -443,10 +355,10 @@ private function mk_border($i, $y, $split){
}

if($this->row_data[$i][1]['border-color']!=false){
$this->resetColor($this->document_style['bgcolor'], 'D');
$this->pdf_obj->resetColor($this->document_style['bgcolor'], 'D');
}
if($split){
$this->row_data[$i][1]['border']['T']=0;
$this->pdf_obj->row_data[$i][1]['border']['T']=0;
}
}

Expand All @@ -464,11 +376,9 @@ private function print_text($i, $y, $split){
$xpadding=2*$this->row_data[$i][1]['paddingX'];
$l=count($this->row_data[$i][0])* $this->row_data[$i][1]['line-height']*$this->row_data[$i][1]['font-size'];
$this->pdf_obj->SetXY($x, $y+$k);
$this->resetColor($this->row_data[$i][1]['font-color'], 'T');
$this->pdf_obj->SetFont($this->row_data[$i][1]['font-family'], $this->row_data[$i][1]['font-style'], $this->row_data[$i][1]['font-size']);
$this->pdf_obj->CellBlock($this->row_data[$i][2]-$xpadding, $this->row_data[$i][1]['line-height']*$this->row_data[$i][1]['font-size'], $this->row_data[$i][0], $this->row_data[$i][1]['align']);
$this->pdf_obj->CellBlock($this->row_data[$i][2]-$xpadding, $this->row_data[$i][1]['line-height'], $this->row_data[$i][0], $this->row_data[$i][1]['align']);
$this->pdf_obj->SetFont($this->document_style['font-family'], $this->document_style['font-style'], $this->document_style['font-size']);
$this->resetColor($this->document_style['font-color'], 'T');
$this->pdf_obj->resetColor($this->document_style['font-color'], 'T');
}
if($this->row_data[$i][1]['img']!==false ){
$x=$this->row_data[$i][6];
Expand Down Expand Up @@ -500,9 +410,9 @@ private function mk_bg($i, $T, $split){
$h=$this->pdf_obj->PageBreak()-$T;
}
if($this->row_data[$i][1]['bgcolor']!=false){
$this->resetColor($this->row_data[$i][1]['bgcolor']);
$this->pdf_obj->resetColor($this->row_data[$i][1]['bgcolor']);
$this->pdf_obj->Rect($this->row_data[$i][6], $T, $this->row_data[$i][2], $h, 'F');
$this->resetColor($this->document_style['bgcolor']);
$this->pdf_obj->resetColor($this->document_style['bgcolor']);
}
}

Expand Down Expand Up @@ -625,11 +535,21 @@ private function scan_for_breaks($index, $H, $l=true){
}
}
}
if($mx==0 && $rr<($this->row_data[$index][1]['line-height']*$this->row_data[$index][1]['font-size'])*count($this->row_data[$index][0])){
$rr=$rr/($this->row_data[$index][1]['line-height']*$this->row_data[$index][1]['font-size']);
$n=floor($rr);
if($n<count($this->row_data[$index][0])){
$mx=($this->row_data[$index][1]['line-height']*$this->row_data[$index][1]['font-size'])*($rr-$n);
$nh=0;
$keys=array_keys($this->row_data[$index][0]);
foreach($keys as $i){
$nh+=$this->row_data[$index][0][$i]['height'];
}
$nh*=$this->row_data[$index][1]['line-height'];
if($mx==0 && $rr<$nh){
$nw=0;
foreach($keys as $i){
$nw+=$this->row_data[$index][0][$i]['height']*$this->row_data[$index][1]['line-height'];
if($nw>$rr){
$nw-=$this->row_data[$index][0][$i]['height']*$this->row_data[$index][1]['line-height'];
$mx=$rr-$nw;
break;
}
}
}
$this->overflow=max($this->overflow, $mx);
Expand Down Expand Up @@ -714,11 +634,11 @@ public function __construct($fpdf_obj, $num_cols, $style=''){
}
self::$table_counter=true;
$this->pdf_obj=&$fpdf_obj;
$this->document_style['bgcolor']=$this->getColor($this->pdf_obj->get_color('fill'));
$this->document_style['bgcolor']=$this->pdf_obj->get_color('fill');
$this->document_style['font-family']=$this->pdf_obj->current_font('family');
$this->document_style['font-style']=$this->pdf_obj->current_font('style');
$this->document_style['font-size']=$this->pdf_obj->current_font('size');
$this->document_style['font-color']=$this->getColor($this->pdf_obj->get_color('text'));
$this->document_style['font-color']=$this->pdf_obj->get_color('text');
$this->document_style['document_width']=$this->pdf_obj->GetPageWidth()-$this->pdf_obj->get_margin('l')-$this->pdf_obj->get_margin('r');
$this->document_style['orientation']=$this->pdf_obj->get_orientation();
$this->document_style['line-width']=$this->pdf_obj->get_linewidth();
Expand Down Expand Up @@ -875,8 +795,12 @@ public function easyCell($data, $style=''){
$data=$sty['img'];
$sty['img']=false;
}
$data=$this->pdf_obj->extMultiCell($sty['font-family'], $sty['font-style'], $sty['font-size'], $w, $data);
$h=count($data) * $sty['line-height']*$sty['font-size'];
$data=& $this->pdf_obj->extMultiCell($sty['font-family'], $sty['font-style'], $sty['font-size'], $sty['font-color'], $w, $data);
$h=0;
$rn=count($data);
for($ri=0; $ri<$rn; $ri++){
$h+=$data[$ri]['height']*$sty['line-height'];
}
if($sty['img']){
if($sty['img']['w']>$w){
$sty['img']['h']=$w*$sty['img']['h']/$sty['img']['w'];
Expand Down Expand Up @@ -1055,12 +979,6 @@ public function endTable($bottomMargin=2){
unset($this->overflow);
unset($this->header_row);
}

public function write_file($file, $str, $mod='w'){
$h=fopen('/var/www/html/' . $file, $mod);
fwrite($h, var_export($str,true) . "\n");
fclose($h);
}

}
?>
Binary file modified example-2.pdf
Binary file not shown.
Binary file modified example-3.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion example1.php
Expand Up @@ -411,7 +411,7 @@
$write->endTable(5);

$rows=20;
$table=new easyTable($pdf, '{100, 30,30,30}', 'align:L; border:1');
$table=new easyTable($pdf, '{100, 30,30,30}', 'split-row:true; align:L; border:1');
$table->easyCell($text, "rowspan:$rows; border:0;");
$table->easyCell('text 1', 'bgcolor:#000; font-color:#fff');
$table->easyCell('text 2', 'bgcolor:#000; font-color:#fff');
Expand Down

0 comments on commit 3c6c84e

Please sign in to comment.