douque9982 2019-04-09 17:27
浏览 63

与cakephp冲突utf-8 tcpdf 3

Hi community I'm using plugin CakePdf with the library tcpdf and when generating the pdf it shows me the following error

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]

my configuration is like this

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

within my action which generates the pdf is this way

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

inside my template the configuration is this way, also apply the function mb_internal_encoding ('UTF-8'); to reset the enconding but still the error continues

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

please help I go several days with the problem thanks.

  • 写回答

3条回答 默认 最新

  • dsbezji539113152 2019-04-10 00:36
    关注

    I recently made a pdf with TCPDF and had the same problem. It looks like you're building your PDF with the TCPDF engine directly.

    This error happens when CakePHP throws an error before the PDF output can begin... for example, it could be a "Trying to get a property of a non-object in...." error or something like that. You should be able to see the specific error message info below the htmlspecialchars() warnings.

    I suggest checking to make sure your pdf is working correctly first... instead of your //build my pdf code, make a simple line like

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

    If that works, then your configuration is working and the error is likely in your variables somewhere, so start building your pdf piece by piece, testing as you go.

    I'll also add that I also chose to use the TCPDF engine directly, so I didn't use the CakePDF plugin (which works great but didn't meet my needs for this particular problem). I can provide more info on this if needed.

    EDIT:

    I'll provide some info on how I used TCPDF directly in my project without CakePDF in case you or anyone finds it helpful.

    First, I wanted to use TCPDF engine directly for a few reasons:

    • Precise control of headers and footers

    • Able to use the text scaling, FIT CELL functions of TCPDF

    • more precise absolute positioning of elements

    • avoid CSS.

    So I installed TCPDF directly with composer

    composer require tecnickcom/tcpdf
    

    Added this to app/vendor/cakephp-plugins.php

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

    Then in app/config/bootstrap.php

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

    Then in app/config/routes.php

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

    Then in app/src/controller/mycontroller.php, I created the method outputpdf. In that method, I set all the data collections to be used in the pdf, then

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

    Then in app/src/template/mycontroller/pdf/ i created the mypdf.php. This file contains only this code:

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

    Then in app/src/template/layout/pdf/ I created the file mypdf.php. In this file I built my PDF with the data from the controller.

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

    One downside with this approach is with foreign language fonts, you need to add and use the fonts you need in the app/vendor/tecnickcom/tcpdf/fonts folder, and those are all that are available for your pdf.

    Please feel free to critique or advise on improvements to this approach.

    评论

报告相同问题?