Reputation: 2073
I have a problem with Cakepdf. I can generate .pdfs to my server, but can´t view them in my browser.
$CakePdf = new \CakePdf\Pdf\CakePdf();
$CakePdf->template('view', 'default');
$CakePdf->viewVars($this->viewVars);
// Get the PDF string returned
$pdf = $CakePdf->output();
echo $pdf;
Just gives me cryptic code like
%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R 14 0 R ] /Count 2 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R /F2 9 0 R >> /XObject << /I1 12 0 R /I2 13 0 R /I3 18 0 R /I4 19 0 R >> >> /MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/PDF /Text /ImageC ] endobj 5 0 obj << /Creator (DOMPDF) /CreationDate (D:20160208165451+00'00') /ModDate (D:20160208165451+00'00') >> endobj 6 0 obj << /Type /Page /Parent 3 0 R /Annots [ 10 0 R ] /Contents 7 0 R >> endobj 7 0 obj << /Filter /FlateDecode /Length 819 >> stream x�}Uˎ�8��+������`O�d$H�<��,�@K�M�5|L�|}���� �-QbwUuus�眶�����HuÚ��ڪc5/�8����t<���g9�Ɂ��p�A�Eԁ��ncx�����M�������"���HEnW]��k�.�Z�;~�������(}xo�4�& V[G^F�=�v�� C��Ġ&�{5^Hj}�ɝ���6� ����rp��U��bUQd�h䊞�u��iP�є� ڀe�'��T��'/��B��K'<@J#�Z94e�ʮLh V�u��%�B�rD\�.Q2�{���0��K3A&hv�rO2*o��О�b��Y5f�k*�`i�2�e�$AͪX�=��|'c5ʨR�Z�:QWg�y�s��ҭZ$2Y�,jM}װ��KhJVWUM*I'��W`�2_o����8���>JzPA��R�QR�er2(�»9(]ɪ�Y�)rN�!�����h)��)�bNz�:� I�:Igi� dv�����t_�`�'�C�����JX�c{{{���:�qK�m>�O�5Ku��6a�X%�?f�2V 2n �$� kڸ�o��y����_�Y� endstream endobj xref 0 20 0000000000 65535 f 0000000009 00000 n 0000000074 00000 n 0000000120 00000 n 0000000351 00000 n 0000000388 00000 n 0000000502 00000 n 0000000584 00000 n 0000001475 00000 n 0000001587 00000 n 0000001694 00000 n 0000001821 00000 n 0000001895 00000 n 0000002860 00000 n 0000003940 00000 n 0000004024 00000 n 0000004307 00000 n 0000004434 00000 n 0000004508 00000 n 0000005473 00000 n trailer << /Size 20 /Root 1 0 R /Info 5 0 R >> startxref 6553 %%EOF
I have my default.ctp in Layout/pdf/default.ctp and my view in my controller. Simply won´t create an pdf online. Any ideas?
Upvotes: 0
Views: 1261
Reputation: 35
Here a short pdf preview example for CakePhp 4.3 with CakePdf 4.1 which works for me:
Invoice Controller:
public function pdfPreview() {
$invoice = $this->Invoices->get(1);
$CakePdf = new CakePdf();
$CakePdf->template('view','invoiceLayout');
$CakePdf->viewVars(['invoice' => $invoice]);
$pdf = $CakePdf->output();
//Or write it into a file if you want to
//$CakePdf->write(APP . 'files' . DS . 'invoice.pdf');
$this->response = $this->response->withType('pdf');
$this->viewBuilder()->setLayout('pdfPreview');
$this->set(['pdf' => $pdf]);
}
Layout "invoiceLayout":
Here belongt your Html design for your invoice pdf
<?= $this->fetch('content') ?>
Template "view"
Here belongs the content, which should be loaded into "invoiceLayout" For example products
foreach ($products as $product) {
echo $product
}
Layout "pdfPreview":
<?php
echo $pdf;
?>
Hope this might help others.
Upvotes: 0
Reputation: 60463
What you have there is just regular PDF source content, and the fact that you are seeing it, instead of the browser recognizing it as a PDF document, is because you aren't sending a proper Content-Type
header.
What I ment with "use the plugin as shown in the docs" in my comment, is the method that does that for you automatically, that is using the PDF view + Request Handler variant.
Readme > Render as PDF (including forced download) in the browser with PdfView (also check https://github.com/FriendsOfCake/CakePdf/issues/147)
If you don't do that, then you need to handle that on your own. In a controller action for example, you'd use the response object to set the body content and the required type/header, like
// ...
$pdf = $CakePdf->output();
$this->response->body($pdf);
$this->response->type('pdf');
return $this->response;
See also Cookbook > Request & Response Objects > Response
Upvotes: 1