Search code examples
phpfpdffpdi

Class Object inside Function Not Working (FPDF)


I'm using FPDF (plus FPDI).

I have a code like this:

$pdf->setSourceFile("source.pdf");
$tplIdx = $pdf->importPage(1);
$size = $pdf->useTemplate($tplIdx, 1, 1, 5.4);

Result: Works fine.

But when I wrap the code inside a function:

function hello(){
$pdf->setSourceFile("source.pdf");
$tplIdx = $pdf->importPage(1);
$size = $pdf->useTemplate($tplIdx, 1, 1, 5.4);
}
hello();

Result:

Fatal Error: Call to a member function setSourceFile() on a non-object

For some reason, the $pdf object isn't working when inside the function.

Any clue why?


Solution

  • @qrafzvzv, You need to pass pdf object as a parameter inside your function.

    For Example :
    
    function hello($pdf) {
        $pdf->setSourceFile("source.pdf");
        $tplIdx = $pdf->importPage(1);
        $size = $pdf->useTemplate($tplIdx, 1, 1, 5.4);
    }
    hello($pdf);