2013-04-16 129 views
2

我使用CakePHP 2.x與tcpdf創建PDF文件。我想現在輸出到瀏覽器,而不保存。CakePhp 2.x TCPDF輸出Chrome

佈局 - > pdf.ctp

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

查看 - > 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'); 

?> 

對於Internet Explorer能正常工作和PDF顯示出來。

有了Chrome我只得到非常用戶友好的輸出,如:

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

甚至當我將它設置爲

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

將其保存爲文件,我仍然得到一個「內容長度:20」和,對於Inline選項「I」,我總是得到Content-Type HTML/Text而不是Application/pdf。

任何想法非常讚賞。

在此先感謝。

回答

5

不是包括在佈局文件中header電話,請添加以下代碼到你的控制器的方法:

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

蛋糕送出頭時,所以你不應該在您的視圖直接包括他們這是準備/佈局文件。如果你想設置你應該使用響應的頭方法的標題,例如:

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

我不知道這是否會解決您的問題,我無法測試它,但我認爲它有一個好球。

+0

你是天才...工程就像現在的魅力:d –

+1

很高興我能幫助! – Hoff

+0

這幫了我很多。謝謝 –

0

我有同樣的問題,通過禁用自動CakePHP中控制器的方法使固定它:

$this->autoRender = false;