Search code examples
phplaraveldompdf

Class 'Dompdf\Dompdf' not found


I'm trying to install dompdf using composer, I followed the instructions from Installing DOMPDF with Composer

So far I've

  • In composer.json

    ...
    "require": {
        ...
        "dompdf/dompdf": "~0.6.1"
    },
    "autoload": {
    ....
    
  • run composer update

  • in autoload.php already have require __DIR__.'/../vendor/autoload.php';
  • In vendor/dompdf/dompdf/dompdf_config.inc.php changed def("DOMPDF_ENABLE_AUTOLOAD", true); to def("DOMPDF_ENABLE_AUTOLOAD", false);
  • My controller code

```

use Dompdf\Adapter\CPDF;      
use Dompdf\Dompdf;
use Dompdf\Exception;

require_once "vendor/dompdf/dompdf/dompdf_config.inc.php";

class ArticleController extends BaseController {
  ...
  public function downloadPdf(){
    $dompdf = new Dompdf();
    $dompdf->loadHtml('hello world');
    $dompdf->render();
    $dompdf->output();
  }
}
  • "post" route for ArticleController@downloadPdf

so now when I try to download pdf, if gives me error:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'Dompdf\Dompdf' not found'

have I missed any setup step or doing something wrong?


Solution

  • This issue at dompdf github page helped me to solve this error

    The latest stable (0.6.1) does not support namespaces and so would not need the use statement in your code. The upcoming release (0.7.0) does include namespace support.

    So, I just removed

    use Dompdf\Adapter\CPDF;      
    use Dompdf\Dompdf;
    use Dompdf\Exception;
    

    and used new DOMPDF(); instead of new Dompdf(); as with version 0.6.* namespace will not work.