user3040821
user3040821

Reputation: 31

CakePdf produces strange code instead of pdf

I am using CakePHP 2.4.1. I was able to produce PDFs with dompdf without plugin. Because I need to save them on the server and send via email I downloaded CakePdf from https://github.com/ceeram/CakePdf. I followed all the instructions, added this code to app/Config/bootstrap.php:

CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
    'engine' => 'CakePdf.WkHtmlToPdf',
    'options' => array(
        'print-media-type' => false,
        'outline' => true,
        'dpi' => 96
    ),
    'margin' => array(
        'bottom' => 15,
        'left' => 50,
        'right' => 30,
        'top' => 45
    ),
    'orientation' => 'landscape',
    'download' => true
));

In the controller I added:

public $components = array('RequestHandler');

public function view($id = null) {
    if (!$this->Project->exists($id)) {
    throw new NotFoundException(__('Invalid project'));
    }
    $this->pdfConfig = array(
        'orientation' => 'portrait',
        'filename' => 'Project_' . $id.'pdf'
    );
    $this->set('project', $this->Project->read(null, $id));
}

I created new files app/View/Projects/pdf/view.ctp:

<h2 id="titel">
    <span><?php echo h($project['Project']['name']);?></span>
</h2>           
<table id="container" width="100%" border="0">
    <tr>
        <td>
    <table id="table" width="100%" border="0">
    <tr>
        <td><p>Gebäude:&nbsp;</p><hr></td><td>&nbsp;</td>
        <td><p>Eigentümer:&nbsp;</p><hr></td><td>&nbsp;</td>
        <td><p>Kontaktperson:&nbsp;</p><hr></td>
    </tr>
    <tr>
        <!-- GebaeudeDaten -->
        <td><p>
            <?php echo h($project['Town']['gdenamk']); ?>
            <br><?php echo h($project['Building'][0]['type_id']);?>
            <br><?php echo h($project['Building'][0]['address']);?>
            <br><?php echo h($project['Building'][0]['plz']);?>&nbsp;<?php echo h($project['Building'][0]['ort']);?>
        </p></td><td>&nbsp;</td>
        <!-- EigentuemerDaten -->
        <td><p> 
            <?php echo h($project['Eigentuemer']['Title']['name']);?>
            <br><?php echo h($project['Eigentuemer']['vorname']);?>&nbsp;<?php echo h($project['Eigentuemer']['name']);?>
            <br><?php echo h($project['Eigentuemer']['firma']);?>
            <br><?php echo h($project['Eigentuemer']['adresse']);?>
            <br><?php echo h($project['Eigentuemer']['plz']);?>&nbsp;<?php echo h($project['Eigentuemer']['ort']);?>
        </p></td><td>&nbsp;</td>
        <!-- KontaktDaten -->
        <td><p>                 
            <?php echo h($project['Contact']['Title']['name']);?>
            <br><?php echo h($project['Contact']['vorname']);?>&nbsp;<?php echo h($project['Contact']['name']);?>
            <br><?php echo h($project['Contact']['firma']);?>
            <br><?php echo h($project['Contact']['adresse']);?>
            <br><?php echo h($project['Contact']['plz']);?>&nbsp;<?php echo h($project['Contact']['ort']);?>

        </p></td>
    </tr>
    <tr>
        <td colspan="3"><p><?php echo h($project['Task'][0]['Subject']['name']); ?>&nbsp;<hr></td><td>&nbsp;</td>
        <td><p>ausführender Installateur:&nbsp;</p><hr></td>
    </tr>
    <tr>
        <!-- Daten zum Gegenstand // 3- Zweispaltig -->
        <td colspan="3" align="right">
            <table width="355" border="0">
                <tr><td><p class="right">Datum der Installation:</p></td><td><p><?php echo h($project['Task'][0]['datAusgefuehrt']); ?></p></td></tr>
            </table>
        </td><td>&nbsp;</td>
        <!-- Installateur -->
        <td><p>             
            <?php echo h($project['Task'][0]['Contact']['Title']['name']);?>
            <br><?php echo h($project['Task'][0]['Contact']['vorname']);?>&nbsp;<?php echo h($project['Task'][0]['Contact']['name']);?>
            <br><?php echo h($project['Task'][0]['Contact']['firma']);?>
            <br><?php echo h($project['Task'][0]['Contact']['adresse']);?>
            <br><?php echo h($project['Task'][0]['Contact']['plz']);?>&nbsp;<?php echo h($project['Task'][0]['Contact']['ort']);?>
        </p></td>
    </tr>
    </table>
    </td>
    </tr>
