0

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.

owino
  • 151
  • 1
  • 3
  • 11
  • 2
    How did you install these packages? Using `composer install`? – Nico Haase Apr 17 '21 at 09:22
  • Yes! I used composer. I issued the command 'composer require dompdf/dompdf' – owino Apr 17 '21 at 09:28
  • And what have you tried to resolve the error? Where were these files installed? – Nico Haase Apr 17 '21 at 13:09
  • I am stuck completely! I can't figure out why the parser says the file is not there. The path given is correct, the file is there. – owino Apr 17 '21 at 14:57
  • I bumped into [a similar problem for someone](https://stackoverflow.com/questions/37831516/dompdf-with-codeigniter) and noticed that they were pre-pending the APPPATH constant to the relative paths to the files mentioned in the controller. And this worked! So > require 'vendor/autoload.php'; becomes > require APPPATH.'vendor/autoload.php'; Now I don't consider this the answer because I don't know why the clearly correct relative paths do not work. If someone can explain this, that should be the answer. – owino Apr 18 '21 at 06:22

1 Answers1

0

If you run this command (composer require dompdf/dompdf) in the root directory of your Codeigniter site then you only need to change DomPdf path in the app/Config/Autoload.php file from APPPATH . 'ThirdParty/vendor/dompdf/dompdf/src' to '/vendor/dompdf/src' like this:

public $psr4 = [
    APP_NAMESPACE => APPPATH, // For custom app namespace
    'Config'      => APPPATH . 'Config',
    'Dompdf'    =>  '/vendor/dompdf/src',
];

Explanation: When you run a composer command the Composer create (or update the content if already exist) a vendor directory in the current directory (where you entered in command line before execute any composer command) and downloads the requirements into this directory.

UPDATE:

When you execute a composer command this create or update own vendor directory in current directory. So firstly you create a new CI4 project by Composer with following command: composer create-project codeigniter4/appstarter


Then the Composer also create a "config" directory to itself for the current environment that includes classmaps, PSR-4 autoloads, etc... Now in the vendor/composer directory there is a autoload_psr4.php file which generated when command executed. This file contains all of requirements (both of composer require <example> and found at "require"-block in composer.json file.


Now in the /project-root-directory/vendor/composer/autoload_psr4.php file contains the Codeigniter namespace! After that -- because you wrote -- your Dompdf library and its requirements found in /project-root-directory/app/ThirdParty/vendor/. This is possible if you execute the next command inside the /app/ThirdParty/ directory: composer require dompdf/dompdf

And when appears the following question, you pressed n as No. No composer.json in current directory, do you want to use the one at /project-root-directory? [Y,n]?

Otherwise the Dompdf and its requirements installed into 1st-level vendor directory in project-root.


When you execute the command above, in the app/ThirdParty/vendor/ directory contains only! the Dompdf and its requirements and nothing else therefore you can load the Codeigniter\Controller class with use CodeIgniter\Controller; but the use Dompdf\Dompdf; not working, because the Dompdf vendor directory is not same as Codeigniter framework vendor directory therefore can not communicate to each other.

adampweb
  • 1,135
  • 1
  • 9
  • 19
  • Sorry you are not answering the question... with everything else staying constant, i.e. no change(s) to app/Config/Autoload.php from what I have stated above, my problem was solved simply by pre-pending the constant APPPATH to the relative CORRECT PATHS in the CONTROLLER. Why? Why are these CORRECT PATHS (in the CONTROLLER) not working unless we pre-pend them with the constant, APPPATH? The question has now degraded to this? – owino Apr 18 '21 at 17:13
  • Iys your **app/ThirdParty** directory contains the **vendor** directory and that is contains the **dompdf** directory? – adampweb Apr 18 '21 at 18:57
  • @owino The tutorial which you linked as first is correct, but I think the author forgot to leave a warning that he/she redirected the **vendor** directory to the **app/ThirdParty** location – adampweb Apr 18 '21 at 19:07
  • Yes, if you use composer to install, like I did, the app/ThirdParty directory contains the vendor directory. The path to the dompdf files is app/ThirdParty/vendor/dompdf/dompdf/ – owino Apr 18 '21 at 23:09