Skip to content
Advertisement

How to use mpdf in codeigniter?

controller: function 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");  
}

view:

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

In this code I have created a view file having name pdf-question.php and now I want to export these question into pdf formate for this I am using mpdf codeigniter where I have load M_pdf.php library and mpdf in third party but Its not working it show me an error and i.e.

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

So, How can I fix this issue ?Please help me.

Thank You

Advertisement

Answer

Make a simple change in your pdf_question function.

$html=$this->load->view('pdf-question', $data,true);
$this->m_pdf->pdf->WriteHTML($html);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement