DOMPDF产生胡言乱语

时间:2015-03-15 05:15:10

标签: cakephp dompdf

我已按照http://www.syahzul.com/cakephp/how-to-generate-pdf-in-cakephp-2-x-with-dompdf/上的说明操作并安装了DOMPDF版本0.6.1。 当我在视图页面上按PDF按钮时,显示出乱码。 (见下文)。我已经检查过这些字体是否安装在类库中。 有什么建议吗?

第2步(在/app/Config/routes.php

    Router::parseExtensions('pdf');

第3步(在ChecksController.php中)

    class ChecksController extends AppController {

    public $components = array('Paginator', 'Session', 'RequestHandler');

第4步(在/app/View/Layouts/pdf/default.ctp中)

require_once(APP . 'Vendor' . DS . 'dompdf' . DS 
.dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper = 'A4';
$dompdf->load_html(utf8_decode($content_for_layout), Configure::read('App.encoding'));
$dompdf->render();
echo $dompdf->output();

第5步(在ChecksController.php中)

public function view_pdf($id = null) {
$this->Check->id = $id;
if (!$this->Check->exists()) {
    throw new NotFoundException(__('Invalid Check'));
}
// increase memory limit in PHP
ini_set('memory_limit', '512M');
$this->set('check', $this->Check->read(null, $id));

第6步(在check \ app \ View \ Checks \ pdf \ view_pdf.ctp中)

<div class="checks view">
<h2><?php echo __('Check Flight'); ?></h2>
<?php //echo debug($check); ?>
<dl>
    <dt><?php echo __('Instructor'); ?></dt>
    <dd>
        <?php echo h($check['Instructor']['first'] . ' ' . $check['Instructor']['last']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Pilot'); ?></dt>
    <dd>
        <?php echo h($check['Person']['first'] . ' ' .  $check['Person']['last']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Flight Date'); ?></dt>
    <dd>
        <?php echo h($check['Check']['flight_date']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Check Type'); ?></dt>
    <dd>
        <?php echo h($check['Check']['check_type']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Aircraft'); ?></dt>
    <dd>
        <?php echo h($check['Aircraft']['registration']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Hours'); ?></dt>
    <dd>    <?php echo h($check['Check']['hours']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Conditions'); ?></dt>
    <dd>
        <?php echo h($check['Check']['conditions']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Passed'); ?></dt>
    <dd>
        <?php echo h($check['Check']['passed']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Notes'); ?></dt>
    <dd>
        <?php echo h($check['Check']['notes']); ?>
        &nbsp;
    </dd>
    <div class='items'>
        <dt><?php echo __('Additional Areas'); ?></dt>
        <dd>
            <?php foreach ($check['Assessment'] as $assessment): ?>
                    <?php  echo h($assessment['description']); ?><br/>
            <?php endforeach; ?>
        </dd>
     </div>   
</dl>

第7步(在app \ View \ Checks \ view.ctp中)

    <li><?php echo $this->Html->link(__('PDF'), array('action' => 'view_pdf', 'ext' => 'pdf', $check['Check']['id']));?></li>

输出:

%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R /OpenAction 8 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 9 0 R /F2 10 0 R >> >> /MediaBox [0.000 0.000 612.000 792.000] >> endobj 4 0 obj [/PDF /Text ] endobj 5 0 obj << /Creator (dompdf) /CreationDate (D:20150315060510+01'00') /ModDate (D:20150315060510+01'00') >> endobj 6 0 obj << /Type /Page /Parent 3 0 R /Contents 7 0 R >> endobj 7 0 obj << /Filter /FlateDecode /Length 349 >> stream x�m��k�0���W�c�D�ڷ�ha��>��C�H�5�5�����Ӵ}I�K>�~sw����a�wq�d�@�������s��6�i ��1� �/�$",�ȸ?`��s�E��4bL�vqϯa$�3�#��g��xJ�����T�+�A�U�jSA�K�?wѧ=ʦ�[]�y���"0���?���|x��(lfd+�,��������PUf!2s����數r}@]I��z��>�$�:|��x됣�qpFf�\�|>��9�KS§nލ��֏ �#���y���tpJ+;�Mv�5G����5LUۦu�Q�Up.�V'!܈��͇yܻ�(m}q��e� endstream endobj 8 0 obj [6 0 R /Fit] endobj 9 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont /Times-Bold /Encoding /WinAnsiEncoding >> endobj 10 0 obj << /Type /Font /Subtype /Type1 /Name /F2 /BaseFont /Times-Roman /Encoding /WinAnsiEncoding >> endobj xref 0 11 0000000000 65535 f 0000000008 00000 n 0000000091 00000 n 0000000137 00000 n 0000000302 00000 n 0000000331 00000 n 0000000445 00000 n 0000000508 00000 n 0000000929 00000 n 0000000957 00000 n 0000001065 00000 n trailer << /Size 11 /Root 1 0 R /Info 5 0 R >> startxref 1175 %%EOF 

1 个答案:

答案 0 :(得分:0)

它可能是这一行:

$dompdf->load_html(utf8_decode($content_for_layout), Configure::read('App.encoding'));

$ content_for_layout已弃用:请改用$this->fetch('content')