Created
March 12, 2014 14:05
-
-
Save dehart/9507560 to your computer and use it in GitHub Desktop.
HtmlCell FPDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('fpdf.php'); | |
class PDF extends FPDF { | |
//variables of html parser | |
private $B=0; | |
private $I=0; | |
private $U=0; | |
private $HREF=''; | |
private $fontList=array('arial', 'times', 'courier', 'helvetica', 'symbol'); | |
private $issetfont=false; | |
private $issetcolor=false; | |
public function HtmlCell($html) { | |
//HTML parser | |
$html=strip_tags($html,"<b><u><i><img><p><br><strong><em><font><tr><blockquote>"); //remove unsupported tags | |
$html=str_replace("\n",' ',$html); //replace newline with space | |
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //break chain with tags | |
foreach($a as $i=>$e) { | |
if($i%2==0){ | |
$this->Write(5,htmlspecialchars($e,ENT_QUOTES)); | |
} else { | |
//Tag | |
if($e[0]=='/') | |
$this->CloseTag(strtoupper(substr($e,1))); | |
else | |
{ | |
//Extract attributes | |
$a2=explode(' ',$e); | |
$tag=strtoupper(array_shift($a2)); | |
$attr=array(); | |
foreach($a2 as $v) | |
{ | |
if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3)) | |
$attr[strtoupper($a3[1])]=$a3[2]; | |
} | |
$this->OpenTag($tag,$attr); | |
} | |
} | |
} | |
} | |
private function OpenTag($tag, $attr) { | |
//Opening tag | |
switch($tag){ | |
case 'B': | |
case 'I': | |
case 'U': | |
$this->SetStyle($tag,true); | |
break; | |
case 'IMG': | |
if(isset($attr['SRC']) && (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) { | |
if(!isset($attr['WIDTH'])) | |
$attr['WIDTH'] = 0; | |
if(!isset($attr['HEIGHT'])) | |
$attr['HEIGHT'] = 0; | |
$this->Image($attr['SRC'], $this->GetX(), $this->GetY(), $this->px2mm($attr['WIDTH']), $this->px2mm($attr['HEIGHT'])); | |
} | |
break; | |
case 'BLOCKQUOTE': | |
case 'BR': | |
$this->Ln(5); | |
break; | |
case 'P': | |
$this->Ln(10); | |
break; | |
case 'FONT': | |
if (!empty($attr['COLOR'])) { | |
$this->SetTextColor($attr['COLOR']); | |
$this->issetcolor=true; | |
} | |
break; | |
} | |
} | |
private function px2mm($px, $dpi=72){ | |
return $px*25.4/$dpi; | |
} | |
public function SetTextColor($r, $g=null, $b=null) { | |
$color = $r; | |
if(strpos($color, '#')) { | |
$r = hexdec(substr($color, 1, 2)); | |
$g = hexdec(substr($color, 3, 2)); | |
$b = hexdec(substr($color, 5, 2)); | |
} | |
parent::SetTextColor($r,$g,$b); | |
} | |
private function CloseTag($tag) | |
{ | |
if($tag=='B' || $tag=='I' || $tag=='U') | |
$this->SetStyle($tag,false); | |
if($tag=='FONT' && $this->issetcolor) | |
$this->SetTextColor(0); | |
} | |
private function SetStyle($tag, $enable) | |
{ | |
$this->$tag+=($enable ? 1 : -1); | |
$style=''; | |
foreach(array('B','I','U') as $s) { | |
if($this->$s>0) | |
$style.=$s; | |
} | |
$this->SetFont('',$style); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment