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