fpdf with cakephp

Asked

Viewed 371 times

1

I am using the library in fpdp cakephp, created a private method in my controller by mounting a basic pdf and then called this method in an action but it does not generate pdf..

private method:

private function montaPDF(){
    require('fdpf/fpdf.php');
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
}

and returns this error on my page:

%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x 3R 2 35W(�r Q�w3T04�30PISp �Z*�[����(hx����+���(j*�d��7W endstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 5 0 R >> /XObject << >> >> endobj 6 0 obj << /Producer (FPDF 1.7) /CreationDate (D:20150824102520) >> endobj 7 0 obj << /Type /Catalog /Pages 1 0 R >> endobj xref 0 8 00000000000000 65535 f 0000000228 00000 n 0000000416 00000 n 0000000009 00000 n 0000000087 00000 n 0000000315 00000 n 0000000520 00000 n 0000000595 00000 n trailer << /Size 8 /Root 7 0 R /Info 6 0 R >> startxref 644%EOF

  • Have you checked if it is not rendering the view? try $this->autoRender = false

  • yes, it is with autoRender = false;

1 answer

0

1 - Create a layout for example "pdf.ctp" in /views/layoyts

<?php
header("Content-type: application/pdf"); 
echo $content_for_layout;
?>

2 - In the controller, use the layout above.

3 - In the view, make sure to use the TCPDF with the correct encoding.

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

Link: http://www.tcpdf.org/doc/code/classTCPDF.html#a134232ae3ad1ec186ed45046f94b7755

Browser other questions tagged

You are not signed in. Login or sign up in order to post.