CakePhp 3.x, TCPDF, htmlspecialchars

I installed the CakePDF plugin according to the documentation: https://github.com/FriendsOfCake/CakePdf

Now I want to create the first PDF file, and I got the following error:

enter image description here

This is my configuration in bootstrap.php:

Configure::write('CakePdf', [ 'engine' => 'CakePdf.Tcpdf', 'margin' => [ 'bottom' => 15, 'left' => 50, 'right' => 30, 'top' => 45 ], 'download' => true, 'encoding' => 'UTF-8' ]); 

The only code I wrote is the following in the template:

 $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); 

This is the code from line 68 in functions.php:

 function h($text, $double = true, $charset = null) { if (is_string($text)) { //optimize for strings } elseif (is_array($text)) { $texts = []; foreach ($text as $k => $t) { $texts[$k] = h($t, $double, $charset); } return $texts; } elseif (is_object($text)) { if (method_exists($text, '__toString')) { $text = (string)$text; } else { $text = '(object)' . get_class($text); } } elseif (is_bool($text)) { return $text; } static $defaultCharset = false; if ($defaultCharset === false) { $defaultCharset = mb_internal_encoding(); if ($defaultCharset === null) { $defaultCharset = 'UTF-8'; } } if (is_string($double)) { $charset = $double; } return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, ($charset) ? $charset : $defaultCharset, $double); } 

I am completely confused and cannot find any solution. Does anyone have an idea?

+6
source share
4 answers

As you understand, the problem is that the TCPDF class messes with mb_internal_encoding() , which is used in CakePHPs h() to determine the default encoding of applications, in case the explicit is not passed as an argument.

Iโ€™m not a TCPDF expert, I havenโ€™t used it in my age, but having a quick look at the current source, itโ€™s hard for me to understand why it involves internal encoding in general, since the only mb_* used by the library is mb_convert_encoding() , where both encoding arguments are passed, therefore, internal encoding is not used at all. In doing so, I report this as an error / problem for developers (developers) of TCPDF.

In any case, you only experience this problem because you are using CakePDF incorrectly. There is no need to manually create instances of the PDF instance, which is what CakePDF automatically does for you, which is pretty much the whole next to the plugin, it abstracts the creation of PDF files, so you just need to create the correct HTML code in your presentation templates. In this way, you also avoid the TCPDF encoding problems that you are currently experiencing, since the presentation template is rendered prior to instantiating the PDF engine.

TL; DR

In short, create only HTML in your view template, and if you really need to have access to an instance of the PDF engine because you need to do something that can only be done in this way, then CakePDF is not the plugin you are looking for.

0
source

After trying to find and debug the same error for an hour, I simply reset the UTF-8 value after using TCPDF - and everything works as before:

 $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); ///...create, save, display your pdf // Reset the encoding forced from tcpdf mb_internal_encoding('UTF-8'); 

I also tried resetting it immediately after calling new TCPDF , and everything was fine. I donโ€™t know what could go wrong with this reset :) My PDF files still look the same after that, but the emails are sent again.

+10
source

I think I found the culprit. The TCPDF constructor method sets mb_internal_encoding to ASCII . (line tcpdf.php 1838)

I found a hint in the comments: Please note that this method sets the mb_internal_encoding to ASCII, so if you are using the mbstring module functions with TCPDF you need to correctly set/unset the mb_internal_encoding when needed.

But now I need advice on how to use tcpdf and mb_internal_encoding (no problem with the cake or tcpdf).

I'm sorry, I'm completely new.;)

0
source

return the source encoding by extending the TCPDF class and use the new class:

 class TCPDF_repaired extends TCPDF{ public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) { parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa); mb_internal_encoding($this->internal_encoding); } }; 
0
source

All Articles