Leyla Leyla - 1 year ago 321
PHP Question

CakePhp 3.x, TCPDF, htmlspecialchars

I´ve installed the plugin "CakePDF" following the documentation: https://github.com/FriendsOfCake/CakePdf

Now I want to build the first PDF and I´ve got the following error:

enter image description here

This is my configuration in the 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´ve written is the following one 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´m absolutely confused and can´t find any solution. Has anyone an idea?

ndm ndm
Answer Source

As you've figured, the problem is/was that the TCPDF class messes with mb_internal_encoding(), which is used in CakePHPs h() function to determine the apps default encoding, in case no explicit one is passed as an argument.

I'm no TCPDF expert, haven't used it in ages, but from a quick look at the current source, I'm having a hard time to understand why it fiddles with the internal encoding at all, since the only mb_* function the library uses, is mb_convert_encoding(), where both encoding arguments are passed, so the internal encoding isn't being used at all. That being said, I'd report this as a bug/issue to the TCPDF developer(s).

Anyways, you are only experiencing this problem because you are using CakePDF the wrong way. There is no need to manually create PDF engine instances, that's what CakePDF automatically does for you, which is pretty much the whole point of the plugin, it abstracts the PDF creation so that you just need to build the proper HTML in your view templates. That way you'll also avoid the encoding problems with TCPDF that you are currently experiencing, as the view template is being rendered before the PDF engine instance is being created.


Long story short, build only the HTML in your view template, and if you actually need to have access to a PDF engine instance because there's something you need to do that can only be achieved that way, then CakePDF is not the plugin you are looking for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download