tuto5.php 2.3 KB

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