TCPDF module ported to Drupal 8
A few months ago, I decided to port the TCPDF module for Drupal 8. My first thought was that it would be an easy task, but I ran into my first problem early, when I tried to pull the TCPDF library into Drupal. The main problem was the Libraries module, it manages the load of the TCPDF library in the Drupal 7 version, but it only has a dev. version for Drupal 8 at the moment. Drupal 8 created a methodology to load third party libraries into Drupal, but it isn’t proper for use with PHP libraries. At first I thought it was really annoying that the php libraries aren’t supported, then I realized we got a new tool with Drupal 8, Composer.
What is composer?
This is the definition from Composer's website.
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
So, in our situation the dependency is the TCPDF library which will be downloaded and loaded by Composer. If you are familiar with the drush make, it’s similar.
Composer is part of the Drupal 8 ecosystem and comes with the Drupal 8 codebase. The Composer workflow was changed a little bit for Drupal 8.1. In Drupal 8.1 the vendor folder isn’t followed by git anymore. The Composer stores the downloaded libraries in the vendor folder. Everything mentioned in this article assumes you are using Drupal 8.1 or a newer version.
There are two ways to download it to your local computer.
- Download the Drupal 8 in tar.gz or zip format.
In this case, the vendor folder is part of the tar.gz and if you check the content of the Drupal folder, you will find the vendor folder in it.
- Download the Drupal 8 with git.
In this situation, the vendor folder is missing and you have to install the libraries for Drupal with Composer, otherwise you will get a PHP error when you try to install the Drupal.
It doesn’t matter which way you choose to download it, you have to install Composer to your computer. You can find information about the installation process on Composer’s site for all platforms (Windows/Linux/OSX).
At the beginning of the porting process, I used an extra contrib module to manage the TCPDF library for the module, this was the Composer Manager module, but with the release of 8.1 it became deprecated. Here you can find more information about these changes.
How did the module install change with Drupal 8?
So, as I mentioned before, the biggest change in the Drupal 8 version of the module is that we started using Composer, everything else remained unchanged. I only modified the code in order to make it work with Drupal 8 properly.
1. Install Composer on your local computer
As I described above, you have to install the Composer on your computer.
Just check Composer’s website for more information.
2. Check that Composer is working properly
- Open a terminal,
- go the the Drupal root folder,
- type the next command:
composer install or
If you download Drupal 8 in tar.gz or zip format, you should use the update command instead of install. If everything's alright, it will start to download or update the pre-defined libraries for the Drupal core. It takes a few minutes.
3. Download the TCPDF module
Download the module from the module’s page and put it into the modules folder. Just a reminder, the modules folder moved from
sites/all/modules to my_drupal_install/modules in Drupal 8.
4. Add the TCPDF libraries to the Composer
The easiest way to do this is with the command
composer require tecnickcom/tcpdf 6.2.11
This command downloads the 6.2.11 version of the TCPDF module into the vendor folder and add the tcpdf library to the composer.json file.
If you don’t manage your project with git, you have to be careful whenever you update the Drupal core. The composer.json file is a main component of Composer, so you have to run this command every time.
If you used the Drupal 7 version of the module, maybe you noticed that you didn’t need to put the downloaded libraries files into the
5. Enable the modules
The last remaining thing is to enable the
tcpdf and the
tcpdf_extra modules. I suggest enabling the tcpdf_extra module to make sure that everything's alright.
If you haven’t used the TCPDF module before, I suggest reading this blog post about the Drupal 7 version. There you can find more information on how to use the module, and how to create your own pdf template for your site.