php - 如何在codeigniter中使用mpdf?

标签 php codeigniter mpdf

Controller :函数 pdf_question()

public function pdf_question()
{
    $data = [];
    $pdfFilePath = "output_pdf_name.pdf";
    $this->load->library('m_pdf');
    $this->m_pdf->pdf->WriteHTML($this->load->view('pdf-question'));
    $this->m_pdf->pdf->Output($pdfFilePath, "D");  
}

查看:

<html>
<head>
</head>
<body>
    <div class="container">
        <div class="col-lg-12" style="background:#38558a;">
            <h3 style="color:#ff7900;"><b>Quiz Questions</b></h3>
            <hr>
        </div>
        <div class="row">
            <div class="col-md-12">
            <br/> 
                <div class="login-panel panel panel-default">
                    <div class="panel-body"> 
                        <a href="javascript:void(0)" class="btn btn-danger" style="float:right;" id="pdf">PDF</a>
                        <div class="col-lg-12" id="target">
                            <?php
                                $this->db->select('*');
                                $this->db->from('qbank');
                                $query = $this->db->get();
                                $result = $query->result_array();
                                foreach($result as $row)
                                {
                                    $qid = $row['qid'];
                                    $question = $row['question'];
                            ?>
                                    <div class="col-div-container">
                                        <table class="bix-tbl-container" width="100%" cellspacing="0" cellpadding="0" border="0">
                                            <tbody>
                                                <tr>
                                                    <td rowspan="2" valign="top" align="left"><?php echo $qid; ?>.&nbsp;</td>
                                                    <td valign="top"><p><?php echo $question; ?></p></td>
                                                </tr>

                                                <tr>
                                                    <td valign="top">
                                                        <table width="100%" cellspacing="0" cellpadding="0" border="0" style="line-height: 1.7;">
                                                            <tbody>
                                                                <?php 
                                                                    $this->db->select('*');
                                                                    $this->db->from('options');
                                                                    $where = "qid = '$qid'";
                                                                    $this->db->where($where);
                                                                    $sql = $this->db->get();
                                                                    //echo $this->db->last_query();
                                                                    $res = $sql->result_array();
                                                                    foreach($res as $rows)
                                                                    {
                                                                        $option = $rows['q_option']; 
                                                                ?>
                                                                <tr>
                                                                    <ol style="list-style-type: upper-alpha;">
                                                                        <td width="5%">
                                                                            <li><a href="javascript:void(0)"><input type="radio" name="radio_btn"></a></li>
                                                                        </td>
                                                                    </ol>
                                                                    <td width="99%"><?php echo $option; ?></td>
                                                                </tr>
                                                                <?php
                                                                    }
                                                                ?>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                    <hr/>
                            <?php
                                }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

在这段代码中,我创建了一个名为 pdf-question.php 的 View 文件,现在我想将这些问题导出为 pdf 格式,为此我使用 mpdf codeigniter,在第三方中加载 M_pdf.php 库和 mpdf但它不起作用,它向我显示一个错误,即

HTML contains invalid UTF-8 character(s)
mPDF error:

那么,我该如何解决这个问题?请帮助我。

谢谢

最佳答案

对 pdf_question 函数进行简单的更改。

$html=$this->load->view('pdf-question', $data,true);
$this->m_pdf->pdf->WriteHTML($html);

关于php - 如何在codeigniter中使用mpdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47363480/

相关文章:

php - 在 ob_end_flush() 之前和标题之前需要样式表(位置 : ), 如何?

php - bcdiv 使用带有科学记数法的非常小的 float 导致 "Division by zero"错误

codeigniter - mpdf 错误 - preg_replace() : The/e modifier is deprecated, 改用 preg_replace_callback

php - mysql 无法与 php 一起使用

php - 缓慢的 MySQL 性能和 sleep 查询

php - CodeIgniter MySQL 对不同行进行计数

php - 为什么 Code Igniter 给我一个白页?

PHP mPDF 二维码 : How to control the auto-generated QR-Code-border?

html - img src 使用 codeigniter 使 pdf 生成速度太慢

javascript - 从 MySQL 导出到 PHP 中的对象数组,然后导出到 JavaScript