2015-10-13 71 views
1

當使用FPDF庫生成PDF文件時,我得到了這個輸出。使用FPDF PHP時奇怪的字符?

%PDF-1.3 3 0 OBJ <> endobj 4 0 OBJ <>流 xUn0 <ˁ78' !ZQ( U〜 !B 8 o e l e & l tʙ :Cl k|| p |K e 「-9BWj $ FVtq3toXrlQP%ncXB_!S1中 + B )וו I (m HVendstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 2 0 obj < </ProcSet [/ PDF /文本/ ImageB/ImageC/ImageI] /字體< </F1 5 0 R >>/x對象< < >> endobj 6 0 OBJ < < /生產 (FPDF 1.7)/ CreationDate(d:20151013130538)>> endobj 7 0 OBJ < < /類型 /目錄/頁1個0 R >> endobj外部參照0 8 0000000000 65535˚F0000000354 00000ñ0000000542 00000ñ0000000009 00000ñ0000000087 00000ñ 0000000441 00000ñ0000000646 00000ñ0000000721 00000ñ拖車< < /尺寸8/7根0 R/Info 6 0 R >> startxref 770 %% EOF

我的代碼:

header("Content-Type: application/pdf"); 

$pdf = new FPDF(); 

$pdf->AddPage(); 

$pdf->SetFont('Arial','B',16); 

$pdf->Cell(40,10,'Hello World!'); 

$pdf->Output(); 


**when i check the header response this is what i get:** 

Cache-Control:private, max-age=0, must-revalidate 

Connection:Keep-Alive 

Content-Disposition:inline; filename="doc.pdf" 

Content-Encoding:gzip 

Content-Length:708 

Content-Type:text/html;charset=UTF-8 

Date:Tue, 13 Oct 2015 17:17:47 GMT 

Expires:Thu, 19 Nov 1981 08:52:00 GMT 

Keep-Alive:timeout=5, max=100 

Pragma:public 

Server:Apache/2.4.10 (Ubuntu) 

Set-Cookie:PHPSESSID=q20auj7ssdj2c1obhbfqu8ha85; path=/ 

Vary:Accept-Encoding 

回答

2

下面的頭文件和輸出命令是我目前與FPDF使用:

// Set a filename 
$filename = "AppName_Day_".$day1."_gen_".date("Y-m-d_H-i").".pdf"; 

// Send headers 
header("Content-Type: application/pdf"); 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
header("Content-Transfer-Encoding: binary "); 

// Blast out the PDF 
$pdf->Output('php://output'); 

值得一提的,我的使用情況是可以在變化的動態文檔下一次下載,所以我從不想讓瀏覽器緩存它。它也總是下載並且從不在瀏覽器中查看,因此content-disposition: attachment可能不適用於您的用例。

+1

像一個魅力工作,非常感謝你。 –

+0

好東西:) :) –