westdc-zf1/include/fpdf/tutorial/tuto5.php

103 lines
2.3 KiB
PHP
Raw Normal View History

2009-03-06 03:20:46 +00:00
<?php
require('../fpdf.php');
class PDF extends FPDF
{
2011-11-03 02:25:38 +00:00
// Load data
2009-03-06 03:20:46 +00:00
function LoadData($file)
{
2011-11-03 02:25:38 +00:00
// Read file lines
$lines = file($file);
$data = array();
2009-03-06 03:20:46 +00:00
foreach($lines as $line)
2011-11-03 02:25:38 +00:00
$data[] = explode(';',trim($line));
2009-03-06 03:20:46 +00:00
return $data;
}
2011-11-03 02:25:38 +00:00
// Simple table
function BasicTable($header, $data)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Header
2009-03-06 03:20:46 +00:00
foreach($header as $col)
$this->Cell(40,7,$col,1);
$this->Ln();
2011-11-03 02:25:38 +00:00
// Data
2009-03-06 03:20:46 +00:00
foreach($data as $row)
{
foreach($row as $col)
$this->Cell(40,6,$col,1);
$this->Ln();
}
}
2011-11-03 02:25:38 +00:00
// Better table
function ImprovedTable($header, $data)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Column widths
$w = array(40, 35, 40, 45);
// Header
2009-03-06 03:20:46 +00:00
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C');
$this->Ln();
2011-11-03 02:25:38 +00:00
// Data
2009-03-06 03:20:46 +00:00
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR');
$this->Cell($w[1],6,$row[1],'LR');
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
$this->Ln();
}
2011-11-03 02:25:38 +00:00
// Closing line
2009-03-06 03:20:46 +00:00
$this->Cell(array_sum($w),0,'','T');
}
2011-11-03 02:25:38 +00:00
// Colored table
function FancyTable($header, $data)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Colors, line width and bold font
2009-03-06 03:20:46 +00:00
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
2011-11-03 02:25:38 +00:00
// Header
$w = array(40, 35, 40, 45);
2009-03-06 03:20:46 +00:00
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
2011-11-03 02:25:38 +00:00
// Color and font restoration
2009-03-06 03:20:46 +00:00
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
2011-11-03 02:25:38 +00:00
// Data
$fill = false;
2009-03-06 03:20:46 +00:00
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
$this->Ln();
2011-11-03 02:25:38 +00:00
$fill = !$fill;
2009-03-06 03:20:46 +00:00
}
2011-11-03 02:25:38 +00:00
// Closing line
2009-03-06 03:20:46 +00:00
$this->Cell(array_sum($w),0,'','T');
}
}
2011-11-03 02:25:38 +00:00
$pdf = new PDF();
// Column headings
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
// Data loading
$data = $pdf->LoadData('countries.txt');
2009-03-06 03:20:46 +00:00
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->BasicTable($header,$data);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>