在codeigniter中将HTML转换为pdf

时间:2019-02-12 09:55:20

标签: javascript php codeigniter

控制器:函数income_reports()

 public function income_reports(){

    if(isset($_POST["search"])) {
    $from=$this->input->post('from_date');
    $to=$this->input->post('to_date');
    $this->load->model("Home_model");
    $data['view_report']=$this->Home_model->income_report($from,$to);
    $data['cat_result']=$this->Home_model->add_cat();
    $pdfFilePath = "output_pdf_name.pdf";
    $this->load->library('m_pdf');
    $html = $this->load->view('income_reports',$data,true);
    $this->m_pdf->pdf->WriteHTML($html);
    $this->m_pdf->pdf->Output($pdfFilePath, "D");

  }

 }

视图:

 <!DOCTYPE html>
 <html>
  <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title> Income |Report</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">

   </head>
    <div class="wrapper" >
     <!-- Main content -->
     <section class="invoice">

        <!-- Table row -->
        <div class="row">
           <div class="col-xs-12 table-responsive">
              <table class="table-border" cellspacing="50">
                 <thead>
                    <tr>
                      <th>#</th>
                      <th>Date</th>
                      <th>Category</th>
                      <th>Description</th>
                      <th>Income Amount</th>
                    </tr>
                 </thead>
                 <tbody>
                        <?php
                   $total_sum=0;
                                  if($view_report->num_rows() > 0)
                         {
                              $i=1;
                               foreach($view_report->result() as $row)
                                {
                           ?>
                    <tr>
                      <td><?php echo $i++;?></td>
                       <td><?php echo $row->date;?></td>
                        <td><?php echo $row->cat_name;?></td>
                        <td><?php echo $row->description;?></td>
                        <td><?php echo $row->inc_amount;?></td>
                    </tr>
                    <?php
                     $total_sum+=$row->inc_amount;
                             }

                          }
                          else
                           {

                           ?>

                                   <tr><td>Not Found</td></tr>

                           <?php
                          }
                           ?>
                 </tbody>
              </table>
              <div>

            <h4 style="color: green; padding-left: 500px;">Total : ₹<?php echo $total_sum ?> !</h4>
          </div>
           </div>
           <!-- /.col -->
        </div>

        <!-- /.row -->
     </section>
     <!-- /.content -->
     </div>
     <!-- ./wrapper -->
    </body>
   </html>

在这段代码中,我创建了一个名称为income_reports.php的视图文件,现在我想将这些问题导出为pdf格式,为此我使用的是mpdf CodeIgniter,其中我已加载了{ {1}}第三方库和pdf,但无法正常工作,它显示了一个错误。

那么,如何解决此问题?请帮助我。

错误:1)

  

遇到PHP错误        严重程度:注意        消息:未定义的属性:m_pdf :: $ pdf        文件名:controllers / Home.php        行号:156

2)

  

遇到未捕获的异常          类型:错误          消息:在null上调用成员函数WriteHTML()         文件名:C:\ xampp \ htdocs \ income_expence_manage \ application \ controllers \ Home.php         行号:156

谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

1.application / libraries / M_pdf.php:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class M_pdf {

    function m_pdf()
    {
        $CI = & get_instance();
        log_message('Debug', 'mPDF class is loaded.');
    }

    function load($param=NULL)
    {
        include_once APPPATH.'/third_party/mpdf/mpdf.php';

        if ($params == NULL)
        {
            $param = '"en-GB-x","A4","","",10,10,10,10,6,3';                
        }

        //return new mPDF($param);
        return new mPDF();
    }
}
?>

2。将mpdf文件夹复制到application / third_party /

3.Controller:

$html = $this->load->view('filename',$data,true);
$pdfFilePath = 'path/filename';
$pdf = $this->m_pdf->load();
$pdf->AddPage('','','','','',5,5,5,5,10,10); //set margin
$pdf->WriteHTML($html,2);
ob_clean();
$pdf->Output($pdfFilePath, "F");