I am trying to write a function that would call multiple classes, each would add a widget to a single PDF file and then return that entire PDF file.
Here's the constructor's code:
<?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');
}
}
Then in studentheader
I have this:
<?php
class StudentHeader {
protected $pdf;
public function __construct($pdf){
$this->$pdf = $pdf;
}
public function getHeader(){
$this->pdf->AddPage();
}
}
I am trying to pass the $pdf
object to studentheader
but I'm getting the error:
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
Why is it trying to convert it to string, and is there a better way to achieve constructing that pdf using widgets?
This is line 8:
$this->$pdf = $pdf;
It should be:
$this->pdf = $pdf;
It's saying it can't convert $pdf
to a string because property names need to be a string. You're using it as a property name there.
Footnote to this: if you don't "get" an error message, it's always a good strategy to Google it. I googled "Catchable fatal error: Object of class TCPDF could not be converted to string" and the top results all point one on the right direction.