Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
Leyla Avatar asked Jan 25 '16 14:01

Leyla


1 Answers

After trying to find and debug the same error for over an hour, I just reset the value to UTF-8 after the usage of TCPDF - and everything works like 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 directly after the call of new TCPDF and everything was fine, too. I don't know what could go wrong with this reset :) My PDFs still look the same after this but emails get send again.

like image 89
lorem monkey Avatar answered Sep 20 '22 01:09

lorem monkey