2010-02-15 54 views
0

我有一個網站在該用戶需要採取報告爲PDF [在代碼],我試着下面的代碼,堅果它顯示一些特殊字符,請幫我解決這個問題。打印數據爲PDF使用PHP

<?php 
require('fpdf.php'); 

//create a FPDF object 
$pdf=new FPDF(); 

//set document properties 
$pdf->SetAuthor('Lana Kovacevic'); 
$pdf->SetTitle('FPDF tutorial'); 

//set font for the entire document 
$pdf->SetFont('Helvetica','B',20); 
$pdf->SetTextColor(50,60,100); 

//set up a page 
$pdf->AddPage('P'); 
$pdf->SetDisplayMode(real,'default'); 

//insert an image and make it a link 
$pdf->Image('logo.png',10,20,33,0,' ','http://www.fpdf.org/'); 

//display the title with a border around it 
$pdf->SetXY(50,20); 
$pdf->SetDrawColor(50,60,100); 
$pdf->Cell(100,10,'FPDF Tutorial',1,0,'C',0); 

//Set x and y position for the main text, reduce font size and write content 
$pdf->SetXY (10,50); 
$pdf->SetFontSize(10); 
$pdf->Write(5,'Congratulations! You have generated a PDF.'); 

//Output the document 
$pdf->Output('example1.pdf','I'); 
?> 

我得到了輸出,如下 克endstream endobj 1 0 OBJ <> endobj 5 0 OBJ <> endobj 2 0 OBJ < </ProcSet [/ PDF /文本/ ImageB/ImageC/ImageI] /字體< </F1 5 0 R >>/XObject < < >> >> endobj 6 0 obj < </Producer(FPDF 1.6)/ Title(FPDF tutorial)/作者(Lana Kovacevic)/ CreationDate(D:20100215182640)>> endobj 7 0目標< < /類型/目錄/頁面1 0 R/OpenAction [3 0 R/FitH null]/PageLayout/OneColumn >> endobj xref 0 8 0000000000 65535 f 0000000407 00000 n 000 0000595 00000ñ0000000204 00000ñ0000000282 00000ñ0000000494 00000ñ0000000699 00000ñ0000000822 00000ñ拖車< < /尺寸8 /根7 0 R /信息6 0 R >> startxref 925 %% EOF

+0

歡迎來到SO。請給出更詳細的問題描述,例如通過顯示輸出什麼特殊字符。 – 2010-02-15 17:24:06

+0

是的,看起來你需要添加一個'content-type'頭文件。請參閱下面的Cryo答案。 – 2010-02-15 17:53:48

回答

1

最有可能的,你只需要將內容類型指定爲PDF,以便瀏覽器知道如何處理文件。

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

一定要調用以前你叫$pdf->Output('example1.pdf','I');

這個問題也可能造成其被送到正確的瀏覽器信息,但安裝an application that understands PDFs沒有。

+0

我認爲FPDF已經在Output()中實現了,但我可能弄錯了。 – 2010-02-15 17:25:12

+0

啊,可能是你說得對,沒有標題輸出的指示。 http://www.fpdf.org/en/doc/output.htm – 2010-02-15 17:26:12

+0

@Pekka我認爲,但他的問題聽起來像他正在獲得PDF的文本輸出。 – nortron 2010-02-15 17:26:13

0
$pdf->Output('example1.pdf','I'); 

將pdf保存到文件。如果您想直接在瀏覽器中顯示PDF(或打開下載對話框,這取決於瀏覽器和插件),你應該做的:

$pdf->Output(); 
+0

實際上,第二個參數指定文件的發送位置,'I'表示'內嵌到瀏覽器':http://www.fpdf.org/en/doc/output.htm – nortron 2010-02-15 17:28:35

+0

你說得對,我只有looged到我的應用程序的源代碼中:/ – skyman 2010-02-15 17:56:04

0

Output(..., 'I')應該處理的HTTP標頭爲你 - 除非你使用PHP- CLI。

FDPF::Output取決於php_sapi_name返回'cli'當目標'I'。它仍然迴應緩衝區,但沒有指定正確的標題。

如果您使用的是FPDF 1.6,請參閱第1010-1027行, 1014,fpdf.php