Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 696
RHM Avatar asked Sep 03 '26 12:09

RHM


2 Answers

I don't think that you want use the dompdf 0.6 family. In the 0.6 version all the classes are in global space. But since your code is ready to the 0.7, change it to

"dompdf/dompdf": "~0.7"

and run composer update.

like image 159
Federkun Avatar answered Sep 05 '26 03:09

Federkun


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.

like image 25
RHM Avatar answered Sep 05 '26 03:09

RHM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!