【问题标题】:mpdf error - preg_replace(): The /e modifier is deprecated, use preg_replace_callback insteadmpdf 错误 - preg_replace(): /e 修饰符已弃用,请改用 preg_replace_callback
【发布时间】:2015-04-03 12:46:16
【问题描述】:

我正在使用 MPDF 在 codeigniter 中生成 pdf 文件。

我的控制器功能看起来像

   function save_pdf($std_id)
   {
     $data['section1_report']= $this->common_model->get_details('tbl_section1',array('id'=>$std_id));
     $html = $this->load->view('reports/section1',$data,true);
      // print_r($html);exit; 
     $this->load->library('pdf');
     $pdf = $this->pdf->load();
     $pdf->WriteHTML($html);
     $pdf->Output();
}

我的pdf 库是

 <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
   class pdf {    
      function pdf()
      {
         $CI = & get_instance();
         log_message('Debug', 'mPDF class is loaded.');
      }

     function load($param=NULL)
     {
         include_once APPPATH.'/mpdf/mpdf.php';         
         if ($params == NULL)
         {
             $param = '"en-GB-x","A4","","",10,10,10,10,6,3';         
         }         
       return new mPDF($param);
     }
  }

我想从视图文件section1 生成 pdf 文件。但是当我调用控制器函数save_pdf 时,我得到了如下错误

当我print_r($html);exit; 时,它会显示视图文件中的所有内容。我在mpdf/includes/functions.php 中使用preg_replace_callback 而不是preg_replace,但它仍然显示这样的错误

我研究了mpdf 文档,它在纯 php 中正常工作。但我想在Codeigniter 中生成 pdf 文件。 如何解决mpdf 中的此类错误?如果我可以在Codeigniter 中使用mpdf 生成pdf file,我将不胜感激。谢谢。

【问题讨论】:

标签: codeigniter mpdf


【解决方案1】:

尝试将 functions.php 的第 79 和 80 行替换为:

$str = preg_replace_callback('/\&\#([0-9]+)\;/m', function($m) use ($lo){return code2utf($m[1],$lo); }, $str);
$str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function($m) use ($lo){return codeHex2utf($m[1],$lo);}, $str);

来源:https://github.com/carlholmberg/mpdf/issues/1

【讨论】:

    【解决方案2】:
    $str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\\1',{$lo})",$str);
    $str = preg_replace('/\&\#x([0-9a-fA-F]+)\;/me', "codeHex2utf('\\1',{$lo})",$str);
    // Remove above and add below code in includes/functions.php
    $str = preg_replace_callback('/\&\#([0-9]+)\;/m', function($m) use ($lo){return code2utf($m[1],$lo); }, $str);
    $str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function($m) use ($lo){return codeHex2utf($m[1],$lo);}, $str);
    
    // Also comment below line in mpdf.php
    $html = preg_replace('/\{DATE\s+(.*?)\}/e',"date('\\1')",$html );
    

    【讨论】:

    • 这个对我有用。我使用了解决方案。它肯定会消除错误,但会使 pdf 损坏。但是当我评论你要求评论的那行时,它起作用了
    • 为你的改变干杯!
    【解决方案3】:
    $str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\\1',{$lo})",$str);
    $str = preg_replace('/\&\#x([0-9a-fA-F]+)\;/me', "codeHex2utf('\\1',{$lo})",$str);
    // Remove above and add below code in includes/functions.php
    $str = preg_replace_callback('/\&\#([0-9]+)\;/m', function($m) use ($lo){return code2utf($m[1],$lo); }, $str);
    $str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function($m) use ($lo){return codeHex2utf($m[1],$lo);}, $str);
    
    // Also comment below line in mpdf.php
    $html = preg_replace('/\{DATE\s+(.*?)\}/e',"date('\\1')",$html );
    

    【讨论】:

      【解决方案4】:

      我替换了这些行:

         $str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\\1',{$lo})",$str);
         $str = preg_replace('/\&\#x([0-9a-fA-F]+)\;/me', "codeHex2utf('\\1',{$lo})",$str);
      

      这些行:

         $str = preg_replace_callback('/\&\#([0-9]+)\;/m', function($m) use ($lo){return code2utf($m[1],$lo); }, $str);
         $str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function($m) use ($lo){return codeHex2utf($m[1],$lo);}, $str);
      

      它工作正常。

      【讨论】:

        猜你喜欢
        • 2014-04-13
        • 1970-01-01
        • 1970-01-01
        • 2016-11-20
        • 2013-03-05
        相关资源
        最近更新 更多