</table>

and app/View/Layouts/pdf/default.ctp:

<?php echo $this->fetch('content'); ?>

I don't get no error messages but the browser doesn't show any pdf but only some code like:

%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R /Names <> >> 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 8 0 R /F2 9 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:20140107065614+00'00') /ModDate (D:20140107065614+00'00') >> endobj 6 0 obj << /Type /Page /Parent 3 0 R /Contents 7 0 R >> endobj 7 0 obj << /Filter /FlateDecode /Length 2532 >> stream x����s7���W�1�IV���Cnz���:��'�\�NF�׉/�䓥���\ �h�k=�^�K��K�y&;)�h�>>���0��ʋ�]�L�Wb�O%T줸����_>�߮�ۻ�k��f��}7�b�݋��as%��A���W���ú�^|����I��l�K�9gN�_�w0�h:a�*6�����n�;^4�!£9��#~؈o��n�\����qy/~Y��]՝r��.4�l���r�Y\��}���z�B�%�;%����;%@t1�h:�B5ܗE�X=sn׃"+@�>϶�}TU��]��q�y-�lК#릋V��M�[ߪ��uR(x��x��s�u9�����:�-g��C����q6e��S�pu�+�v����~��"���i�Mɟ�:?\\,.և��c_/?�W׋���4���?�&�܂�h��Owb B�����P�!�O�w!����g�$��&�N��Y�_Ol5$��E�ML�T�X);�턕oB~��&�j�br 4�8��A��#Xyb�W�?�����������w�M���`:�|Sg�e�h"�@v������Ir}t�]]r��䬰^�Y�yZ��D��J<���X/(��T����<�����ٺ��T�[AvI��N�o����Ed�y�"�ٔ�N���k�e�k�� �$a];d�n�5�\p]]��o~3�H�)p��Q��k�ڸ�o�}�=����hb��u ����M�Rg�iqM��q��N�i@�*68E�qт=ڒ(V�h74�� _�#�:�B�U�뤈� �8����@�Dhc�( �����Ŋ�c�43S�c�hu�+�v��98X_���s�Y��� ��~�E}}ӭ�H���A�������g%k&9�{�|�Ñ�G�}���o�J��~w��8�P5K�� ~Fh���-C����4��;�|C*C�HN2C>���!��!�]L�q�W�H����oU��:)�j(�e6�\���!QI7�BK�y ���"V�h�"�ٔ�N���)���q��0���O��_޾>@����~���9�x=kX1��+�L����;��0�ƭ�lV��U?�4�!�@�4ɍ�u[�A�Й�5Y����9���dY��dYL��+74�;�f�ή�q�����Fbq�r �&sp؅�����oj2g�lo�&�bh|G�rC�9���T�9Z�fZ%$~76=�������w� jfq3����|��}��r}Xb��-��Ĉ#���#Nbd�ߵ�@����@�^�f1�������㭈bň�q#�l���:�@�#&�����(d/�s���uHT� F~'��h[ߋX1�q�gSf�:eW���ʉ����1`�TE=����l��N6 F���Ѩ�O�h� b��7W��!�����$=:���� ����4!k�bHm?AC�k?D+kM�b���q��l���:�Z� ߪhd�O�P��OQ��O�����@aM�'�N_�{+=4N��l�[����W��8�s�Qpʐ���Izl�gӣmi[zP3��Q�DϨ�BO��T~�;�$�8J�9���E9��̯��^h����:ek�"|.�@�t�%(�)W�q+J4���f�f��ZQ!ߪ(]�K�ബ�����K�����d��j/���Ŋ�S�8�2��)s�:�};N������X/�Akn` .����TI�i;� �f�tk��s}^k.urxNcVZ��=���"�|�/Gv)����Sx,�)�l����`S�pO!�1q�l��tlz Y�����$2K<^v3�.���^h��+���yf)+�垂M�RU��%PMO�&��4���[ߋ�,�8E��)3l�2���Lkă��`�&9l̩����f�ۮ����l�N����Ki�/��f�4Lx���`���|k������b|<�I�| l�k��I�kȢ7\Qۨ�אVq�!��r�Nb��qG�l���:A�#/���|�)�?r�!��r�!��5�r ƽ�,��w+O4���l�L�����)���q���<�'�{ o���?n���է&���0�>��1��"k&a�{��u�޵������ �mR\^�vx���5I��߁�Ր5�p��zߙ�uChZ 6����h"��$V�h�1��}^�#�:RB�UQǦ�`��í�,���u3<�J(��VCU�{+E4N��l�[����W��8�s(�M�&)���\����R��Y�:�$�F��`_�z��s����b��o���W�^�����������Eǝ�@a�~j�n��/�>Y�K��m7����U/�N�\���Ys�&�Ԗn�z�K�,F���E�kJ���K�,����lG�h6�W�#��:�K��X��`��gC.ݬɏ)T뙥�t�&f�tˢo}/b��)r�M�a�9\�fZk#$~���t������n���<��Ь��X#��jM�a&Z�L����y;�f�-��Yii���wϏНU]�B�>�:�!���T endstream endobj 8 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont /Times-Bold /Encoding /WinAnsiEncoding >> endobj 9 0 obj << /Type /Font /Subtype /Type1 /Name /F2 /BaseFont /Times-Roman /Encoding /WinAnsiEncoding >> endobj 10 0 obj << /Names [(EmbeddedJS) 11 0 R] >> endobj 11 0 obj << /S /JavaScript /JS ( function traceToggle\(event, id\) { var el = document.getElementById\(id\); el.style.display = \(el.style.display === 'block'\) ? 'none' : 'block'; event.preventDefault\(\); return false; } ) >> endobj xref 0 12 0000000000 65535 f 0000000008 00000 n 0000000103 00000 n 0000000149 00000 n 0000000313 00000 n 0000000342 00000 n 0000000456 00000 n 0000000519 00000 n 0000003124 00000 n 0000003232 00000 n 0000003341 00000 n 0000003393 00000 n trailer << /Size 12 /Root 1 0 R /Info 5 0 R >> startxref 3633 %%EOF

What's going wrong? Any hints are highly appreciated.Thank you very much.

Upvotes: 2

Views: 1018

Answers (2)

user3040821
user3040821

Reputation: 31

Thanks everybody!

I had a mistake in my code - sorry. The wrong layout file was taken. After checking all the view and layout files again, I get a pdf shown in the brwoser with DomPdf. Mpdf and Tcpdf deliver long lists of notices and warnings together with some code similar to the one posted in the question, but not valid pdf. WkHtmlToPdf still misses some libraries.

Upvotes: 0

akirk
akirk

Reputation: 6837

What you see is the proper contents of the PDF file but the browser is displaying it as if it were a HTML file because there is no indication of the content type.

So, in short, to fix this you need to send the HTTP header Content-Type to the browser, indicating that this is a PDF file:

Just ahead of $this->pdfConfig = [...] add a line:

header("Content-type: application/pdf");

Depending on the settings of the browser it will then display the PDF inline.

You can also suggest to the browser that the file is actually intended to be downloaded, using the HTTP header Content-Disposition, like this:

header("Content-disposition: attachment; filename=\"Project_" . $id. "pdf\"");

Upvotes: 4

Related Questions