I have been porting my project to CodeIgniter4 from CodeIgniter3 for some time now. I am now at the stage where I would like to spew out some information in PDF. I used to use dompdf library in CI3 without any problems. I have hit a concrete wall in CI4.
I went through the step 1 to 4 as explained in this tutorial with the following deviations
In the app/Config/Autoload.php, I registered the dompdf service this way:
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
//'Dompdf' => APPPATH . 'ThirdParty/dompdf/src',
//not like this ^^^
'Dompdf' => APPPATH . 'ThirdParty/vendor/dompdf/dompdf/src', <-- like this
];
and my controller looks like this
<?php
namespace App\ThirdParty;
namespace App\Controllers;
use Dompdf\Dompdf;
use CodeIgniter\Controller;
class AjaxDomPDF extends Controller
{
function htmlToPDF($view){
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml(view($view));
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();
}
}
and my route entry in app/Config/Routes.php file is
$routes->get('generatePDF/(:segment)', 'AjaxDomPDF::htmlToPDF/$1');
With this arragement, I get the error
Class 'Dompdf\Cpdf' not found
I foraged and the best solution seemed to be this and therefore I edited my controller to like as follows
<?php
namespace App\ThirdParty;
namespace App\Controllers;
require 'vendor/autoload.php';
require 'vendor/dompdf/dompdf/lib/Cpdf.php';
use Dompdf\Dompdf;
// disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', false);
define("DOMPDF_ENABLE_REMOTE", true);
// include DOMPDF's default configuration
require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php';
use CodeIgniter\Controller;
class AjaxDomPDF extends Controller
{
function htmlToPDF($view){
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml(view($view));
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();
}
}
With this I now get the error
require(vendor/autoload.php): failed to open stream: No such file or directory
I foraged and found this similar question but no answer there, so far, seem to address my problem.