1 | <?php |
---|
2 | define('FPDF_FONTPATH','../font/'); |
---|
3 | require('../fpdf.php'); |
---|
4 | |
---|
5 | class PDF extends FPDF |
---|
6 | { |
---|
7 | //Load data |
---|
8 | function LoadData($file) |
---|
9 | { |
---|
10 | //Read file lines |
---|
11 | $lines=file($file); |
---|
12 | $data=array(); |
---|
13 | foreach($lines as $line) |
---|
14 | $data[]=explode(';',chop($line)); |
---|
15 | return $data; |
---|
16 | } |
---|
17 | |
---|
18 | //Simple table |
---|
19 | function BasicTable($header,$data) |
---|
20 | { |
---|
21 | //Header |
---|
22 | foreach($header as $col) |
---|
23 | $this->Cell(40,7,$col,1); |
---|
24 | $this->Ln(); |
---|
25 | //Data |
---|
26 | foreach($data as $row) |
---|
27 | { |
---|
28 | foreach($row as $col) |
---|
29 | $this->Cell(40,6,$col,1); |
---|
30 | $this->Ln(); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | //Better table |
---|
35 | function ImprovedTable($header,$data) |
---|
36 | { |
---|
37 | //Column widths |
---|
38 | $w=array(40,35,40,45); |
---|
39 | //Header |
---|
40 | for($i=0;$i<count($header);$i++) |
---|
41 | $this->Cell($w[$i],7,$header[$i],1,0,'C'); |
---|
42 | $this->Ln(); |
---|
43 | //Data |
---|
44 | foreach($data as $row) |
---|
45 | { |
---|
46 | $this->Cell($w[0],6,$row[0],'LR'); |
---|
47 | $this->Cell($w[1],6,$row[1],'LR'); |
---|
48 | $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R'); |
---|
49 | $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R'); |
---|
50 | $this->Ln(); |
---|
51 | } |
---|
52 | //Closure line |
---|
53 | $this->Cell(array_sum($w),0,'','T'); |
---|
54 | } |
---|
55 | |
---|
56 | //Colored table |
---|
57 | function FancyTable($header,$data) |
---|
58 | { |
---|
59 | //Colors, line width and bold font |
---|
60 | $this->SetFillColor(255,0,0); |
---|
61 | $this->SetTextColor(255); |
---|
62 | $this->SetDrawColor(128,0,0); |
---|
63 | $this->SetLineWidth(.3); |
---|
64 | $this->SetFont('','B'); |
---|
65 | //Header |
---|
66 | $w=array(40,35,40,45); |
---|
67 | for($i=0;$i<count($header);$i++) |
---|
68 | $this->Cell($w[$i],7,$header[$i],1,0,'C',1); |
---|
69 | $this->Ln(); |
---|
70 | //Color and font restoration |
---|
71 | $this->SetFillColor(224,235,255); |
---|
72 | $this->SetTextColor(0); |
---|
73 | $this->SetFont(''); |
---|
74 | //Data |
---|
75 | $fill=0; |
---|
76 | foreach($data as $row) |
---|
77 | { |
---|
78 | $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill); |
---|
79 | $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill); |
---|
80 | $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill); |
---|
81 | $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill); |
---|
82 | $this->Ln(); |
---|
83 | $fill=!$fill; |
---|
84 | } |
---|
85 | $this->Cell(array_sum($w),0,'','T'); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | $pdf=new PDF(); |
---|
90 | //Column titles |
---|
91 | $header=array('Country','Capital','Area (sq km)','Pop. (thousands)'); |
---|
92 | //Data loading |
---|
93 | $data=$pdf->LoadData('countries.txt'); |
---|
94 | $pdf->SetFont('Arial','',14); |
---|
95 | $pdf->AddPage(); |
---|
96 | $pdf->BasicTable($header,$data); |
---|
97 | $pdf->AddPage(); |
---|
98 | $pdf->ImprovedTable($header,$data); |
---|
99 | $pdf->AddPage(); |
---|
100 | $pdf->FancyTable($header,$data); |
---|
101 | $pdf->Output(); |
---|
102 | ?> |
---|