dompdfcakephp-2.7

Generate pdf files with dompdf engine in cakepdf plugin in CakePHP


I am using CakePHP 2.7.3 and CakePdf 1.0.8. Am trying to generate pdf files for my Cake app, but the pdf I get just has some weird characters.
This is how the generated pdf looks like:

%PDF-1.3 1 0 obj > endobj 2 0 obj > endobj 3 0 obj > >> /MediaBox [0.0000.000 612.000 792.000] >> endobj 4 0 obj
[/PDF /Text ] endobj 5 0 obj > endobj 6 0 obj > endobj 7 0 obj > stream xœí™KoÛ8…÷ý\Îl8"ÅçÒq’¾•¤¨ ÌZµU[-,’… óë‡I\•Ýãôn¡‘@ˆÎ-ïˆä¥ò.“Y–‰ñïnóîb)Üó±wVj•‰åZüu-„
2•â•‹¶ýÙ‹y±/êv3”•Šåqµ|6f«WR;ód}9¤¶éÏ:H¯íñŠú×?®GI^—¼P|[ìÆ}*ïdëgÃ~Ûv#‡¶Jšxæë,†o?ÊÕ~dÉ“4×çî`qq;Ö‡\Z•æ&æí}Uöã»–ö
œÊd®Ü‹^•ôùøû+ÌíUSîEû]ì·¥¸/Ûûú„—Õ2z‡í·›aS•»Ë£úl•›ºê·ãÛÏò„Ë`‹Ò¹±Î‡˜¥£n:õuƘO•¹¨¥7„ÔL‘AílÕµ}ÿ좫Ö
4Ϸî%€AÃû²ÝtÅýö‘@MÎ[“k³•&•í™·Ry{^úÒ~©ÊU)¾=ŠçŠr~.ëúQPÀ•c¶éªÕP¤•!›wá×ÓƒF!ûdÖK—Q}È•v9t•îŸ³ÝÐW+Š2ï‹nè)hÈðeû˜ºè)`ÈbðÇ 2º)0“V”ÜCŸ˜Ê
ø}šëŸf²å¶k›“ùòÈY?µÛF,šö•b†Lóm¹«ú}GŽLdòãFQCFMÌfZKã(lŽÀ†Ä‹•˜-öCÑUE-«ªlè!Š.0/º¶·?ÊfSPü•ï¦ØSë  

This is my view file View/Books/pdf/books_catalogue.ctp

<h2><?php echo __('Books Catalogue'); ?></h2>
<table cellpadding="0" cellspacing="0">
    <thead>
    <tr>
        <th><?php echo 'Id'; ?></th>
        <th><?php echo 'Name'; ?></th>
        <th><?php echo 'Author'; ?></th>    
    </tr>
    </thead>
    <tbody>
    <?php foreach ($books as $book): ?>
    <tr>
        <td><?php echo h($book['Book']['id']); ?></td>
        <td><?php echo h($book['Book']['name']); ?></td>
        <td><?php echo h($book['Book']['author']); ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>  

My pdf layout: View/Layouts/pdf/default.ctp

<?php  
require_once(APP . 'Plugin' .DS. 'CakePdf' .DS. 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');
    spl_autoload_register('DOMPDF_autoload');
    $dompdf = new DOMPDF();
    $dompdf->load_html(utf8_decode($content_for_layout),Configure::read('App.encoding'));
    $dompdf->render();
    echo $dompdf->output();
    $dompdf->stream("Books Catalogue.pdf", array('Attachment'=>'0'));
?>  

My Config/bootstrap.php

CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));

    Configure::write('CakePdf', array(
        'engine' => 'CakePdf.DomPdf',
        'pageSize' => 'A4',
        'orientation' => 'landscape',
        'download' => true
    ));  

And my controller:

public function booksCatalogue(){
    header("Content-type: application/pdf");
    $this->pdfConfig = array(
        'orientation' => 'portrait',
        'filename' => 'Books Catalogue'
    );
    $books = $this->Book->find('all');
    $this->set(compact('books'));
}  

I have googled and tried to find some answers to no avail. Kindly Assist.


Solution

  • What you're seeing is the source of the PDF document. So your PDF is generating correctly but somewhere along the line the content type sent to the browser is set to text/html (it should be application/pdf). You're using the stream() method in your layout which will set the header appropriately (so you don't need to additionally set it in your controller).

    dompdf does not exit() after sending the PDF to the client and so you might have output buffering enabled and CakePHP is doing something somewhere to set the content type after the PDF is generated. If this is the case you could just add an exit() to the end of your layout.

    Better, however, might be to configure content-type/extension support in CakePHP. It's been a while since I've done this in CakePHP but I believe you would need to do some router and RequestHandler configuration.