Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将数据打印为pdf_Php - Fatal编程技术网

使用php将数据打印为pdf

使用php将数据打印为pdf,php,Php,我有一个网站,用户需要采取pdf格式的报告[在代码中],我尝试了以下代码,坚果它显示一些特殊字符,请帮助我解决这个问题 <?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

我有一个网站,用户需要采取pdf格式的报告[在代码中],我尝试了以下代码,坚果它显示一些特殊字符,请帮助我解决这个问题

<?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'); 
?> 

我得到的输出如下
g endstream endobj 1 0 obj endobj 5 0 obj endobj endobj 2 0 obj/XObject>>endobj 6 0 obj>endobj 7 0 obj>endobj外部参照0 8 000000 65535 f 0000000 407 00000 n 0000000 595 00000 n 0000000 204 00000 n 0000000 282 00000 n 0000000 494 00000 n 0000000 699 00000 n 0000000 822 00000 n拖车>启动外部参照925%%EOF

最有可能您只需要将内容类型指定为PDF,以便浏览器知道如何处理该文件

header('Content-type: application/pdf');
确保在调用
$pdf->Output('example1.pdf','I')之前调用

此问题也可能是由于发送了正确的浏览器信息,但尚未安装

$pdf->Output('example1.pdf','I');
将pdf保存到文件中。如果要在浏览器中直接显示pdf(或打开下载对话框,具体取决于浏览器和插件),应执行以下操作:

$pdf->Output();
输出(…,'I')
应该为您处理HTTP头——除非您使用的是PHP-CLI

FDPF::当目标是
'I'
时,输出取决于不返回
'cli'
。它仍然回显缓冲区,但没有指定正确的头


如果您使用的是FPDF 1.6,请参见
FPDF.php

欢迎使用SO的第1010-1027行,特别是第1014行。请对您的问题进行更详细的描述,例如显示输出的特殊字符。是的,看起来您需要添加
内容类型
标题。请看下面Cryo的回答。我认为FPDF已经在Output()中完成了,但我可能弄错了。啊,可能是您是对的,没有标题输出的迹象@Pekka我也这么认为,但他的问题听起来像是PDF的文本输出。实际上,第二个参数指定了文件的发送位置,“I”表示“内联到浏览器”: