TCPDF Adob​​e不显示页眉和页脚

时间:2016-02-22 14:42:50

标签: php tcpdf codeigniter-3

研究员我有一个问题是使用TCPDF打印页眉和页脚。

打印pdf的类是:

class MyPdf extends Pdf
{
    private $eventid=null;

    function __construct($event_id,$config=null)
    {
        parent::__construct($config);

        $this->eventid=$event_id;
    }

    function Header()
    {
        if($this->eventid!==null)
        {
            $this->SetFont('dejavusans','',8);
            $this->Cell(0,9,'eventid: '.$this->eventid);
            $this->Ln();
        }
    }

    function Footer()
    {
        $this->setY(-20);

        $x=$this->getX();
        $y=$this->getY();

        $this->Cell(0,9,'Powered By Ecampole.com INC ',0,0,'C');

        $this->setXY($x,$y);

        $this->Cell(0,9,$this->PageNo(),0,0,'R');
        $this->Ln(1);
    }

}

PDF库:

require_once('/usr/share/php/tcpdf/tcpdf_import.php');

define('FA_PATH',previous_dir(APPPATH).'assets/vendor/awesome/fonts/');

    class Pdf extends TCPDF
    {

      //private $fa=null;

      private $faPath='fontawesomewebfont';

      /**
      *@param $params {Array} Parameters fo the Library.
      *                       It must have the following:
      *                       *)orientation: 'P' for portrait 'L' for landscape by default is 'L'
      *                       *)unit:  'pt' for points please google and look for more options in this parameter
      *                       *)page: The type of page you want eg. A4, A3 etc etc.
      */
      public function __construct($params)
      {

        $orientation= isset($params['orientation'])?$params['orientation']:'P';//By default portrait landscaping
        $unit=isset($params['unit'])?$params['unit']:'pt';
        $page=isset($params['page'])?$params['page']:'A4';
        parent::__construct($orientation,$unit,$page,true, 'UTF-8', false);


        $ci=&get_instance();
        $ci->load->helper('path');
        //$fa=(isset($params['fa']) && file_exists($params['fa']))?$params['fa']:FA_PATH.'fontawesome-webfont.ttf';

        //$this->fa=$this->addTTFfont($fa,'TrueTypeUnicode', '', 32,FA_PATH);
        //$this->addFont('fontawesomewebfont','',FA_PATH.'fontawesomewebfont');
        //$this->faPath='fontawesomewebfont';
        /*Font setting in order to show UTF-8*/
        //$this->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
        //$this->setFontSubsetting(true);
      }

      /*public function getFontAwesome()
      {
        return $this->fa;
      }*/

      public function getFontAwesome2()
      {
        return $this->faPath;
      }

      public function getHeight()
      {
        return $this->h;
      }

      public function getWidth()
      {
        return $this->w;
      }

      public function bMargin_()
      {
        return $this->bMargin;
      }

      /**
      *We declare it as blank function because by defauld it renders an unwanted black line
      */
      public function Header()
      {

      }

      /**
      *We declare it as blank function because by defauld it renders an unwanted black line
      */
      public function Footer()
        {
        $CI= & get_instance();
        $CI->load->helper('url');

            $this->setY(-15);

            $this->SetFont('dejavusans','',8);
            $x=$this->getX();
            $y=$this->getY();

        $text_size=$this->GetStringWidth('Powered By Example.com INC');
            $this->Cell(0,9,'Powered By Example.com INC',0,0,'C');

        $imagex=$this->getWidth()/2+$text_size/2+1;
        $image=base_url('assets/img/squirrel.png');

        $this->Image($image,$imagex,$y,10,8,'PNG');
        }

      /**
      *Calculate How much height we have
      */
      public function spaceleft_()
      {
        return  $this->getHeight() - $this->GetY() - $this->bMargin_();
      }

      /**
      *It auto breaks the page if remaining space
      *is less that $bias*total_height of the page
      *
      *@param $bias {Float} A floating number betrween 0 and 1.
      *                     0 equals for 0% 1 equals for 100%
      *                     eg. 0.4 equals 40% of the total height
      */
      public function magic_break($bias=0.4)
      {
        $height=$bias*$this->getHeight();
        if($this->spaceleft_()<$height)
        {
          $this->addPage();
        }
      }

      /**
      *Sets The Draw Color Back to Black
      */
      public function resetDrawColor()
      {
        $this->SetDrawColor(0, 0, 0);
      }

      /**
      *Sets The fill Color Back to White
      */
      public function resetFillColor()
      {
        $this->setFillColor(255, 255, 255);
      }

      /**
      *Calculate the image size from pixels to points
      *@param $image {String} The url or the path of the image
      *@param $bias {Int} A scale factor for the image if 0 it does not scale the image.
      *@return {Array} With the $width and $height of the Image
      */
      public function image_size($image,$bias=4)
      {
        list($image_width,$image_height)=getimagesize($image);


        $image_width=$image_width*0.75;
        $image_height=$image_height*0.75;

        if($bias!==0)
        {
          $image_width/=$bias;
          $image_height/=$bias;
        }

        return array($image_width,$image_height);
      }

      /**
      *Converts the image width and image Height to points
      *@param $image_width {Int} The width of the Image in pixels
      *@param $image_height {Int} The height of the image in pixels
      *@param $bias {Int} A scale factor for the image if 0 it does not scale the image.
      *@return {Array} With the $width and $height of the Image
      */
      public function fix_size($image_width,$image_height,$bias=0)
      {
        $image_width=$image_width*0.75;
        $image_height=$image_height*0.75;

        if($bias!==0)
        {
          $image_width/=$bias;
          $image_height/=$bias;
        }

        return array($image_width,$image_height);
      }
    }

