FPDF WriteHtml不起作用

时间:2018-05-04 13:27:30

标签: php html wordpress fpdf

我试图使用fpdf在wordpress中生成pdf但是当我尝试编写html内容时,它给了我任何东西,并且当我编写WriteHtml函数时,页面卡住了重新加载。

这是我的代码:

<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>

这是我要添加的html

ColorFormat()

2 个答案:

答案 0 :(得分:1)

默认情况下不提供写入HTML功能的Fpdf。你需要开发代码。

<?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();

答案 1 :(得分:0)

FPDF不支持WriteHTML();功能。

使用MPDF: GitHub MPDF,支持HTML。

要使FPDF中的文本居中,您必须编写与此类似的代码:

<?php
    require('fpdf/fpdf.php');

    $pdf=new FPDF();
    $pdf->AddPage();

    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(0, 20, "Hello World", 0, 0, 'C');
    $pdf->Output();
?>