FPDF WriteHtml не работает - PullRequest
0 голосов
/ 04 мая 2018

Я пытаюсь сгенерировать PDF в WordPress, используя fpdf, но когда я пытаюсь написать html-контент, он ничего не дает и страница застревает для перезагрузки, когда я пишу функцию WriteHtml.

Вот мой код:

<?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, который я хочу добавить

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

Ответы [ 2 ]

0 голосов
/ 04 мая 2018

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();
?>
0 голосов
/ 04 мая 2018

Fpdf по умолчанию не предусмотрен функцией записи HTML. Вам нужно разработать код.

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