使用FPDF PHP时奇怪的字符?

时间:2015-10-13 17:22:45

标签: php http pdf http-headers fpdf

使用FPDF库生成pdf文件时,我得到了这个输出。

  

%PDF-1.3 3 0 obj<> endobj 4 0 obj<>流   xUn0<!ˁ78' ZQ(U〜B8oel E&放大器;ltʙ:Clk时|| p | Ke'-9BWj $ FV tq3toXrlQP%ncXB_!S1中+    B )ו (m HVendstreamstreamendobj 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<< /制片人   (FPDF 1.7)/ CreationDate(D:20151013130538)>> endobj 7 0 obj<< /类型   / Catalog / Pages 1 0 R>> endobj xref 0 8 0000000000 65535 f 0000000354   00000 n 0000000542 00000 n 0000000009 00000 n 0000000087 00000 n   0000000441 00000 n 0000000646 00000 n 0000000721 00000 n预告片<<   /大小8 /根7 0 R /信息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 个答案:

答案 0 :(得分: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)

你需要添加一个exit();在您的代码末尾。取自 https://expressionengine.com/forums/archive/topic/188158/pdf-and-http-headers 并在 php 7.2 和 codeigniter 4.0 中测试了它的工作