【问题标题】:PHP/FPDF - Value of class variables not appearing on Header and FooterPHP/FPDF - 类变量的值没有出现在页眉和页脚上
【发布时间】:2011-09-07 11:43:55
【问题描述】:

我在设置使用 FPDF 创建的 PDF 的页眉和页脚时遇到了一些问题。如 FPDF 手册 (http://www.fpdf.org/) 中所述,我创建了一个扩展 FPDF 的新类 (PDF),以便我可以创建页眉和页脚。 PDF类的代码如下:

<?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');
}

}
?> 

尽管使用函数 changeName 设置了变量 $primeiroNome 和 $ultimoNome(并且我已经确认变量确实保留了值,在 changeName 的末尾使用了回显),但在打印 PDF 时不会出现, 尽管其余的 Header 内容都会出现。我也尝试过使用 $GLOBALS,但没有成功。

使用以下代码创建 PDF 对象:

$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);
.....

任何帮助将不胜感激。

【问题讨论】:

  • 调试的时候尽量丢弃不相关的代码。如果echo 显示您的值达到changeName(),则对用于获取它们的 SQL 代码进行故障排除是没有意义的。

标签: php fpdf


【解决方案1】:

您提到的那些变量是类属性,而不是方法的局部变量,因此您必须使用$this-&gt; 前缀。而不是这个:

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

... 这样做:

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

你正确地保存它们,但称它们错误:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 2012-08-10
    • 2023-01-28
    • 1970-01-01
    相关资源
    最近更新 更多