2013. május 10.
Drupal

TCPDF and Drupal

Image

If you want to generate pdf on your Drupal site, there are multiple modules you can choose. Most of them are complex modules targeting to solve problems like converting entities or filled forms to pdf. However, when I needed to create a custom pdf file with a custom module I did not find any other viable solutions, but to download and integrate a pdf library myself. I chose to use the TCPDF library, and decided to create a general integration module for it.

Image

The module is pretty easy to install. Download and install libraries module, then copy TCPDF files to sites/all/libraries, so tcpdf.php will be at sites/all/libraries/tcpdf/tcpdf.php. After you have installed tcpdf module, you can verify at the status reports if everything is ok. If it cannot find the library, check if the user of your webserver has permissions to access the TCPDF files.
 

Without parameters, tcpdf_get_instance() will return an instance of TCPDFDrupal which extends TCPDF and adds some extra functionality to it which comes handy when using it from Drupal. However, the main principle is everything that works with the original TCPDF class should work with TCPDFDrupal.
 

  1. // Code snippet which creates a simple pdf object.
  2. <?php
  3.   $tcpdf = tcpdf_get_instance();
  4. 
     
  5.   $tcpdf->DrupalInitialize(array(
  6.     'footer' => array(
  7.       'html' => 'This is the <em>footer</em>',
  8.     ),
  9.     'header' => array(
  10.       'callback' => 'tcpdf_example_custom_pdf_header',
  11.     ),
  12.   ));
  13. ?>
  14. 
     
  15. <?php 
  16. function tcpdf_example_default_header(&$tcpdf) {
  17.   $theme_settings = variable_get('theme_' . variable_get('theme_default', '') . '_settings', '');
  18.   $logo_path = isset($theme_settings['logo_path']) ? $theme_settings['logo_path'] : PDF_HEADER_LOGO;
  19.   $tcpdf->Image(drupal_realpath($logo_path), 10, 10, 30, 0, '', variable_get('site_url', ''), '', TRUE, 150, '', FALSE, FALSE, 0, FALSE, FALSE, FALSE);
  20. }
  21. ?>


DrupalInitialize() is an extra method of TCPDFDrupal that initializes some TCPDF variables (like font types). You can also set the header and footer of your PDF document without extending TCPDF. For a simple html snippet, use 'html', but if you need to do something fancy, or just want to avoid html-to-pdf conversion, it is possible to set a callback instead.
 

This callback gets the TCPDFDrupal object by reference and you can use it like $this in the Header() function of a class that extends TCPDF.
 

In most cases it is unnecessary to extend TCPDFDrupal, but if you really need to, you can do it by adding parameters to tcpdf_get_instance(). tcpdf_get_instance() is also capable of overriding the default parameters for the constructor, and the config file.
 

  1. <?php
  2.   $tcpdf_params = array(
  3.     'unicode' => FALSE,
  4.     'encoding' => 'ISO-8559-1',
  5.   );
  6.   // TCPDF class will be MyCustomTCPDFClass from mymodule/mycustomtcpdf.class.inc.
  7.   $tcpdf_class = array(
  8.     'class' => 'MyCustomTCPDFClass',
  9.     'filetype' => 'inc',
  10.     'filename' => 'mycustomtcpdf.class',
  11.     'module' => 'mymodule',
  12.   )
  13.   // Mymodule/mymodule_tcpdf.config.inc will be used as the config file.
  14.   $tcpdf_config = array(
  15.     'filetype' => 'inc',
  16.     'filename' => 'mymodule_tcpdf.config',
  17.     'module' => 'mymodule', 
  18.   );
  19.   $tcpdf = tcpdf_get_instance($tcpdf_params, $tcpdf_class, $tcpdf_config);
  20. ?>


If you need any support, or find any issues, please use the issue queue.

Related posts

Image
Image
2016. június 3.
Drupal

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.