首页 > 解决方案 > 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?怎么了?谢谢

标签: phpfpdf

解决方案


您可以$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);

推荐阅读