首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FPDF WriteHtml不工作

FPDF WriteHtml不工作
EN

Stack Overflow用户
提问于 2018-05-04 21:27:30
回答 2查看 14.4K关注 0票数 2

我试着用fpdf在wordpress中生成一个pdf,但是当我尝试写一个html内容时,它什么也没给我,而且每当我写WriteHtml函数的时候,页面就会被重新加载。

下面是我的代码:

代码语言:javascript
复制
<?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');
?>

下面是我想要添加的html

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-04 21:48:04

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

代码语言:javascript
复制
<?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();
票数 2
EN

Stack Overflow用户

发布于 2018-05-04 21:48:39

FPDF不支持WriteHTML();函数。

使用MPDF:GitHub MPDF,它支持HTML.

要在FPDF中居中显示文本,您必须编写类似以下内容的代码:

代码语言:javascript
复制
<?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();
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50176102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档