Skip to content

Commit c93f09e

Browse files
Extend default settings to other user units
1 parent 4f3b812 commit c93f09e

File tree

5 files changed

+148
-19
lines changed

5 files changed

+148
-19
lines changed

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ Building and styling a table with easyTable is simple, clean and fast.
4242
- [Documentation](#documentation)
4343
- [Fonts And UTF8 Support](#fonts-and-utf8-support)
4444
- [Using with FPDI](#using-with-fpdi)
45-
- [Tag based font style](#tag-based-font-style) **_NEW FEATURE!!_**
45+
- [Tag based font style](#tag-based-font-style)
46+
- [User units](#user-units) **_NEW FEATURE!!_**
4647
- [Common error](#common-error)
4748
- [Get In Touch](#get-in-touch)
4849
- [Donations](#donations)
4950
- [License](#license)
5051

5152
# Features
5253

53-
- Table and columns width can be defined in mm or percentage
54+
- Table and columns width can be defined in [user units](#user-units) or percentage
5455

5556
- Every table cell is a fully customizable
5657
(font family, font size, font color, background color, position of the text,
@@ -310,8 +311,7 @@ setAsHeader (optional)
310311

311312
bottomMargin (optional)
312313

313-
Optional. Specify the number of white lines left after
314-
the last row of the table. Default 2.
314+
Optional. Specify the size in [user units](#user-units) of the bottom margin for the table. Default 2 in user units.
315315

316316
If it is negative, the vertical position will be set before
317317
the end of the table.
@@ -345,15 +345,15 @@ Full list of properties:
345345
**width** [T]
346346

347347
The width property sets the width of a table.
348-
This property can be defined in millimetres or in percentage of the width of the document.
348+
This property can be defined in [user units](#user-units) or in percentage of the width of the document.
349349

350350
Syntax:
351351

352-
width:mm|%;
352+
width:user-units|%;
353353

354354
Examples:
355355

356-
width:145;
356+
width:145;// 145mm if the user units is mm
357357
width:70%;
358358

359359
Default: the width of the document minus the right and left margin.
@@ -432,7 +432,7 @@ This property indicate the distance from the left margin from where the table sh
432432

433433
Syntax:
434434

435-
l-maring:mm;
435+
l-maring:user-units;
436436

437437
Example:
438438

@@ -447,7 +447,7 @@ specific row.
447447

448448
Syntax:
449449

450-
min-height:mm;
450+
min-height:user-units;
451451

452452
Example:
453453

@@ -633,7 +633,7 @@ The paddingX property sets the left and right padding (space) of the cells.
633633

634634
Syntax:
635635

636-
paddingX:mm;
636+
paddingX:user-units;
637637

638638
Example:
639639

@@ -647,7 +647,7 @@ The paddingY property sets the top and bottom padding (space) of the cells.
647647

648648
Syntax
649649

650-
paddingY:mm;
650+
paddingY:user-units;
651651

652652
Example:
653653

@@ -865,6 +865,14 @@ The sequence '\\<s' is parced as '<s'
865865

866866
<b>Helo <s "font-family:my_fab_font;">\<sammy@example.com></s></b>
867867

868+
869+
# User units
870+
871+
EasyTable supports the same user units (pt/mm/cm/in) supported by [FPDF: construct](http://www.fpdf.org/en/doc/__construct.htm).
872+
Bare in mind that any unit related setting (width, border) needs to be in the respective unit set at the
873+
top document. For example if the units for the document is set as inch, then, all the settings
874+
unit related will be considered in the same user units, for instance min-height:1.2; will mean 1.2in.
875+
868876
# Common Error
869877

870878
A very typical situation is: *"EasyTable works in my localhost but it does not

easyTable.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ private function inherating(&$sty, $setting, $c){
105105
}
106106
}
107107

108+
private function conv_units($x)
109+
{
110+
if($this->pdf_obj->get_scale_factor()==72/25.4)
111+
{
112+
return $x;
113+
}
114+
115+
if($this->pdf_obj->get_scale_factor()==72/2.54)
116+
{
117+
return $x/10;
118+
}
119+
120+
if($this->pdf_obj->get_scale_factor()==72)
121+
{
122+
return $x/25.4;
123+
}
124+
125+
return $x/0.3527777778;
126+
}
108127

109128
private function set_style($str, $c, $pos=''){
110129
$sty=$this->get_style($str, $c);
@@ -159,7 +178,7 @@ private function set_style($str, $c, $pos=''){
159178
$this->inherating($sty, 'paddingX', $c);
160179
}
161180
else{
162-
$sty['paddingX']=self::XPadding;
181+
$sty['paddingX']=$this->conv_units(self::XPadding);
163182
}
164183
}
165184
$sty['paddingX']=abs($sty['paddingX']);
@@ -168,7 +187,7 @@ private function set_style($str, $c, $pos=''){
168187
$this->inherating($sty, 'paddingY', $c);
169188
}
170189
else{
171-
$sty['paddingY']=self::YPadding;
190+
$sty['paddingY']=$this->conv_units(self::YPadding);
172191
}
173192
}
174193
$sty['paddingY']=abs($sty['paddingY']);
@@ -208,14 +227,14 @@ private function set_style($str, $c, $pos=''){
208227
}
209228
}
210229
if(is_numeric($sty['line-height'])){
211-
$sty['line-height']=self::LP*abs($sty['line-height']);
230+
$sty['line-height']=$this->conv_units(self::LP)*abs($sty['line-height']);
212231
}
213232
else{
214233
if($c=='C' || $c=='R'){
215234
$this->inherating($sty,'line-height', $c);
216235
}
217236
else{
218-
$sty['line-height']=self::LP;
237+
$sty['line-height']=$this->conv_units(self::LP);
219238
}
220239
}
221240
if($c=='C'){
@@ -367,7 +386,7 @@ private function print_text($i, $y, $split){
367386
$k=$padding;
368387
if($this->row_data[$i][1]['img']!==false){
369388
if($this->row_data[$i][1]['valign']=='B'){
370-
$k+=$this->row_data[$i][1]['img']['h']+self::IMGPadding;
389+
$k+=$this->row_data[$i][1]['img']['h']+$this->conv_units(self::IMGPadding);
371390
}
372391
}
373392
$l=0;
@@ -384,7 +403,7 @@ private function print_text($i, $y, $split){
384403
$x=$this->row_data[$i][6];
385404
$k=$padding;
386405
if($this->row_data[$i][1]['valign']!='B'){
387-
$k+=$l+self::IMGPadding;
406+
$k+=$l+$this->conv_units(self::IMGPadding);
388407
}
389408
if($this->imgbreak($i, $y)==0 && $y+$k+$this->row_data[$i][1]['img']['h']<$this->pdf_obj->PageBreak()){
390409
$x+=$this->row_data[$i][1]['paddingX'];
@@ -815,7 +834,7 @@ public function easyCell($data, $style=''){
815834
$sty['img']['w']=$w;
816835
}
817836
if($h){
818-
$h+=self::IMGPadding;
837+
$h+=$this->conv_units(self::IMGPadding);
819838
}
820839
$h+=$sty['img']['h'];
821840
}
@@ -932,7 +951,7 @@ public function printRow($setAsHeader=false){
932951
if($this->new_table){
933952
if(count($this->header_row)>0){
934953
$r=$this->pdf_obj->PageBreak()-($this->pdf_obj->GetY()+$block_height);
935-
if($r<0 || $r<self::PBThreshold){
954+
if($r<0 || $r<$this->conv_units(self::PBThreshold)){
936955
$this->pdf_obj->addPage($this->document_style['orientation'], $this->pdf_obj->get_page_size(), $this->pdf_obj->get_rotation());
937956
}
938957
}

example-5.pdf

257 KB
Binary file not shown.

example5.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
include 'fpdf.php';
3+
include 'exfpdf.php';
4+
include 'easyTable.php';
5+
6+
$pdf=new exFPDF('P', 'in');
7+
$pdf->AddPage();
8+
$pdf->SetFont('helvetica','',10);
9+
10+
$write=new easyTable($pdf, 1, 'width:10; align:L; dfont-style:B; font-size:15;font-family:times;');
11+
$write->easyCell('Using inches as user units');
12+
$write->printRow();
13+
14+
$write->endTable(1);
15+
16+
$tableB=new easyTable($pdf, 5, 'width:5; line-height:1; align:R{LC}; border:1;border-color:#1a66ff;');
17+
18+
$tableB->easyCell("Cell 1A A\n B\n C\n D\n E\n F\n", 'rowspan:5;');
19+
$tableB->easyCell("Cell 1BC BB", 'rowspan:2; colspan:2; valign:B');
20+
$tableB->easyCell("Cell 1D 1");
21+
$tableB->easyCell("Cell 1D 1", '');
22+
$tableB->printRow();
23+
24+
$tableB->easyCell("Cell 2D 1\n 2\n 3\n");
25+
$tableB->easyCell("Cell 2D 1\n 2\n 3\n", 'rowspan:3;');
26+
$tableB->printRow();
27+
28+
$tableB->easyCell("Cell 10 ");
29+
$tableB->easyCell("Cell 12 1\n 2\n 3\n 4\n 5\n", 'rowspan:3;');
30+
$tableB->easyCell("Cell 12 1\n 2\n 3\n 4\n 5\n", 'rowspan:2;');
31+
$tableB->printRow();
32+
33+
$tableB->easyCell("Cell 12 ", '');
34+
$tableB->printRow();
35+
36+
$tableB->easyCell("Cell 10 A");
37+
$tableB->easyCell("Cell 12 1");
38+
$tableB->easyCell("Cell 12 1");
39+
$tableB->printRow();
40+
41+
$tableB->endTable(0.5);
42+
43+
//===================================================================
44+
//===================================================================
45+
46+
$table=new easyTable($pdf, '%{40, 30, 30}', 'width:3; align:{RCC}; bgcolor:#fff; line-height:1.2; border-width:0.05; border:1; border-color:#fff;');
47+
$table->easyCell('Change Plan', 'colspan:3; font-color:#bfbfbf; font-size:25; font-style:B; align:L;');
48+
$table->printRow();
49+
50+
$table->easyCell('123-123-1234: Plan name', 'colspan:3; font-color:#bfbfbf; font-size:16; align:L');
51+
$table->printRow();
52+
53+
$table->easyCell('Use the table below to help you select a new plan. Additional plan features can also be configured.', 'line-height:1.5; colspan:3; font-size:10;align:L');
54+
$table->printRow();
55+
56+
$table->rowStyle('bgcolor:#f39; font-style:B; font-color:#fff;');
57+
$table->easyCell('');
58+
$table->easyCell('Current');
59+
$table->easyCell('Plan1');
60+
$table->printRow();
61+
62+
$table->rowStyle('bgcolor:#f2f2f2; paddingY:0.2;');
63+
$table->easyCell('Data');
64+
$table->easyCell('500MB');
65+
$table->easyCell('2.5GB');
66+
$table->printRow();
67+
68+
$table->rowStyle('paddingY:0.2;');
69+
$table->easyCell("Mobile Hotspot\n Capable - $10");
70+
$table->easyCell('');
71+
$table->easyCell('', 'img:Pics/tick.png, w0.2;');
72+
$table->printRow();
73+
74+
$table->rowStyle('bgcolor:#f2f2f2; paddingY:0.2; font-style:B;');
75+
$table->easyCell('');
76+
$table->easyCell('$50');
77+
$table->easyCell('$60');
78+
$table->printRow();
79+
80+
$table->rowStyle('paddingY:0.2;');
81+
$table->easyCell("International\nCalling\n$20");
82+
$table->easyCell('');
83+
$table->easyCell('', 'img:Pics/tick.png, w0.2;');
84+
$table->printRow();
85+
86+
$table->endTable(5);
87+
88+
89+
//===================================================================
90+
//===================================================================
91+
92+
93+
$pdf->Output();
94+
95+
96+
?>

exfpdf.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public function get_rotation()
6969
return $this->CurRotation;
7070
}
7171

72+
public function get_scale_factor()
73+
{
74+
return $this->k;
75+
}
76+
7277
static private $hex=array('0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,
7378
'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15);
7479

@@ -168,6 +173,7 @@ public function resetStaticData(){
168173
self::$font_def='';
169174
self::$options=array('F'=>'', 'T'=>'', 'D'=>'');
170175
}
176+
171177
/***********************************************************************
172178
*
173179
* Based on FPDF method SetFont

0 commit comments

Comments
 (0)