跨多个类使用TCPDF

时间:2017-08-17 05:44:48

标签: tcpdf php-7 slim-3

我正在尝试编写一个可以调用多个类的函数,每个函数都会将一个小部件添加到单个PDF文件中,然后返回整个PDF文件。

这是构造函数的代码:

<?php

class PdfConstructor {

    protected $logger; // I'm sure there's another way to use the logger located in the public dependencies without having to initalise it here

    public function __construct($logger){
        $this->logger = $logger;
    }

    public function studentPdf(){
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // ---------------------------------------------------------
        require 'pdfwidgets/studentheader.php'; // TODO: Need to load this via autoloader
        $headerController = new StudentHeader($pdf);
        $headerInfo = $hederController->getHeader();

        // ---------------------------------------------------------

        //Close and output PDF document
        $pdf->Output('C:\xampp7\htdocs\edquire-api\logs\test.pdf', 'F');
    }
}

然后在studentheader我有这个:

<?php

class StudentHeader {

    protected $pdf;

    public function __construct($pdf){
        $this->$pdf = $pdf;
    }

    public function getHeader(){
        $this->pdf->AddPage();
    }
}

我正在尝试将$pdf对象传递给studentheader,但我收到错误:

Catchable fatal error: Object of class TCPDF could not be converted to string in C:\xampp7\htdocs\edquire-api\classes\pdfwidgets\studentheader.php on line 8

为什么要尝试将其转换为字符串,是否有更好的方法来使用小部件来构建pdf?

1 个答案:

答案 0 :(得分:1)

这是第8行:

 $this->$pdf = $pdf;

应该是:

 $this->pdf = $pdf;

它说它无法将$pdf转换为字符串,因为属性名称必须是字符串。你在那里使用它作为属性名称。

脚注:如果您没有“收到”错误消息,那么谷歌总是一个好的策略。我用谷歌搜索“可捕获的致命错误:类TCPDF的对象无法转换为字符串”,并且顶部的结果都指向正确的方向。

相关问题