tuto6.php 2.6 KB

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