tuto4.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. require('../fpdf.php');
  3. class PDF extends FPDF
  4. {
  5. //Colonne courante
  6. var $col=0;
  7. //Ordonnée du début des colonnes
  8. var $y0;
  9. function Header()
  10. {
  11. //En-tête
  12. global $titre;
  13. $this->SetFont('Arial','B',15);
  14. $w=$this->GetStringWidth($titre)+6;
  15. $this->SetX((210-$w)/2);
  16. $this->SetDrawColor(0,80,180);
  17. $this->SetFillColor(230,230,0);
  18. $this->SetTextColor(220,50,50);
  19. $this->SetLineWidth(1);
  20. $this->Cell($w,9,$titre,1,1,'C',true);
  21. $this->Ln(10);
  22. //Sauvegarde de l'ordonnée
  23. $this->y0=$this->GetY();
  24. }
  25. function Footer()
  26. {
  27. //Pied de page
  28. $this->SetY(-15);
  29. $this->SetFont('Arial','I',8);
  30. $this->SetTextColor(128);
  31. $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
  32. }
  33. function SetCol($col)
  34. {
  35. //Positionnement sur une colonne
  36. $this->col=$col;
  37. $x=10+$col*65;
  38. $this->SetLeftMargin($x);
  39. $this->SetX($x);
  40. }
  41. function AcceptPageBreak()
  42. {
  43. //Méthode autorisant ou non le saut de page automatique
  44. if($this->col<2)
  45. {
  46. //Passage à la colonne suivante
  47. $this->SetCol($this->col+1);
  48. //Ordonnée en haut
  49. $this->SetY($this->y0);
  50. //On reste sur la page
  51. return false;
  52. }
  53. else
  54. {
  55. //Retour en première colonne
  56. $this->SetCol(0);
  57. //Saut de page
  58. return true;
  59. }
  60. }
  61. function TitreChapitre($num,$lib)
  62. {
  63. //Titre
  64. $this->SetFont('Arial','',12);
  65. $this->SetFillColor(200,220,255);
  66. $this->Cell(0,6,"Chapitre $num : $lib",0,1,'L',true);
  67. $this->Ln(4);
  68. //Sauvegarde de l'ordonnée
  69. $this->y0=$this->GetY();
  70. }
  71. function CorpsChapitre($fichier)
  72. {
  73. //Lecture du fichier texte
  74. $f=fopen($fichier,'r');
  75. $txt=fread($f,filesize($fichier));
  76. fclose($f);
  77. //Police
  78. $this->SetFont('Times','',12);
  79. //Sortie du texte sur 6 cm de largeur
  80. $this->MultiCell(60,5,$txt);
  81. $this->Ln();
  82. //Mention
  83. $this->SetFont('','I');
  84. $this->Cell(0,5,'(fin de l\'extrait)');
  85. //Retour en première colonne
  86. $this->SetCol(0);
  87. }
  88. function AjouterChapitre($num,$titre,$fichier)
  89. {
  90. //Ajout du chapitre
  91. $this->AddPage();
  92. $this->TitreChapitre($num,$titre);
  93. $this->CorpsChapitre($fichier);
  94. }
  95. }
  96. $pdf=new PDF();
  97. $titre='Vingt mille lieues sous les mers';
  98. $pdf->SetTitle($titre);
  99. $pdf->SetAuthor('Jules Verne');
  100. $pdf->AjouterChapitre(1,'UN ÉCUEIL FUYANT','20k_c1.txt');
  101. $pdf->AjouterChapitre(2,'LE POUR ET LE CONTRE','20k_c2.txt');
  102. $pdf->Output();
  103. ?>