【问题标题】:conflict utf-8 tcpdf with cakeph 3UTF-8 tcpdf 与 cakeph 3 冲突
【发布时间】:2019-04-09 17:27:00
【问题描述】:

嗨,社区我正在使用插件 CakePdf 和库 tcpdf,生成 pdf 时它显示以下错误

Error:
 Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not      supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69

  Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69

  Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69

 Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69

 Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69

Warning (2): htmlspecialchars() [<a href='http://php.net/function.htmlspecialchars'>function.htmlspecialchars</a>]: charset `ASCII' not supported, assuming utf-8 [CORE\src\Core\functions.php, line 69]

我的配置是这样的

Plugin::load('CakePdf', ['bootstrap' => true]);
Configure::write('CakePdf', [
    'engine' => 'CakePdf.Tcpdf',
    'encoding' => 'UTF-8'
    'download' => true
]);

在我生成pdf的操作中是这样的

public function pdfdo($names = null) {

        $file = new File(WWW_ROOT.'bd/'.'base_datos_do.json');
        $json = $file->read(TRUE,'r');
        $config = json_decode($json,TRUE);
        $this->set('config',$config);
        $persons = explode(',', $names);
        $this->set('lastnames',$persons);
        $this->viewBuilder()->setLayout('ajax');
        $this->viewBuilder()->setTemplate('pdf/pdfdo');
        $this->response->withType('application/pdf');
    }

在我的模板中配置是这样的,也应用函数 mb_internal_encoding ('UTF-8');重置编码,但错误仍然继续

$pdf = new TCPDF('L',PDF_UNIT,PDF_PAGE_FORMAT,TRUE,'UTF-8',FALSE);
$pdf->SetCreator(PDF_CREATOR);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// build my pdf
// finalization of my pdf
mb_internal_encoding('UTF-8');
$pdf->Output('Diplomas-DO.pdf', 'D');
header('Content-Type: application/pdf; charset=utf-8');

请帮我解决几天的问题,谢谢。

【问题讨论】:

    标签: php cakephp-3.0 tcpdf


    【解决方案1】:

    我最近用 TCPDF 制作了一个 pdf,也遇到了同样的问题。看起来您正在直接使用 TCPDF 引擎构建 PDF。

    当 CakePHP 在 PDF 输出开始之前抛出错误时会发生此错误...例如,它可能是“尝试在...中获取非对象的属性”错误或类似的东西。您应该能够在 htmlspecialchars() 警告下方看到特定的错误消息信息。

    我建议先检查以确保您的 pdf 正常工作...而不是您的 //build my pdf code,而是像这样简单的一行

            $pdf->setXY(13, 13);
            $pdf->Write(5, 'Test Hello');
    

    如果可行,那么您的配置正在运行,并且错误可能在您的变量中,因此开始逐个构建您的 pdf,边做边测试。

    我还要补充一点,我还选择了直接使用 TCPDF 引擎,所以我没有使用 CakePDF 插件(效果很好,但不能满足我对这个特定问题的需求)。如果需要,我可以提供更多信息。

    编辑:

    我将提供一些关于我如何在不使用 CakePDF 的情况下直接在项目中使用 TCPDF 的信息,以防您或任何人觉得它有帮助。

    首先,我想直接使用 TCPDF 引擎有几个原因:

    • 精确控制页眉和页脚

    • 能够使用TCPDF的文本缩放、FIT CELL功能

    • 更精确的元素绝对定位

    • 避免使用 CSS。

    所以我直接用composer安装了TCPDF

    composer require tecnickcom/tcpdf
    

    将此添加到 app/vendor/cakephp-plugins.php

    'Tecnickcom/Tcpdf' => $baseDir . '/vendor/tecnickcom/tcpdf/'
    

    然后在 app/config/bootstrap.php 中

    Plugin::load('Tecnickcom/Tcpdf', ['bootstrap' => true]);
    

    然后在 app/config/routes.php 中

    Router::extensions(['pdf']);
    

    然后在 app/src/controller/mycontroller.php 中,我创建了方法 outputpdf。在那个方法中,我设置了所有要在pdf中使用的数据集合,然后

    $this->viewBuilder()->template('mypdf');
    

    然后在 app/src/template/mycontroller/pdf/ 我创建了 mypdf.php。此文件仅包含以下代码:

    header("Content-type:application/pdf");
    
    $this->layout = 'mypdf';
    

    然后在 app/src/template/layout/pdf/ 我创建了文件 mypdf.php。在这个文件中,我使用来自控制器的数据构建了我的 PDF。

    header("Content-type:application/pdf");
    
    // Extend the TCPDF class to create custom Header and Footer
    
    class MYPDF extends TCPDF {
    //And build the header and footer in here
    }
    
    
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    //And make all the body content here
    
    $pdf->Output('mypdf.pdf', 'I');
    

    这种方法的一个缺点是使用外语字体,您需要在 app/vendor/tecnickcom/tcpdf/fonts 文件夹中添加和使用所需的字体,而这些都是您的 pdf 可用的字体。

    请随时对这种方法的改进提出批评或建议。

    【讨论】:

    • 感谢您的回答,在插件直接使用类之前很好,因为我认为错误可能是因为直接使用迁移到插件但持续错误
    【解决方案2】:

    我发现错误在于我在 pdf 中使用的图像,其中一张我用作 pdf 的背景,另一张像小图像。

    $pdf = new TCPDF('L',PDF_UNIT,PDF_PAGE_FORMAT,TRUE,'UTF-8',FALSE);
    $pdf->SetCreator(PDF_CREATOR);
    
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $fontname = TCPDF_FONTS::addTTFfont(WWW_ROOT.'font'.DS.'Mada'.DS.'Mada-Regular.ttf', 'TrueTypeUnicode', '', 96);
     $tagvs = array(
        'div'=> array(
            0 => array('h'=>0,'n' => 0),
            1 => array('h'=>0,'n' => 0)),
        'p'=> array(
            0 => array('h'=>0,'n' => 0),
            1 => array('h'=>0,'n' => 0)),
        'h2' => array(
            0 => array('h'=>0,'n' => 0),
            1 => array('h'=>0,'n' => 0)),
        'img' => array(
            0 => array('h'=>0,'n' => 0),
            1 => array('h'=>0,'n' => 0)
        )
         );
           //variable that has small image
           $imglogo = WWW_ROOT.'logos'.DS.'logoempresa.png';
            foreach ($lastnames as $names) {
        $pdf->AddPage();
        $bMargin = $pdf->getBreakMargin();
        $auto_page_break = $pdf->getAutoPageBreak();
        $pdf->SetAutoPageBreak(false, 0);
        //image for background
        $img = WWW_ROOT.'img'.DS.'Diploma_DO.png';
        $pdf->Image($img, 0, 0, 300, 210, 'png', '', '', false, 600, '', false, false, 0);
    
        $pdf->SetAutoPageBreak($auto_page_break, $bMargin);
        $pdf->setPageMark();
        $pdf->setHtmlVSpace($tagvs);
        $html_title =  '<table  cellspacing="0">'
                . '<tr style="text-aling:center;line-height:11px">'
                . '<td style="font-size: 37pt;font-weight: 600;color: #034bdb;color:#003275">'.$names.'</td>'
                . '</tr>'
                . '</table>';
        $html_text_content = '<div style="text-align: center">'
                . '<p style="color:#333;font-size: 16px;text-align: center">Ha completado con éxito el '.$config["Nombre-Taller-Curso"].',</p>'
                . '<p style="color:#333;font-size: 16px;text-align: center">efectuada el '.$config["Fecha-Inicio-Fin"].' de '.$config["Mes-Ano"].' con una duración de '.$config["Horas"].' Horas.</p>'
                . '</div>';
        $html_text_content_bussines = '<div style="text-align: center">'
                . '<p style="color:#333;font-size: 16px;text-align: center">Este taller ha sido diseñado especialmente para '.$config["Empresa"].'.</p>'
                . '</div>';
        $html_text_content_close = '<div style="text-align: center">'
                . '<p style="color:#333;font-size: 16px;">'.$config["Fecha-Curso-Ubicacion"].'</p>'
                . '</div>';
    
        //img tag that contains the small image
        $html_logo_bussines = '<img src="'.$imglogo.'" width="150" height="100">';
    
        $pdf->SetFont($fontname, 'B', 26, '',false);
        $pdf->writeHTMLCell(300,0,0,78,$html_title, '', 1, 0, true, 'C',true);
        $pdf->SetFont($fontname,'',14,'',false);
        $pdf->writeHTMLCell(300, 0, 0, 88, $html_text_content, '', 1, 0, true, 'C', true);
        $pdf->writeHTMLCell(300, 0, 0, 109, $html_text_content_bussines, '', 1, 0, true, 'C', true);
        $pdf->writeHTMLCell(300,0,0,125,$html_text_content_close,'',1,0,true,'C',true);
    
        //use of the small image
        $pdf->writeHTMLCell(300,0,0,155,'<div style="text-align:center">'.$html_logo_bussines.'<div>',0,0,0,true,'C',true);
        $pdf->lastPage();
    }
    

    错误继续,当我注释所在行时,错误停止显示

    $pdf->writeHTMLCell(300,0,0,155,'<div style="text-align:center">'.$html_logo_bussines.'<div>',0,0,0,true,'C',true);
    

    如果您接受 img 标签,我不知道我做错了什么我阅读了文档和此功能。

    【讨论】:

      【解决方案3】:

      你能调试一下你的 $imglogo 变量看看文件路径是否正确吗?

      或者,尝试使用 $pdf->Image() 函数显示图像?

      请注意,TCPDF 在 vendor\tecnickcom\tcpdf\config\tcpdf_config.php 中有一个配置选项:

      define ('K_PATH_IMAGES', 'C:\\windowsfolder\\htdocs\\app\\webroot\\img\\');
      

      因此您可以通过以下方式调用 PDF 中的图像:

      $image_file = K_PATH_IMAGES.'imagefile.jpg';
      

      看看有没有用……

      【讨论】:

        猜你喜欢
        • 2011-07-17
        • 2015-11-30
        • 2014-08-10
        • 2019-02-26
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 2014-05-14
        • 1970-01-01
        相关资源
        最近更新 更多