Michael Grenzer
Michael Grenzer

Reputation: 501

CakePhp 2.x TCPDF Output Chrome

I am using CakePHP 2.x with tcpdf to create a PDF file. I want to output it now to the browser, without saving.

Layout->pdf.ctp

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

View->pdf_testing.ctp

<?php
App::import('Vendor', 'xtcpdf');
$pdf = new XTCPDF('P', 'mm', 'USLETTER', true, 'UTF-8', false);

$textfont = 'freesans'; // looks better, finer, and more condensed than 'dejavusans'

$pdf->AddPage();

$pdf->setHeaderData('', '', '', 'RMA#100000');
$pdf->SetTitle('Some Text');
$pdf->SetHeaderMargin(20);
$pdf->SetTopMargin(40);
$pdf->setFooterMargin(20);
$pdf->SetAutoPageBreak(True, PDF_MARGIN_FOOTER);
$pdf->SetAuthor('Any Author');
$pdf->SetDisplayMode('real', 'default');

$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont($textfont, 'B', 20);
$pdf->Cell(0, 14, "TESTING", 0, 1, 'L');
echo $pdf->Output('filename.pdf', 'I');

?>

For Internet Explorer this works fine and the PDF shows up.

With Chrome i get only very userfriendly output like:

%PDF-1.7 %���� 10 0 obj << /Type /Page /Parent 1 0 R /Las....

Even when i set it to

echo $pdf->Output('filename.pdf', 'F');

to save it as file i still get a "Content Length:20" and, with option "I" for Inline, i always get Content-Type HTML/Text instead of Application/pdf.

Any ideas are very appreciated.

Thanks in advance.

Upvotes: 2

Views: 3476

Answers (2)

Yaroslav
Yaroslav

Reputation: 3217

I have same issue and fixed it by disabling auto render in cakephp controller method:

$this->autoRender = false;

Upvotes: 0

Hoff
Hoff

Reputation: 1772

Rather than including the header call in your layout file, try adding the following code to your controller method:

$this->response->type('application/pdf');

Cake sends out headers when it's ready so you shouldn't include them directly in your view/layout files. If you want to set a header you should use the response's header method, for example:

$this->response->header('Location', 'http://example.com');

I'm not sure if this will fix your problem as I can't test it, but I think it has a good shot.

Upvotes: 5

Related Questions