I'm trying to generate a pdf in wordpress using fpdf but when I try to write a html content it gives me nothing and page stuck to reload when ever I write the WriteHtml function.
Here is my code:
<?php
require('fpdf/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr>');
$pdf->Output('E:\xampp\spy2html.pdf','F');
?>
here is my html which i want to add
<div id="finalOutput">
<h2 class="llms-quiz-results-title">'.printf( __( "Attempt #%d Results", "lifterlms" ), $attempt->get( "attempt" ) ); .'</h2>
<aside class="llms-quiz-results-aside">
'. echo llms_get_donut( $attempt->get( "grade" ), $attempt->l10n( "passed" ), "default", $donut_class );.'
</aside>
<section class="llms-quiz-results-main">
<ul class="llms-quiz-meta-info">
<li class="llms-quiz-meta-item">'. printf( __( "Status: %s", "lifterlms" ), $attempt->l10n( "passed" ) );.'</li>
<li class="llms-quiz-meta-item"><?php printf( __( "Grade: %s", "lifterlms" ), round( $attempt->get( "grade" ), 2 ) . "%" ); ?></li>
<li class="llms-quiz-meta-item"><?php printf( __( "Correct Answers: %1$d / %2$d", "lifterlms" ), $attempt->get_count( "correct_answers" ), $attempt->get_count( "questions" ) ); ?></li>
<li class="llms-quiz-meta-item"><?php printf( __( "Completed: %s", "lifterlms" ), $attempt->get_date( "start" ) ); ?></li>
<li class="llms-quiz-meta-item"><?php printf( __( "Total time: %s", "lifterlms" ), $attempt->get_time() ); ?></li>
<?php if ( $quiz->show_quiz_results() ) : ?>
<li class="llms-quiz-meta-item"><a class="view-summary" href="#"><?php _e( "View Summary", "lifterlms" ); ?></a></li>
<?php endif; ?>
</ul>
</section>
</div>
Fpdf by default is not provided write HTML function. You need to develop code.
<?php
require('fpdf/fpdf.php');
class PDF extends FPDF
{
protected $B = 0;
protected $I = 0;
protected $U = 0;
protected $HREF = '';
function WriteHTML($html)
{
// HTML parser
$html = str_replace("\n",' ',$html);
$a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
foreach($a as $i=>$e)
{
if($i%2==0)
{
// Text
if($this->HREF)
$this->PutLink($this->HREF,$e);
else
$this->Write(5,$e);
}
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);
}
}
}
}
function OpenTag($tag, $attr)
{
// Opening tag
if($tag=='B' || $tag=='I' || $tag=='U')
$this->SetStyle($tag,true);
if($tag=='A')
$this->HREF = $attr['HREF'];
if($tag=='BR')
$this->Ln(5);
}
function CloseTag($tag)
{
// Closing tag
if($tag=='B' || $tag=='I' || $tag=='U')
$this->SetStyle($tag,false);
if($tag=='A')
$this->HREF = '';
}
function SetStyle($tag, $enable)
{
// Modify style and select corresponding font
$this->$tag += ($enable ? 1 : -1);
$style = '';
foreach(array('B', 'I', 'U') as $s)
{
if($this->$s>0)
$style .= $s;
}
$this->SetFont('',$style);
}
function PutLink($URL, $txt)
{
// Put a hyperlink
$this->SetTextColor(0,0,255);
$this->SetStyle('U',true);
$this->Write(5,$txt,$URL);
$this->SetStyle('U',false);
$this->SetTextColor(0);
}
}
$pdf = new PDF();
// First page
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr>');
$pdf->Output();