tuto5.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. require('../fpdf.php');
  3. class PDF extends FPDF
  4. {
  5. //Chargement des données
  6. function LoadData($file)
  7. {
  8. //Lecture des lignes du fichier
  9. $lines=file($file);
  10. $data=array();
  11. foreach($lines as $line)
  12. $data[]=explode(';',chop($line));
  13. return $data;
  14. }
  15. //Tableau simple
  16. function BasicTable($header,$data)
  17. {
  18. //En-tête
  19. foreach($header as $col)
  20. $this->Cell(40,7,$col,1);
  21. $this->Ln();
  22. //Données
  23. foreach($data as $row)
  24. {
  25. foreach($row as $col)
  26. $this->Cell(40,6,$col,1);
  27. $this->Ln();
  28. }
  29. }
  30. //Tableau amélioré
  31. function ImprovedTable($header,$data)
  32. {
  33. //Largeurs des colonnes
  34. $w=array(40,35,45,40);
  35. //En-tête
  36. for($i=0;$i<count($header);$i++)
  37. $this->Cell($w[$i],7,$header[$i],1,0,'C');
  38. $this->Ln();
  39. //Données
  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],0,',',' '),'LR',0,'R');
  45. $this->Cell($w[3],6,number_format($row[3],0,',',' '),'LR',0,'R');
  46. $this->Ln();
  47. }
  48. //Trait de terminaison
  49. $this->Cell(array_sum($w),0,'','T');
  50. }
  51. //Tableau coloré
  52. function FancyTable($header,$data)
  53. {
  54. //Couleurs, épaisseur du trait et police grasse
  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. //En-tête
  61. $w=array(40,35,45,40);
  62. for($i=0;$i<count($header);$i++)
  63. $this->Cell($w[$i],7,$header[$i],1,0,'C',1);
  64. $this->Ln();
  65. //Restauration des couleurs et de la police
  66. $this->SetFillColor(224,235,255);
  67. $this->SetTextColor(0);
  68. $this->SetFont('');
  69. //Données
  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],0,',',' '),'LR',0,'R',$fill);
  76. $this->Cell($w[3],6,number_format($row[3],0,',',' '),'LR',0,'R',$fill);
  77. $this->Ln();
  78. $fill=!$fill;
  79. }
  80. $this->Cell(array_sum($w),0,'','T');
  81. }
  82. }
  83. $pdf=new PDF();
  84. //Titres des colonnes
  85. $header=array('Pays','Capitale','Superficie (km²)','Pop. (milliers)');
  86. //Chargement des données
  87. $data=$pdf->LoadData('pays.txt');
  88. $pdf->SetFont('Arial','',14);
  89. $pdf->AddPage();
  90. $pdf->BasicTable($header,$data);
  91. $pdf->AddPage();
  92. $pdf->ImprovedTable($header,$data);
  93. $pdf->AddPage();
  94. $pdf->FancyTable($header,$data);
  95. $pdf->Output();
  96. ?>