home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / fpdf / tutorial / tuto6.php < prev    next >
Encoding:
PHP Script  |  2004-12-31  |  2.5 KB  |  123 lines

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