0

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?

Martin
  • 83
  • 3
  • 16

2 Answers2

1

You can solve it like this:

class PDF extends FPDF {
  public  $is_summer;
  function Footer() {
    // some code
  }

  function Header() {
    if ($this->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);
    }
  }
}

So what you'd do is when you make the PDF object, you set the is_summer property to true or false, and it'll be able to use that property in your Header function.

To do this, you can make a constructor function like so:

class PDF extends FPDF {
  public  $is_summer;
  function __construct($is_summer){
    $this->is_summer = $is_summer;
  }
  function Footer() {
    // some code
  }

  function Header() {
    if ($this->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);
    }
  }
}
Florian Humblot
  • 1,121
  • 11
  • 29
1

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);
NibelDz
  • 38
  • 4