tuto6.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. //Appel au constructeur parent
  12. $this->FPDF($orientation,$unit,$format);
  13. //Initialisation
  14. $this->B=0;
  15. $this->I=0;
  16. $this->U=0;
  17. $this->HREF='';
  18. }
  19. function WriteHTML($html)
  20. {
  21. //Parseur HTML
  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. //Texte
  29. if($this->HREF)
  30. $this->PutLink($this->HREF,$e);
  31. else
  32. $this->Write(5,$e);
  33. }
  34. else
  35. {
  36. //Balise
  37. if($e[0]=='/')
  38. $this->CloseTag(strtoupper(substr($e,1)));
  39. else
  40. {
  41. //Extraction des attributs
  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. //Balise ouvrante
  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. //Balise fermante
  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. //Modifie le style et sélectionne la police correspondante
  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. //Place un hyperlien
  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='Vous pouvez maintenant imprimer facilement du texte mélangeant différents styles : <b>gras</b>,
  96. <i>italique</i>, <u>souligné</u>, ou <b><i><u>tous à la fois</u></i></b> !<br><br>Vous pouvez aussi
  97. insérer des liens sous forme textuelle, comme <a href="http://www.fpdf.org">www.fpdf.org</a>, ou bien
  98. sous forme d\'image : cliquez sur le logo.';
  99. $pdf=new PDF();
  100. //Première page
  101. $pdf->AddPage();
  102. $pdf->SetFont('Arial','',20);
  103. $pdf->Write(5,'Pour découvrir les nouveautés de ce tutoriel, cliquez ');
  104. $pdf->SetFont('','U');
  105. $link=$pdf->AddLink();
  106. $pdf->Write(5,'ici',$link);
  107. $pdf->SetFont('');
  108. //Seconde page
  109. $pdf->AddPage();
  110. $pdf->SetLink($link);
  111. $pdf->Image('logo.png',10,12,30,0,'','http://www.fpdf.org');
  112. $pdf->SetLeftMargin(45);
  113. $pdf->SetFontSize(14);
  114. $pdf->WriteHTML($html);
  115. $pdf->Output();
  116. ?>