Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure tcpdf when installing with Composer?

Our legacy PHP code includes tcpdf (https://github.com/tecnickcom/TCPDF) as part of the code base.

I am trying to move it out to a vendor folder, so I added Composer to the project, added TCPDF to composer.json and updated.

But the config/tcpdf_config.php file is modified in our code base (custom PDF author name etc.), and rightfully so, according to the docs: http://www.tcpdf.org/installation.php

Now, I'm not sure it's a good idea to modify vendor/tecnick.com/tcpdf/config/tcpdf_config.php because it might be overwritten by Composer any time I update. Also, there is not a word about Composer in the tcpdf docs.

What is the right solution to configure tcpdf (or any third-party library used through Composer, for that matter) while allowing Composer updates?

like image 979
Dávid Veszelovszki Avatar asked Mar 17 '23 22:03

Dávid Veszelovszki


1 Answers

The way you are supposed to inject your configuration is to define all the constants first before ever touching the first TCPDF class.

Make sure to also set the constant K_TCPDF_EXTERNAL_CONFIG to true. This will prevent the autoconfiguration to search for the file you were talking about. (See line 60 of this file here: http://sourceforge.net/p/tcpdf/code/ci/master/tree/tcpdf_autoconfig.php)

This is well hidden in the documentation, but I found this: http://www.tcpdf.org/doc/code/example__019_8php.html

How to override TCPDF config using Composer

  1. Copy the original tcpdf_config.php somewhere to your project, for example src/tcpdf_config.php.
  2. Add define('K_TCPDF_EXTERNAL_CONFIG', true); at the beginning of your config copy and modify the rest of the config to your needs.
  3. Edit your composer.json and add/update autoload section:
...
"autoload": {
  ...
  "files": [
    "src/tcpdf_config.php",
    ...
  ]
}
...
  1. Regenerate the composer autoloader using composer dump-autoload.
like image 134
Sven Avatar answered Apr 02 '23 14:04

Sven