使用 FPDF PHP 时出现奇怪的字符?
weird characters when using FPDF PHP?
我在使用 FPDF 库生成 pdf 文件时得到了这个输出。
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream
x�U��n�0��<�ˁ�7��8'�!Z���q(U~���!B�8��o�e����l���e�&��l�tʙ��:Cl�k||��p�|K����e�'�-9���B���Wj�$F�����V��t���q��3to��XrlQP�%���n-c�X��B_!Sl�����+��
����B�)װ�I����(m�����HV endstream endobj 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 << /Producer
(FPDF 1.7) /CreationDate (D:20151013130538) >> endobj 7 0 obj << /Type
/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 trailer <<
/Size 8 /Root 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
下面的headers和输出命令是我目前在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
可能不适用于您的用例。
您需要添加一个exit();在代码的末尾。取自 https://expressionengine.com/forums/archive/topic/188158/pdf-and-http-headers 并测试其与 php 7.2 和 codeigniter 4.0
的工作
我在最后添加了一个出口,它对我有用。
<?php
require('12345/1234.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',72);
$pdf->Cell(40,10,'Hello!');
$pdf->Output();
exit;
?>
我在使用 FPDF 库生成 pdf 文件时得到了这个输出。
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�U��n�0��<�ˁ�7��8'�!Z���q(U~���!B�8��o�e����l���e�&��l�tʙ��:Cl�k||��p�|K����e�'�-9���B���Wj�$F�����V��t���q��3to��XrlQP�%���n-c�X��B_!Sl�����+�� ����B�)װ�I����(m�����HV endstream endobj 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 << /Producer (FPDF 1.7) /CreationDate (D:20151013130538) >> endobj 7 0 obj << /Type /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 trailer << /Size 8 /Root 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
下面的headers和输出命令是我目前在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
可能不适用于您的用例。
您需要添加一个exit();在代码的末尾。取自 https://expressionengine.com/forums/archive/topic/188158/pdf-and-http-headers 并测试其与 php 7.2 和 codeigniter 4.0
的工作我在最后添加了一个出口,它对我有用。
<?php
require('12345/1234.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',72);
$pdf->Cell(40,10,'Hello!');
$pdf->Output();
exit;
?>