0

In my Joomla 4 component I successfully used the Mpdf library. Since migrating to Joomla 5, the library does not initialise properly. This is the setup that worked in Joomla 4:

class MyModel extends BaseModel
{

   public function generatePDF($data)
   {
      $mpdf = new \Mpdf\Mpdf([]); 
      ...
   }
}

The composer.json which loads the Mpdf version working with PHP 8.2 https://mpdf.github.io/about-mpdf/requirements-v7.html:

{
  "config": {
    "vendor-dir": "admin/vendor",
    "platform": {
      "php": "8.2.0"
    }
  }, "require: {
    "mpdf/mpdf": "^8.1.3"
  }
}

The vendor classes are located in /administrator/components/com_mycomponent/vendor which contains the /autoload.php as well.

In the mycomponent.xml the vendor files are registered like this:

<administration>
  <files folder="admin">
     ...
     <folder>vendor</folder>
  </files
</administration>

What is the proper way to autoload external libraries in Joomla 5?

4
  • Could you turn on debug and share the error message? Where are you loading vendor/autoload.php? Why is your vendor-dir set to "admin/vendor"? Commented Nov 20, 2023 at 22:21
  • I get the following debug error: Compile Error: Declaration of Mpdf\Mpdf::setLogger(Psr\Log\LoggerInterface $logger) must be compatible with Psr\Log\LoggerAwareInterface::setLogger(Psr\Log\LoggerInterface $logger): void (500 Whoops, looks like something went wrong.)
    – cappu
    Commented Nov 21, 2023 at 10:12
  • As far as I have found on the web, the individual component vendor libraries should be separated from the Joomla core libraries. Therefore the "admin/vendor" dir. - I do not know if this is still state of the art.
    – cappu
    Commented Nov 21, 2023 at 10:13
  • I found the web source why I put Mpdf into "admin/vendor". - It is described here in dionysopoulos.me/component/docimport/article/…
    – cappu
    Commented Nov 21, 2023 at 16:44

1 Answer 1

0

Solution:

There is one missing piece of the puzzle. The component must have access to the autoloader. To do this, you need to place this line of code on my component's service provider - like this:

...
use Joomla\DI\ServiceProviderInterface;

//require_once __DIR__ . '/../vendor/autoload.php'; // This did not work in my case
require_once JPATH_ADMINISTRATOR . '/components/com_mycomponent/vendor/autoload.php';

/**
 * The service provider.
 *
 * @since  4.0.0
 */
return new class() implements ServiceProviderInterface
{
   ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.