php - FPDF如何附加带有动态路径/名称的png
问题描述
您好,我的目标是创建一个带有动态生成的 png 文件(二维码)的 pdf。
我使用如下 FPDF 库:
require('fpdf/fpdf.php');
class PDF extends FPDF
{
private $pathQR;
function setPathQR($pathQR){
$this->pathQR = $pathQR;
}
// Page header
function Header()
{
// Logo
$this->Image('img/logo.png',10,10,-300);
// Arial bold 15
$this->SetFont('Helvetica','B',14);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(100,10,'TITLE',0,0,'C');
// Line break
$this->Ln(10);
$this->SetFont('Helvetica','',12);
$this->Cell(85);
$this->Cell(80,10,'Other row..',0,0,'R');
$this->Ln(10);
$this->SetFont('Times','B',14);
$this->Cell(85);
$this->Cell(80,10,'Blablabla',0,0,'C');
$this->Ln(15);
$this->SetFont('Courier','B',24);
$this->Cell(85);
$this->Cell(80,10,'V T M',1,0,'C');
$this->Image('qrcode/'.$pathQR.'.png',10,10,-300);
$this->Ln(15);
$this->SetFont('Helvetica','',12);
$this->Cell(85);
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',16);
$pdf->setPathQR($e_mail);
$filename="pathFix/file-".$e_mail.".pdf";
$pdf->Output($filename,'F');
我在 PDF 类中创建一个函数
function setPathQR($pathQR){
$this->pathQR = $pathQR;
}
我调用该函数将值设置为 pathQR ($e_mail)
$pdf->setPathQR($e_mail);
但我收到错误通知:未定义的变量:C:\xampp\htdocs 中的 pathQR....
在这一行
$this->Image('qrcode/'.$pathQR.'.png',10,10,-300);
为什么不识别 $pathQR?怎么了?谢谢
解决方案
您可以$e_mail
使用 setter 方法将变量传递到您的类中。创建 fPDF 实例后,添加以下内容:
class PDF extends FPDF {
public $email;
public function setemail($input) {$this->email = $input;}
当你想设置它使用:
$pdf->setemail($e_mail);
在您的header
方法中,使用以下方法引用电子邮件地址:
$this->Image('qrcode/'. $this->email .'.png',10,10,-300);
推荐阅读
- php - If radio button not checked, display all rows from column - formulating sql query
- javascript - 在运行时在 javascript 中更新 Map 的键
- laravel - Is there a way to represent date hole in chartjs time series?
- logging - librdkafka.redist 的日志是什么意思
- javascript - 通过 jquery/javascript 中的 Ajax 调用进行分页
- python - 连接到上游 jwilder/ngnix-proxy 时没有实时上游
- c - PSET3 - Tideman - 锁定对
- excel - 如何在本地时区的 Excel 中显示日期和时间
- r - 具有持久数据表的可编辑闪亮 DT 不显示过滤记录
- sparql - GraphDB 或 RDF4J 错误?BOUND() 产生错误的值