PHP/FPDF – Value of class variables not appearing on Header and Footer

fpdfPHP

I'm having a bit of trouble setting the Header and Footer of a PDF I'm creating with FPDF. As specified in the FPDF manual (http://www.fpdf.org/), I created a new class (PDF) that extends FPDF so that I could create my Header and Footer. The code for the class PDF is the following:

<?php
require('fpdf.php');

class PDF extends FPDF
{

var $primeiroNome;
var $ultimoNome;

function changeName($firstName, $lastName) {
 $this->primeiroNome = $firstName;
 $this->ultimoNome = $lastName;
}
// Cabeçalho
function Header()
{
 // Cor do texto
 $this->SetTextColor(0, 0, 0);
 // Logo
 $this->Image('Imagens/manviaPdf.png',110,6);
 // Tipo de letra
 if($this->page == 1) {
  $this->SetFont('Arial','B',15);
  // Titulo
  $this->Cell(30,10,'Currículo institucional de:',0,0,'L');
  // Quebra de linha
  $this->Ln(10);
  // Nome colaborador
  $this->Cell(30,10, $primeiroNome . ' ' . $ultimoNome,0,0,'L');
 }
 // Line break
 $this->Ln(12);
}

// Rodapé
function Footer()
{
 // Cor do texto
 $this->SetTextColor(0, 0, 0);
 // Posicionar o cabeçalho a 1,5 centimetros do fim da página
 $this->SetY(-15);
 // Tipo de letra
 $this->SetFont('Arial','I',8);
 // Número da página
 $this->Cell(0,10,'Pag '.$this->PageNo().'/{nb}',0,0,'L');
 // Informação adicional
 $this->Cell(0,10,'Curriculum Vitae de ' . $ultimoNome . ', ' . $primeiroNome . ' | MANVIA, S.A', 0, 0, 'R');
}

}
?> 

The variables $primeiroNome and $ultimoNome, despite being set with the function changeName (and I have confirmed that the variables do keep the value, having used an echo in the end of changeName) don't appear when the PDF is printed, although the rest of the Header contents appear. I have also tried using $GLOBALS, but to no success.

The PDF object is created using the following code:

$link = mysql_connect('localhost', 'user', 'password'); 
if (!$link) {
 die("A ligação ao servidor não foi possível!");
}
$bd_escolhida = mysql_select_db('criadorcv',$link);
if(!$bd_escolhida) {
 die("Não é possível escolher a base de dados definida");
}

$queryString = "SELECT primeiroNome, ultimoNome FROM cartaovisita WHERE id=" . $value; 
$query = mysql_query($queryString) or die ("Problema ao obter os dados do colaborador");
$row = mysql_fetch_array($query);

$pdf = new PDF();
$pdf->changeName($row['primeiroNome'], $row['ultimoNome']);

mysql_free_result($query);

$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->AddFont('Garamond', '', 'GARA.php');
$pdf->SetFont('Arial','B',15);
$pdf->SetFillColor(174, 38, 22);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(0,10,'Cartão de Visita',1, 0, 'L', true);
$pdf->SetTextColor(0, 0, 0);
.....

Any help would be appreciated.

Best Answer

Those variables you mention are class properties, not local variables of the methods, so you must use the $this-> prefix. Rather than this:

$this->Cell(0,10,'Curriculum Vitae de ' . $ultimoNome . ', ' . $primeiroNome . ' | MANVIA, S.A', 0, 0, 'R');
}

... do this:

$this->Cell(0,10,'Curriculum Vitae de ' . $this->ultimoNome . ', ' . $this->primeiroNome . ' | MANVIA, S.A', 0, 0, 'R');
}

You save them correctly but call them wrong :)

Related Topic