Search code examples
phpheaderfpdf

FPDF: different header logo


I use the FPDF Class in PHP to create PDFs. All works well, but now I need the PDF with different logos in the header.

Creating a header works well.

class PDF extends FPDF {
  function Footer() {
    // some code
  }

  function Header() {
    $this->Image('PDF/images/pdf_header.png', 0, 0, 215);
  }
}

Now I need something like this. The variable "$is_summer" exists in my file.

class PDF extends FPDF {
  function Footer() {
    // some code
  }

  function Header() {
    if ($is_summer)
      $this->Image('PDF/images/pdf_header_summer.png', 0, 0, 215);
    } else {
      $this->Image('PDF/images/pdf_header_winter.png', 0, 0, 215);
    }
  }
}

But I get: Notice: Undefined variable: is_summer in createPDFpriv.php on line 200.

Ok, undefined var. So I tried

function Header($is_summer) {
  if ($is_summer)
    $this->Image('PDF/images/pdf_header_summer.png', 0, 0, 215);
  } else {
    $this->Image('PDF/images/pdf_header_winter.png', 0, 0, 215);
  }
}

Another error: Warning: Declaration of PDF::Header($is_summer) should be compatible with FPDF::Header() in createPDFpriv.php on line 145

Also tried:

 if ($is_summer) {
   function Header() {
     $this->Image('PDF/images/pdf_header_summer.png', 0, 0, 215);
   }
 } else {
   function Header() {
     $this->Image('PDF/images/pdf_header_winter.png', 0, 0, 215);
   }
 }

Also an error: parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in createPDFpriv.php on line 199. Ok, there is a function expected, this doesn't work.

Can anyone help?


Solution

  • Perhaps you have forgotten " $this " before " is_summer "? You can try this :

    class PDF extends FPDF{
    
    	private $isSummer = false;
    	function __construct($summer) {
    		parent::__construct();
    		$this->isSummer = $summer;
    		print "In BaseClass constructor\n";
    	}
    
    	function Header() {
    		if ($this->isSummer) {
    			$this->Image('PDF/images/pdf_header_summer.png', 0, 0, 215);
    		} else {
    			$this->Image('PDF/images/pdf_header_winter.png', 0, 0, 215);
    		}
    	}
    }

    and when using it :

    $mysummer = true; // or false
    $mypdf = new PDF($mysummer);