Last active
April 19, 2025 05:18
-
-
Save estefanionsantos/b017f99efba17210bb5998cb73abd115 to your computer and use it in GitHub Desktop.
fpdf-tuto6-1
This file contains hidden or 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 | |
use Rubricate\Fpdf\FileFpdf; | |
class PDF extends FileFpdf | |
{ | |
protected $B = 0; | |
protected $I = 0; | |
protected $U = 0; | |
protected $HREF = ''; | |
public function writeHTML($html) | |
{ | |
$html = str_replace("\n", ' ', $html); | |
$a = preg_split('/<(.*)>/U', $html,-1, PREG_SPLIT_DELIM_CAPTURE); | |
foreach($a as $i=>$e) { | |
if(($i%2) == 0) { | |
if($this->HREF){ | |
$this->putLink($this->HREF,$e); | |
} else{ | |
$this->write(5,$e); | |
} | |
} else { | |
if($e[0] == '/'){ | |
$this->closeTag(strtoupper(substr($e,1))); | |
} else { | |
$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); | |
} | |
} | |
} | |
} | |
public function openTag($tag, $attr) | |
{ | |
if($tag =='B' || $tag == 'I' || $tag == 'U'){ | |
$this->setStyle($tag,true); | |
} | |
if($tag == 'A'){ | |
$this->HREF = $attr['HREF']; | |
} | |
if($tag == 'BR'){ | |
$this->ln(5); | |
} | |
} | |
public function closeTag($tag) | |
{ | |
if($tag=='B' || $tag=='I' || $tag=='U'){ | |
$this->SetStyle($tag,false); | |
} | |
if($tag=='A'){ | |
$this->HREF = ''; | |
} | |
} | |
public function setStyle($tag, $enable) | |
{ | |
$this->$tag += ($enable ? 1 : -1); | |
$style = ''; | |
foreach(['B', 'I', 'U'] as $s){ | |
if($this->$s > 0){ | |
$style .= $s; | |
} | |
} | |
$this->setFont('', $style); | |
} | |
public function putLink($URL, $txt) | |
{ | |
$this->setTextColor(0,0,255); | |
$this->setStyle('U',true); | |
$this->write(5,$txt,$URL); | |
$this->setStyle('U',false); | |
$this->setTextColor(0); | |
} | |
} | |
$html = 'You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>, | |
<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.'; | |
$pdf = new PDF(); | |
$pdf->addPage(); | |
$pdf->setFont('Arial', '', 20); | |
$pdf->write(5,"To find out what's new in this tutorial, click "); | |
$pdf->setFont('', 'U'); | |
$link = $pdf->addLink(); | |
$pdf->write(5, 'here', $link); | |
$pdf->setFont(''); | |
$pdf->addPage(); | |
$pdf->setLink($link); | |
$pdf->image('logo.png', 10, 12, 30, 0, '', 'https://rubricate.github.io/components/fpdf/'); | |
$pdf->setLeftMargin(45); | |
$pdf->setFontSize(14); | |
$pdf->writeHTML($html); | |
$pdf->output(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment