【问题标题】:HTML to PDF conversion with external CSS support in Codeigniter在 Codeigniter 中使用外部 CSS 支持将 HTML 转换为 PDF
【发布时间】:2015-06-29 11:55:44
【问题描述】:

在比较了 dompdf、TCPDF、MPDF 的特性之后,我在这里使用 MPDF 在我的 codeigniter 应用程序中将 HTML 转换为 PDF。在本地主机中,它工作正常。并且 pdf 在 2 或 3 秒内生成。当我将它上传到实时服务器时,创建相同的 pdf 文件需要 3 分钟以上。我已经检查了文件权限。 Pdf 正在生成并保存在文件夹中。但这需要很长时间。

$this->ci->load->library('m_pdf');
$html = $this->ci->load->view('ecommerce/salespdf',$this->data,true);
$m_pdf = $this->ci->m_pdf->load();
$m_pdf->WriteHTML($html);
$filepath = getcwd()."/assets/other_uploads/pdf_files/";
$m_pdf->Output($filepath.$filename, "F");

从上面的代码可以看出,pdf是在指定文件夹中生成的,但是耗时比较长。视图文件中只包含一个外部 css 文件。

<link href="<?php echo CSS_URL; ?>pdf-invoice.css" rel="stylesheet" type="text/css" />

我的代码有什么问题吗?

【问题讨论】:

    标签: css codeigniter pdf mpdf


    【解决方案1】:

    最后我修复了这个错误。问题是在视图页面内加载图像。在这里,我添加带有公司名称和徽标的标题。

    http://domain.com/assets/other_uploads/photo/photo.png
    

    如果我在图像 URL 中提供上述来源,它会尝试在我的视图页面中加载图像并且失败。所以,我用下面的代码来解决这个问题。

    var/www/domain/assets/other_uploads/photo/photo.png
    

    对于外部 css 问题,我将视图页面中的 css 部分添加为内部 css。

    现在可以了!!!

    【讨论】:

      【解决方案2】:
      function WriteHTML($html,$bi)
          {
              //remove all unsupported tags
              $this->bi=$bi;
              if ($bi)
                  $html=strip_tags($html,"<a><img><p><br><font><tr><blockquote><h1><h2><h3><h4><pre><red><blue><ul><li><hr><b><i><u><strong><em>"); 
              else
                  $html=strip_tags($html,"<a><img><p><br><font><tr><blockquote><h1><h2><h3><h4><pre><red><blue><ul><li><hr>"); 
              $html=str_replace("\n",' ',$html); //replace carriage returns with spaces
              // debug
              if ($this->debug) { echo $html; exit; }
      
              $html = str_replace('&trade;','™',$html);
              $html = str_replace('&copy;','©',$html);
              $html = str_replace('&euro;','€',$html);
      
              $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
              $skip=false;
              foreach($a as $i=>$e)
              {
                  if (!$skip) {
                      if($this->HREF)
                          $e=str_replace("\n","",str_replace("\r","",$e));
                      if($i%2==0)
                      {
                          // new line
                          if($this->PRE)
                              $e=str_replace("\r","\n",$e);
                          else
                              $e=str_replace("\r","",$e);
                          //Text
                          if($this->HREF) {
                              $this->PutLink($this->HREF,$e);
                              $skip=true;
                          } else 
                              $this->Write(5,stripslashes(txtentities($e)));
                      } else {
                          //Tag
                          if (substr(trim($e),0,1)=='/')
                              $this->CloseTag(strtoupper(substr($e,strpos($e,'/'))));
                          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);
                          }
                      }
                  } else {
                      $this->HREF='';
                      $skip=false;
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2012-08-16
        • 2020-04-03
        • 2020-12-03
        • 1970-01-01
        • 1970-01-01
        • 2016-12-06
        • 2011-07-15
        • 2023-03-22
        • 2012-08-13
        相关资源
        最近更新 更多