绘制Pdf的代码是:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    //define("_SYSTEM_TTFONTS",APPPATH.'/libraries/font/unifont');

    error_reporting(E_ERROR | E_PARSE);
    define('TOP_MARGIN',30);
    define('SIDE_MARGIN',5);


    /**
     * Class for Setting The Header and Footer
     */
    class MyPdf extends Pdf
    {
        private $eventid=null;

        function __construct($event_id,$config=null)
        {
            parent::__construct($config);

            $this->eventid=$event_id;
        }

        function Header()
        {
            if($this->eventid!==null)
            {
                $this->SetFont('dejavusans','',8);
                $this->Cell(0,9,'eventid: '.$this->eventid);
                $this->Ln();
            }
        }

        function Footer()
        {
            $this->setY(-20);

            $x=$this->getX();
            $y=$this->getY();

            $this->Cell(0,9,'Powered By Conferience',0,0,'C');

            $this->setXY($x,$y);

            $this->Cell(0,9,$this->PageNo(),0,0,'R');
            $this->Ln(1);
        }

    }


    $pdf new MyPdf($data['header']);

    $pdf->SetAuthor($owner,true);
    $pdf->SetCreator('Example.com INC',true);
    $pdf->SetTitle($title,true);
    $pdf->SetMargins(SIDE_MARGIN,TOP_MARGIN,SIDE_MARGIN);

    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $pdf->setFontSubsetting(true);

    /* Add the first Page */
    $pdf->AddPage();

    /*####################### Size Metrics ###########################*/
    $remaining=$pdf->getHeight()-28-18-TOP_MARGIN;
    $available_width=$pdf->getWidth()-SIDE_MARGIN-SIDE_MARGIN;
    //Calculate the hald Width
    $available_width_half=$available_width/2;
    //Bootstap like Grid set the Column Size
    $columns=($available_width)/12;



    ?>

但是出于某种原因,在Linux上的evincew显示了页眉和页脚,而Windows上的Firefox和Adobe 9.0 Professional则没有显示它们。 Firefox也没有显示页脚。

你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:-1)

TCPDF有自己的处理页眉和页脚的功能。您可以轻松使用TCPDF::setHeaderData()TCPDF::setFooterData()