使用php将数据打印为pdf

时间:2010-02-15 17:19:55

标签: 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 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&lt;&gt; endobj 5 0 obj&lt;&gt; endobj 2 0 obj&lt;&lt; / ProcSet [/ PDF / Text / ImageB / ImageC / ImageI] / Font&lt;&lt; / F1 5 0 R&gt; / XObject&lt;&lt; &GT;&GT; &GT;&GT; endobj 6 0 obj&lt;&lt; /制作人(FPDF 1.6)/标题(FPDF教程)/作者(Lana Kovacevic)/ CreationDate(D:20100215182640)&gt;&gt; endobj 7 0 obj&lt;&lt; /类型/目录/页1 0 R / OpenAction [3 0 R / FitH null] / PageLayout / OneColumn&gt;&gt; endobj xref 0 8 0000000000 65535 f 0000000407 00000 n 0000000595 00000 n 0000000204 00000 n 0000000282 00000 n 0000000494 00000 n 0000000699 00000 n 0000000822 00000 n预告片&lt;&lt; /大小8 /根7 0 R /信息6 0 R&gt;&gt; startxref 925 %% EOF

3 个答案:

答案 0 :(得分:1)

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

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

请务必在致电$pdf->Output('example1.pdf','I');

之前致电

此问题也可能是由于发送了正确的浏览器信息但未安装an application that understands PDFs而引起的。

答案 1 :(得分:0)

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

将pdf保存到文件。如果你想直接在浏览器中显示pdf(或打开下载对话框,它取决于浏览器和插件),你应该这样做:

$pdf->Output();

答案 2 :(得分:0)

Output(..., 'I')应该为您处理HTTP标头 - 除非您使用的是PHP-CLI。

当目的地为FDPF::Output时,

'cli'取决于php_sapi_name 返回'I'。它仍然会回显缓冲区,但未指定正确的标题。

如果您使用的是FPDF 1.6,请参阅第1010-1027行,尤其是第1010-1027行。 1014,fpdf.php

相关问题