tuto5.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. require('../fpdf.php');
  3. class PDF extends FPDF
  4. {
  5. // Cargar los datos
  6. function LoadData($file)
  7. {
  8. // Leer las líneas del fichero
  9. $lines = file($file);
  10. $data = array();
  11. foreach($lines as $line)
  12. $data[] = explode(';',trim($line));
  13. return $data;
  14. }
  15. // Tabla simple
  16. function BasicTable($header, $data)
  17. {
  18. // Cabecera
  19. foreach($header as $col)
  20. $this->Cell(40,7,$col,1);
  21. $this->Ln();
  22. // Datos
  23. foreach($data as $row)
  24. {
  25. foreach($row as $col)
  26. $this->Cell(40,6,$col,1);
  27. $this->Ln();
  28. }
  29. }
  30. // Una tabla más completa
  31. function ImprovedTable($header, $data)
  32. {
  33. // Anchuras de las columnas
  34. $w = array(40, 35, 45, 40);
  35. // Cabeceras
  36. for($i=0;$i<count($header);$i++)
  37. $this->Cell($w[$i],7,$header[$i],1,0,'C');
  38. $this->Ln();
  39. // Datos
  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. // Línea de cierre
  49. $this->Cell(array_sum($w),0,'','T');
  50. }
  51. // Tabla coloreada
  52. function FancyTable($header, $data)
  53. {
  54. // Colores, ancho de línea y fuente en negrita
  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. // Cabecera
  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',true);
  64. $this->Ln();
  65. // Restauración de colores y fuentes
  66. $this->SetFillColor(224,235,255);
  67. $this->SetTextColor(0);
  68. $this->SetFont('');
  69. // Datos
  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. // Línea de cierre
  81. $this->Cell(array_sum($w),0,'','T');
  82. }
  83. }
  84. $pdf = new PDF();
  85. // Títulos de las columnas
  86. $header = array('País', 'Capital', 'Superficie (km2)', 'Pobl. (en miles)');
  87. // Carga de datos
  88. $data = $pdf->LoadData('paises.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. ?>