tuto4.php 2.2 KB

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