Tuesday, September 25, 2018

Few Notes about TCPDF - An Open Source PHP class for generating PDF documents

Few Notes about TCPDF - An Open Source PHP class for generating PDF documents

TCPDF is an open source PHP class for generating PDF documents. It is easy to install and setup as well as many options available. There are few things worth to be noticed in my experience of usage. Here are some:

1. Installation

To install TCPDF class is easy.

  1. Download all files from TCPDF’s github.
  2. Create a directory class under the main directory of the Site.
  3. Create a directory class/tcpdf.
  4. Copy all files to class/tcpdf.
  5. Add a line of code in the beginning of the php file:
    require_once "class/tcpdf/tcpdf.php";
  6. Have fun!

2. Traditional Chinese support

The default setting of TCPDF does not support Traditional Chinese character because it lacks of fonts in Traditional Chinese. We have to add the font files manually.

  1. Download font file ‘Droid Sans Fallback’ and ‘msungstdlight’.
  2. Add two font files to class/tcpdf/fonts.
  3. Open config/tcpdf_config.php and change two settings:
    define ('PDF_FONT_NAME_MAIN', 'helvetica');
       to
    define ('PDF_FONT_NAME_MAIN', 'msungstdlight');
    
    define ('PDF_FONT_NAME_DATA', 'helvetica');
    	to
    define ('PDF_FONT_NAME_DATA', 'msungstdlight');
    

p.s Here explains how to generate TrueTypeUnicode font files from DroidSansFallback.ttf font file.

3. A bug fixed while using PHP 7.2

When using PHP 7.2, TCPDF version 6.2.12 shows the error message:

Deprecated: The each() function is deprecated. 
This message will be suppressed on further calls in /Users/xxx/Sites/mini_shop/class/tcpdf/tcpdf.php on line 16544
  
TCPDF ERROR: Some data has already been output, 
can't send PDF file

Since each() function is deprecated after PHP version 7.2, the code must be modified in order to fit the new version. Fortunately, somebody has posted the solution. It works again just to replace three lines of code in tcpdf.php as follows:

-  while (list($key, $val) = each($prop)) {
+  foreach($prop as $key => $val) {
-  while (list($id, $name) = each($attr_array[1])) {
+  foreach($attr_array[1] as $id => $name) {
-  while (list($id, $name) = each($style_array[1])) {
+  foreach($style_array[1] as $id => $name) {

Replace each() function with foreach() function, and that’s it!

1 comment:

  1. It is possible to put in and also create along with many choices accessible. You can find Digital Ocean vs AWS a handful of items well worth being seen if you ask me regarding use.

    ReplyDelete