php - CakePhp 3.x、TCPDF、htmlspecialchars

标签 php cakephp cakephp-3.0 tcpdf

我已经按照文档安装了插件“CakePDF”:https://github.com/FriendsOfCake/CakePdf

现在我想构建第一个 PDF,但出现以下错误:

enter image description here

这是我在 bootstrap.php 中的配置:

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

我编写的唯一代码是模板中的以下代码:

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

这是 functions.php 中第 68 行的代码:

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);
    }

我非常困惑,找不到任何解决方案。有人有想法吗?

最佳答案

在尝试查找和调试相同错误一个多小时后,我只是在使用 TCPDF 后将值重置为 UTF-8 - 一切正常之前:

$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');

我也尝试在 new TCPDF 调用后直接重置它,一切都很好。我不知道这次重置会出什么问题 :) 我的 PDF 在此之后看起来仍然一样,但电子邮件会再次发送。

关于php - CakePhp 3.x、TCPDF、htmlspecialchars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34995422/

相关文章:

php - MySQL查询结果按查询顺序

php - CakePHP - 只是布局?

PHP - 以某种方式散列对象具有相同字段值的不同对象具有相同的散列

php - 不会更新数据库中的记录

php - 从命令行运行 Gearman PHP 的段错误

php - 为什么 mysqli 给出 "Commands out of sync"错误?

php - cakephp - 修改和保存数据

mysql - CakePHP 3 - 两个外键链接到同一个表 - 使用了错误的外键

php - 如何修改与auth相关的flash消息的外观?

javascript - 使用 cakePHP 3 将 JSON 对象保存为具有关联的实体