tuto6.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. require('../fpdf.php');
  3. class PDF extends FPDF
  4. {
  5. protected $B = 0;
  6. protected $I = 0;
  7. protected $U = 0;
  8. protected $HREF = '';
  9. function WriteHTML($html)
  10. {
  11. // HTML parser
  12. $html = str_replace("\n",' ',$html);
  13. $a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
  14. foreach($a as $i=>$e)
  15. {
  16. if($i%2==0)
  17. {
  18. // Text
  19. if($this->HREF)
  20. $this->PutLink($this->HREF,$e);
  21. else
  22. $this->Write(5,$e);
  23. }
  24. else
  25. {
  26. // Tag
  27. if($e[0]=='/')
  28. $this->CloseTag(strtoupper(substr($e,1)));
  29. else
  30. {
  31. // Extract attributes
  32. $a2 = explode(' ',$e);
  33. $tag = strtoupper(array_shift($a2));
  34. $attr = array();
  35. foreach($a2 as $v)
  36. {
  37. if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
  38. $attr[strtoupper($a3[1])] = $a3[2];
  39. }
  40. $this->OpenTag($tag,$attr);
  41. }
  42. }
  43. }
  44. }
  45. function OpenTag($tag, $attr)
  46. {
  47. // Opening tag
  48. if($tag=='B' || $tag=='I' || $tag=='U')
  49. $this->SetStyle($tag,true);
  50. if($tag=='A')
  51. $this->HREF = $attr['HREF'];
  52. if($tag=='BR')
  53. $this->Ln(5);
  54. }
  55. function CloseTag($tag)
  56. {
  57. // Closing tag
  58. if($tag=='B' || $tag=='I' || $tag=='U')
  59. $this->SetStyle($tag,false);
  60. if($tag=='A')
  61. $this->HREF = '';
  62. }
  63. function SetStyle($tag, $enable)
  64. {
  65. // Modify style and select corresponding font
  66. $this->$tag += ($enable ? 1 : -1);
  67. $style = '';
  68. foreach(array('B', 'I', 'U') as $s)
  69. {
  70. if($this->$s>0)
  71. $style .= $s;
  72. }
  73. $this->SetFont('',$style);
  74. }
  75. function PutLink($URL, $txt)
  76. {
  77. // Put a hyperlink
  78. $this->SetTextColor(0,0,255);
  79. $this->SetStyle('U',true);
  80. $this->Write(5,$txt,$URL);
  81. $this->SetStyle('U',false);
  82. $this->SetTextColor(0);
  83. }
  84. }
  85. $html = 'You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
  86. <u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
  87. text, such as <a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';
  88. $pdf = new PDF();
  89. // First page
  90. $pdf->AddPage();
  91. $pdf->SetFont('Arial','',20);
  92. $pdf->Write(5,"To find out what's new in this tutorial, click ");
  93. $pdf->SetFont('','U');
  94. $link = $pdf->AddLink();
  95. $pdf->Write(5,'here',$link);
  96. $pdf->SetFont('');
  97. // Second page
  98. $pdf->AddPage();
  99. $pdf->SetLink($link);
  100. $pdf->Image('logo.png',10,12,30,0,'','http://www.fpdf.org');
  101. $pdf->SetLeftMargin(45);
  102. $pdf->SetFontSize(14);
  103. $pdf->WriteHTML($html);
  104. $pdf->Output();
  105. ?>