TCPDF / TCPDI变量未在合并文档中打印

时间:2018-08-02 18:12:39

标签: php tcpdf

我尝试了几种不同的方法,但似乎无法弄清为什么它不起作用。这将在合并文档的底部打印页码。示例:如果姓氏是Smith,则应该说Smith POL0001。但是,它不是在打印姓氏……它只是说POL0001。下面的代码是它的一部分,还有一个phpMailer,但是我将其排除在外,因为它不相关-但是,相同的$lastName变量在该代码中有效。我尝试将查询复制到同一部分,弄乱了引号和其他一些内容。

<?php

include("db_connection.php");


    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use setasign\Fpdi;

    require '../PHPMailer/src/Exception.php';
    require '../PHPMailer/src/PHPMailer.php';
    require '../PHPMailer/src/SMTP.php';

    if(isset($_POST))
    {
        $uid = $_POST['uid'];
        $updaterID = $_POST['updaterID'];

        $supp_pol_letter = $_POST['supp_pol_letter'];
        $building_pol = $_POST['building_pol'];
        $RCV_pol = $_POST['RCV_pol'];
        $andy_estimate_letter = $_POST['andy_estimate_letter'];

        $supp_pol_letter_order = $_POST['supp_pol_letter_order'];
        $building_pol_order = $_POST['building_pol_order'];
        $RCV_pol_order = $_POST['RCV_pol_order'];
        $andy_estimate_order = $_POST['andy_estimate_order'];

         $query = "SELECT i.insEmail, i.insAltEmail, i.companyName, i.attn, l.companyID, u.lastName, f.s1_claimno FROM insurancecompany i INNER JOIN letterofrep l on l.companyID = i.companyID INNER JOIN logins u ON u.userID = l.clientid INNER JOIN fcis f ON f.uid = l.clientid WHERE l.clientID = ? AND l.letter_type = ?";
         $query = $con->prepare($query);
         $query->execute([$uid,"letterofrep"]);
         $row = $query->fetch();

        $insEmail = $row['insEmail'];
        $insAltEmail = $row['insAltEmail'];
        $companyName = $row['companyName'];
        $companyID = $row['companyID'];
        $attn = $row['attn'];
        $lastName = $row['lastName'];
        $claimno = $row['s1_claimno'];

        $supppolletterpdf = $uid ."_POLletterofrep.pdf";
        $buildingpdf = $uid ."_building_pol_pol.pdf"; 
        $RCVpdf = $uid ."_RCV_pol_pol.pdf";
        $andyestimatepdf = $uid ."_andy_estimate.pdf";      

        $filesarray = array();

        if(($supp_pol_letter == 1) && (file_exists($supppolletterpdf) == 1)) { 
           $filesarray[$supp_pol_letter_order]=$supppolletterpdf;
        }

        if(($building_pol == 1) && (file_exists($buildingpdf) == 1)) { 
           $filesarray[$building_pol_order]=$buildingpdf;
        }

        if(($RCV_pol == 1) && (file_exists($RCVpdf) == 1)) { 
           $filesarray[$RCV_pol_order]=$RCVpdf;
        }

        if(($andy_estimate_letter == 1) && (file_exists($andyestimatepdf) == 1)) { 
           $filesarray[$andy_estimate_order]=$andyestimatepdf;
        } 

        ksort($filesarray);

        require_once('../tcpdf/tcpdf.php');
        require_once('../tcpdf/tcpdi.php');

        class MyPDF extends TCPDI
        {
            public $stamp_num = 1;
            public function Footer()
            {

                $page_num = $this->PageNo();
                if((int)$page_num  > 2){

                        $this->SetFont('helvetica', 'N', 12);
                        $number =  str_pad($this->stamp_num, 4, "0", STR_PAD_LEFT);
                        $paragraph = "<font style=\"color:red\">" . $lastName . " POL " . $number ."</font>";

                        $this->SetY(-13);
                        $this->SetX(148);
                        $this->MultiCell(0, 0,  $paragraph, 0, 'C', 0, 12, '', '', true, 0, true);
                        $this->stamp_num++;
                }

            }

        }

        $pdf = new MyPDF(); 


        foreach ($filesarray AS $file) {

        $pageCount = $pdf->setSourceFile($file);

        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {

        $templateId = $pdf->importPage($pageNo);

        $size = $pdf->getTemplateSize($templateId);

        $pdf->AddPage($size['orientation'], $size);

        $pdf->setPageFormatFromTemplatePage($pageNo, $size['orientation']);

        $pdf->useTemplate($templateId);

        }

        }

        $attachment = $pdf->Output("Merged.pdf", "S");


        echo "Success";

    }

?>

1 个答案:

答案 0 :(得分:0)

您确定$ lastName不为空并且在您的课程中可用吗?实际上...您是否考虑过将$ lastName作为全局对象导入MyPDF?

class MyPDF extends TCPDI {
        global $lastName;
        public $stamp_num = 1;

甚至更好(更干净),创建一个变量设置器:

class MyPDF extends TCPDI {

    protected $lastName = '';
    public $stamp_num = 1;

    public function Footer() {

        $page_num = $this->PageNo();
        if((int)$page_num  > 2){

                $this->SetFont('helvetica', 'N', 12);
                $number =  str_pad($this->stamp_num, 4, "0", STR_PAD_LEFT);
                $paragraph = "<font style=\"color:red\">" . $this->lastName . " POL " . $number ."</font>";

                $this->SetY(-13);
                $this->SetX(148);
                $this->MultiCell(0, 0,  $paragraph, 0, 'C', 0, 12, '', '', true, 0, true);
                $this->stamp_num++;
        }

    }

    public function setlastName($lastName) {
        $this->lastName = $lastName;
    }

    protected function getlastName() {
        return $this->lastName;
    }

}

$pdf = new MyPDF();
$pdf->setlastName($lastName);

有几种方法可以解决此问题,但是我相信您明白了。我测试了您尝试使用自己的TCPDF / TCPDI安装执行的操作的核心,并且该方法有效。您的$ paragraph变量按书面形式,引号和所有形式工作。