phpfpdffpdi

PHP - Class 'Fpdi' not found but the file defining it is included


Hello I'm using Fpdi class to add an Image on a pdf file (in PHP) (I downloaded code from github https://github.com/Setasign/FPDI)

But when i try instantiate a new Fpdi, I get the error: Class 'Fpdi' not found at line $pdf = new Fpdi();

Here is my code:

<?php
    //I don't get any error like require_once(): Failed opening required so I guess the files exist 
    require_once "fpdf.php";
    require_once "FPDI/src/autoload.php";
    require_once "FPDI/src/Fpdi.php";

    $test = new FPDF();
    echo '<br><br>';
    //no probleme here
    var_dump($test);
    echo '<br><br>';
    //I can see the files I want to include (fpdf.php,FPDI/src/autoload.php,/FPDI/src/Fpdi.php,FPDI/src/FpdfTpl.php,FPDI/src/FpdiTrait.php)
    print_r(get_required_files());
    echo '<br><br>';
    //error here
    $pdf = new Fpdi();
    $pdf->AddPage();
    $pdf->setSourceFile("commentaires.pdf");
    $template = $pdf->importPage(1);
    $pdf->useTemplate($template);
    $pdf->Image('test.jpg', 1, 1, 200, 200);
    $pdf->Output();
?>

Can someone help me please?

Thank you in advance


Solution

  • Check the namespace on the class. You likely need to include that when instantiating it. Looking at the repo, this should work.

    $pdf = new \setasign\Fpdi\Fpdi();