RelatorioPDF.php
5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
require_once('Relatorio.php');
require_once('fpdf.php');
class PDFMTable extends FPDF
{
private $widths;
private $aligns;
public function PDFMTable()
{
parent::__construct('L', 'mm', 'a3');
}
function SetWidths($w)
{
//Set the array of column widths
$this->widths=$w;
$fullwidth = 0;
foreach ($w as $wh)
{
$fullwidth += $wh;
}
$this->fhPt = ($fullwidth + $this->lMargin + $this->rMargin) * $this->k;
}
function SetAligns($a)
{
//Set the array of column alignments
$this->aligns=$a;
}
function Row($data, $fillcolor, $fill, $cellColors)
{
$this->SetFillColor($fillcolor[0], $fillcolor[1], $fillcolor[2]);
//Calculate the height of the row
$nb=0;
$i = 0;
foreach ($data as $value)
{
$nb=max($nb,$this->NbLines($this->widths[$i],$value));
$i++;
}
$h=5*$nb;
//Issue a page break first if needed
$this->CheckPageBreak($h);
$i = 0;
foreach ($data as $value)
{
$this->SetTextColor($cellColors[$i][0], $cellColors[$i][1], $cellColors[$i][2]);
$w=$this->widths[$i];
$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
//Save the current position
$x=$this->GetX();
$y=$this->GetY();
if ($fill)
{
$style = 'DF';
}
else
{
$style = 'D';
}
//Draw the border
$this->Rect($x,$y,$w,$h, $style);
//Print the text
$this->MultiCell($w,5,$value,0,$a);
//Put the position to the right of the cell
$this->SetXY($x+$w,$y);
$i++;
}
//Go to the next line
$this->Ln($h);
}
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 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;
}
}
class RelatorioPDF extends Relatorio
{
private function getMaxTextWidth($fpdf)
{
$maxWidths = array();
$mstr = array();
$arrays = array(array($this->getHeader()), $this->getBody());
foreach ($arrays as $array)
{
foreach ($array as $row)
{
$i = 0;
foreach ($row as $cell)
{
$w = 0;
$cell = $this->relstrip($cell);
if (strpos($cell, "\n") !== FALSE)
{
$lines = explode("\n", $cell);
foreach ($lines as $ln)
{
$tmp = $fpdf->GetStringWidth($ln);
if ($tmp > $w)
{
$w = $tmp;
$lol = $ln;
}
}
}
else
{
$w = $fpdf->GetStringWidth((string) $cell);
}
if (!isset($maxWidths[$i]))
{
$maxWidths[$i] = $w + 10;
$mstr[$i] = $cell;
}
else if ($w > $maxWidths[$i])
{
$maxWidths[$i] = $w + 10;
$mstr[$i] = $cell;
}
$i++;
}
}
}
return $maxWidths;
}
public function html2rgb($color)
{
if ($color[0] == '#')
$color = substr($color, 1);
if (strlen($color) == 6)
list($r, $g, $b) = array($color[0].$color[1],
$color[2].$color[3],
$color[4].$color[5]);
elseif (strlen($color) == 3)
list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
else
return false;
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array($r, $g, $b);
}
public function output()
{
$pdf = new PDFMTable();
//Colors, line width and bold font
$pdf->SetFillColor(90, 90, 200);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128, 0, 0);
$pdf->SetLineWidth(.3);
$pdf->SetFont('Arial');
//Header
$w = $this->getMaxTextWidth($pdf);
$fullwidth = array_sum($w);
$pdf->AddPage('L');
$pdf->SetWidths($w);
$header = $this->getHeader();
$size = count($header);
$i = 0;
foreach ($header as $cell)
{
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->MultiCell($w[$i], 7, $this->relstrip($cell), 1, 'C', 1);
$pdf->SetXY($x + $w[$i], $y);
$i++;
}
$pdf->Ln();
//Color and font restoration
$pdf->SetFillColor(224, 235, 255);
$defaultFillColor = array(224, 235, 255);
$defaultTextColor = 0;
$pdf->SetTextColor($defaultTextColor);
$pdf->SetFont('Arial');
//Data
$fill = 0;
#foreach ($this->getBody() as $row)
$body = &$this->getBody();
$size = count($body);
for ($i = 0; $i < $size; $i++)
{
$row = &$body[$i];
$fillcolor = $defaultFillColor;
$color = $this->getRowColor($i);
if ($color)
{
$fillcolor = $color;
$fill = 1;
}
$count = count($row);
$j = 0;
$cellColors = array();
foreach ($row as $key => $value)
{
$cellColors[$j] = $this->getCellColor($i, $j);
$row[$key] = $this->relstrip($value);// iconv('utf-8' ,'iso-8859-1', 'AAA'/*strip_tags($row[$j])*/);
$j++;
}
$pdf->Row($row, $fillcolor, $fill, $cellColors);
$fill = !$fill;
}
$pdf->Output('relatorio.pdf', 'D');
}
}
?>