tuto4.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. require('../fpdf.php');
  3. class PDF extends FPDF
  4. {
  5. //Current column
  6. var $col=0;
  7. //Ordinate of column start
  8. var $y0;
  9. function Header()
  10. {
  11. //Page header
  12. global $title;
  13. $this->SetFont('Arial','B',15);
  14. $w=$this->GetStringWidth($title)+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,$title,1,1,'C',1);
  21. $this->Ln(10);
  22. //Save ordinate
  23. $this->y0=$this->GetY();
  24. }
  25. function Footer()
  26. {
  27. //Page footer
  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. //Set position at a given column
  36. $this->col=$col;
  37. $x=10+$col*65;
  38. $this->SetLeftMargin($x);
  39. $this->SetX($x);
  40. }
  41. function AcceptPageBreak()
  42. {
  43. //Method accepting or not automatic page break
  44. if($this->col<2)
  45. {
  46. //Go to next column
  47. $this->SetCol($this->col+1);
  48. //Set ordinate to top
  49. $this->SetY($this->y0);
  50. //Keep on page
  51. return false;
  52. }
  53. else
  54. {
  55. //Go back to first column
  56. $this->SetCol(0);
  57. //Page break
  58. return true;
  59. }
  60. }
  61. function ChapterTitle($num,$label)
  62. {
  63. //Title
  64. $this->SetFont('Arial','',12);
  65. $this->SetFillColor(200,220,255);
  66. $this->Cell(0,6,"Chapter $num : $label",0,1,'L',1);
  67. $this->Ln(4);
  68. //Save ordinate
  69. $this->y0=$this->GetY();
  70. }
  71. function ChapterBody($fichier)
  72. {
  73. //Read text file
  74. $f=fopen($fichier,'r');
  75. $txt=fread($f,filesize($fichier));
  76. fclose($f);
  77. //Font
  78. $this->SetFont('Times','',12);
  79. //Output text in a 6 cm width column
  80. $this->MultiCell(60,5,$txt);
  81. $this->Ln();
  82. //Mention
  83. $this->SetFont('','I');
  84. $this->Cell(0,5,'(end of excerpt)');
  85. //Go back to first column
  86. $this->SetCol(0);
  87. }
  88. function PrintChapter($num,$title,$file)
  89. {
  90. //Add chapter
  91. $this->AddPage();
  92. $this->ChapterTitle($num,$title);
  93. $this->ChapterBody($file);
  94. }
  95. }
  96. $pdf=new PDF();
  97. $title='20000 Leagues Under the Seas';
  98. $pdf->SetTitle($title);
  99. $pdf->SetAuthor('Jules Verne');
  100. $pdf->PrintChapter(1,'A RUNAWAY REEF','20k_c1.txt');
  101. $pdf->PrintChapter(2,'THE PROS AND CONS','20k_c2.txt');
  102. $pdf->Output();
  103. ?>