【问题标题】:tcpdf edit footertcpdf 编辑页脚
【发布时间】:2011-02-18 22:42:45
【问题描述】:

如何使用 tcpdf 编辑页脚?我想在页脚添加当前日期和时间。请帮忙。

【问题讨论】:

    标签: edit footer tcpdf


    【解决方案1】:

    像这样覆盖类:

    class MYPDF extends TCPDF {
        public function Footer() {
            $image_file = "img/bg_bottom_releve.jpg";
            $this->Image($image_file, 11, 241, 189, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
    
            $this->SetY(-15);
            $this->SetFont('helvetica', 'N', 6);
            $this->Cell(0, 5, date("m/d/Y H\hi:s"), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }
    }
    

    然后代替调用:

    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    

    做:

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    

    【讨论】:

    • 通过使用中间显示的这个代码日期但我想在左下角显示日期我该怎么办?
    【解决方案2】:

    您必须按照默认示例 n 中的说明扩展 TCPDF 类并覆盖 Footer() 方法。 3.查看官方http://www.tcpdf.org网站和论坛了解更多信息。

    【讨论】:

      【解决方案3】:

      请问如何创建 2 个页脚行?

      class MYPDF extends TCPDF {
          private $customFooterText = "";
      
          /**
           * @param string $customFooterText
           */
          public function setCustomFooterText($customFooterText)
          {
              $this->customFooterText = $customFooterText;
          }
      
          public function Footer()
          {
              $this->SetY(-15);
              // Set font
              $this->SetFont('helvetica', 'I', 8);
              // Page number
              $this->Cell(0, 10, $this->customFooterText, 0, false, 'C', 0, '', 0, false, 'T', 'M');
          }
      }
      

      用法

      $pdf = new MYPDF();
      $pdf->setCustomFooterText('THIS IS CUSTOM FOOTER');
      

      【讨论】:

      • 对于多行文本我认为使用 HTML 更容易,在 $mycustomfootertxt 中定义它: Replace $this->Cell line with $this->writeHTML($mycustomfootertxt, false, true, false,真);
      【解决方案4】:

      EOD之后使用下面的代码覆盖footer方法。

       EOD;
      
       $txt = date("m/d/Y h:m:s");    
      
       // print a block of text using Write()
       $pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
      

      【讨论】:

        【解决方案5】:

        如果您想在多个页面上放置不同的内容:

        define("bottom_info", "|FIRST PAGE|SECOND PAGE|THIRD PAGE|...", true);  
        

        信息将用“|”分隔(带有分解功能)

        类:

        class MYPDF extends TCPDF {
        
        public function Header() {
        }
        
        // Page footer
        public function Footer() {
            $this->SetY(-15);       
            $this->SetFont('helvetica', '', 7);
            // Page number      
            $titulos = explode("|",bottom_info);        
            $this->Cell(0, 10,  $titulos[$this->page].' - Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }
        

        }

        【讨论】:

          【解决方案6】:

          如果您想动态更改页脚文本,请像这样扩展 TCPDF

          class MYPDF extends TCPDF {
          
              private $customFooterText = "";
          
              /**
               * @param string $customFooterText
               */
              public function setCustomFooterText($customFooterText)
              {
                  $this->customFooterText = $customFooterText;
              }
          
              public function Footer()
              {
                  $this->SetY(-15);
                  // Set font
                  $this->SetFont('helvetica', 'I', 8);
                  // Page number
                  $this->Cell(0, 10, $this->customFooterText, 0, false, 'C', 0, '', 0, false, 'T', 'M');
              }
          
          }
          

          用法:

          $pdf = new MYPDF();
          $pdf->setCustomFooterText('THIS IS CUSTOM FOOTER');
          // .... etc 
          

          【讨论】:

            【解决方案7】:

            我想通了,这是可能遇到此线程的其他人的解决方案。

            编辑文件“tcpdf.php”。

            搜索“public function Footer()

            在此处添加时间戳:

            $timestamp = date("m/d/Y h:m:s");
            
            if (empty($this->pagegroups)) {
            
            $pagenumtxt = ' Created on ' .$timestamp.'   '.$this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
            
            } else {
            
            $pagenumtxt = ' Created on ' .$timestamp.'   '. $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
            
            }
            

            【讨论】:

            • 这被否决了,因为最好扩展类并覆盖方法而不是按照此答案中的建议编辑源代码。 HTH
            • 顺便说一句:在许多情况下,您不能像 TCPDF 这样编辑依赖项,否则您必须将其添加到您的存储库中
            猜你喜欢
            • 2023-01-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-04
            • 1970-01-01
            相关资源
            最近更新 更多