tuto4.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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',true);
  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',true);
  67. $this->Ln(4);
  68. // Save ordinate
  69. $this->y0 = $this->GetY();
  70. }
  71. function ChapterBody($file)
  72. {
  73. // Read text file
  74. $txt = file_get_contents($file);
  75. // Font
  76. $this->SetFont('Times','',12);
  77. // Output text in a 6 cm width column
  78. $this->MultiCell(60,5,$txt);
  79. $this->Ln();
  80. // Mention
  81. $this->SetFont('','I');
  82. $this->Cell(0,5,'(end of excerpt)');
  83. // Go back to first column
  84. $this->SetCol(0);
  85. }
  86. function PrintChapter($num, $title, $file)
  87. {
  88. // Add chapter
  89. $this->AddPage();
  90. $this->ChapterTitle($num,$title);
  91. $this->ChapterBody($file);
  92. }
  93. }
  94. $pdf = new PDF();
  95. $title = '20000 Leagues Under the Seas';
  96. $pdf->SetTitle($title);
  97. $pdf->SetAuthor('Jules Verne');
  98. $pdf->PrintChapter(1,'A RUNAWAY REEF','20k_c1.txt');
  99. $pdf->PrintChapter(2,'THE PROS AND CONS','20k_c2.txt');
  100. $pdf->Output();
  101. ?>