westdc-zf1/include/fpdf/tutorial/tuto6.php

125 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2009-03-06 03:20:46 +00:00
<?php
require('../fpdf.php');
class PDF extends FPDF
{
var $B;
var $I;
var $U;
var $HREF;
2011-11-03 02:25:38 +00:00
function PDF($orientation='P', $unit='mm', $size='A4')
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Call parent constructor
$this->FPDF($orientation,$unit,$size);
// Initialization
$this->B = 0;
$this->I = 0;
$this->U = 0;
$this->HREF = '';
2009-03-06 03:20:46 +00:00
}
function WriteHTML($html)
{
2011-11-03 02:25:38 +00:00
// HTML parser
$html = str_replace("\n",' ',$html);
$a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
2009-03-06 03:20:46 +00:00
foreach($a as $i=>$e)
{
if($i%2==0)
{
2011-11-03 02:25:38 +00:00
// Text
2009-03-06 03:20:46 +00:00
if($this->HREF)
$this->PutLink($this->HREF,$e);
else
$this->Write(5,$e);
}
else
{
2011-11-03 02:25:38 +00:00
// Tag
if($e[0]=='/')
2009-03-06 03:20:46 +00:00
$this->CloseTag(strtoupper(substr($e,1)));
else
{
2011-11-03 02:25:38 +00:00
// Extract attributes
$a2 = explode(' ',$e);
$tag = strtoupper(array_shift($a2));
$attr = array();
2009-03-06 03:20:46 +00:00
foreach($a2 as $v)
{
if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
2011-11-03 02:25:38 +00:00
$attr[strtoupper($a3[1])] = $a3[2];
2009-03-06 03:20:46 +00:00
}
$this->OpenTag($tag,$attr);
}
}
}
}
2011-11-03 02:25:38 +00:00
function OpenTag($tag, $attr)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Opening tag
if($tag=='B' || $tag=='I' || $tag=='U')
2009-03-06 03:20:46 +00:00
$this->SetStyle($tag,true);
if($tag=='A')
2011-11-03 02:25:38 +00:00
$this->HREF = $attr['HREF'];
2009-03-06 03:20:46 +00:00
if($tag=='BR')
$this->Ln(5);
}
function CloseTag($tag)
{
2011-11-03 02:25:38 +00:00
// Closing tag
if($tag=='B' || $tag=='I' || $tag=='U')
2009-03-06 03:20:46 +00:00
$this->SetStyle($tag,false);
if($tag=='A')
2011-11-03 02:25:38 +00:00
$this->HREF = '';
2009-03-06 03:20:46 +00:00
}
2011-11-03 02:25:38 +00:00
function SetStyle($tag, $enable)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Modify style and select corresponding font
$this->$tag += ($enable ? 1 : -1);
$style = '';
foreach(array('B', 'I', 'U') as $s)
{
2009-03-06 03:20:46 +00:00
if($this->$s>0)
2011-11-03 02:25:38 +00:00
$style .= $s;
}
2009-03-06 03:20:46 +00:00
$this->SetFont('',$style);
}
2011-11-03 02:25:38 +00:00
function PutLink($URL, $txt)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Put a hyperlink
2009-03-06 03:20:46 +00:00
$this->SetTextColor(0,0,255);
$this->SetStyle('U',true);
$this->Write(5,$txt,$URL);
$this->SetStyle('U',false);
$this->SetTextColor(0);
}
}
2011-11-03 02:25:38 +00:00
$html = 'You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
2009-03-06 03:20:46 +00:00
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
text, such as <a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';
2011-11-03 02:25:38 +00:00
$pdf = new PDF();
// First page
2009-03-06 03:20:46 +00:00
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
2011-11-03 02:25:38 +00:00
$pdf->Write(5,"To find out what's new in this tutorial, click ");
2009-03-06 03:20:46 +00:00
$pdf->SetFont('','U');
2011-11-03 02:25:38 +00:00
$link = $pdf->AddLink();
2009-03-06 03:20:46 +00:00
$pdf->Write(5,'here',$link);
$pdf->SetFont('');
2011-11-03 02:25:38 +00:00
// Second page
2009-03-06 03:20:46 +00:00
$pdf->AddPage();
$pdf->SetLink($link);
$pdf->Image('logo.png',10,12,30,0,'','http://www.fpdf.org');
$pdf->SetLeftMargin(45);
$pdf->SetFontSize(14);
$pdf->WriteHTML($html);
$pdf->Output();
?>