tuto6.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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',$format='A4')
  10. {
  11. //Call parent constructor
  12. $this->FPDF($orientation,$unit,$format);
  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. if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
  47. $attr[strtoupper($a3[1])]=$a3[2];
  48. $this->OpenTag($tag,$attr);
  49. }
  50. }
  51. }
  52. }
  53. function OpenTag($tag,$attr)
  54. {
  55. //Opening tag
  56. if($tag=='B' or $tag=='I' or $tag=='U')
  57. $this->SetStyle($tag,true);
  58. if($tag=='A')
  59. $this->HREF=$attr['HREF'];
  60. if($tag=='BR')
  61. $this->Ln(5);
  62. }
  63. function CloseTag($tag)
  64. {
  65. //Closing tag
  66. if($tag=='B' or $tag=='I' or $tag=='U')
  67. $this->SetStyle($tag,false);
  68. if($tag=='A')
  69. $this->HREF='';
  70. }
  71. function SetStyle($tag,$enable)
  72. {
  73. //Modify style and select corresponding font
  74. $this->$tag+=($enable ? 1 : -1);
  75. $style='';
  76. foreach(array('B','I','U') as $s)
  77. if($this->$s>0)
  78. $style.=$s;
  79. $this->SetFont('',$style);
  80. }
  81. function PutLink($URL,$txt)
  82. {
  83. //Put a hyperlink
  84. $this->SetTextColor(0,0,255);
  85. $this->SetStyle('U',true);
  86. $this->Write(5,$txt,$URL);
  87. $this->SetStyle('U',false);
  88. $this->SetTextColor(0);
  89. }
  90. }
  91. $html='You can now easily print text mixing different
  92. styles : <B>bold</B>, <I>italic</I>, <U>underlined</U>, or
  93. <B><I><U>all at once</U></I></B>!<BR>You can also insert links
  94. on text, such as <A HREF="http://www.fpdf.org">www.fpdf.org</A>,
  95. or on an image: click on the logo.';
  96. $pdf=new PDF();
  97. //First page
  98. $pdf->AddPage();
  99. $pdf->SetFont('Arial','',20);
  100. $pdf->Write(5,'To find out what\'s new in this tutorial, click ');
  101. $pdf->SetFont('','U');
  102. $link=$pdf->AddLink();
  103. $pdf->Write(5,'here',$link);
  104. $pdf->SetFont('');
  105. //Second page
  106. $pdf->AddPage();
  107. $pdf->SetLink($link);
  108. $pdf->Image('logo.png',10,10,30,0,'','http://www.fpdf.org');
  109. $pdf->SetLeftMargin(45);
  110. $pdf->SetFontSize(14);
  111. $pdf->WriteHTML($html);
  112. $pdf->Output();
  113. ?>