如何在 codeigniter 中生成 FPDF 文件。我试过下面的代码它给出了错误。
控制器功能
public function pdf()
{
// Generate PDF by saying hello to the world
$this->load->library('pdf'); // Load library
$this->pdf->fontpath = 'font/'; // Specify font folder
$this->pdf->AddPage();
$this->pdf->SetFont('Arial','B',16);
$this->pdf->Cell(40,10,'Hello World!');
$this->pdf->Output();
}
我将 pdf.php 文件放在 application/libraries 文件夹中
pdf.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require('fpdf.php');
class Pdf extends FPDF
{
// Extend FPDF using this class
// More at fpdf.org -> Tutorials
function __construct($orientation='P', $unit='mm', $size='A4')
{
// Call parent constructor
parent::__construct($orientation,$unit,$size);
}
}
?>
fpdf.php 文件在同一文件夹中可用。当我使用此代码时,我无法下载 pdf 文件,我给了我一个网络错误或找不到网页
这是我在 fpdf.php 文件中的 output()
function Output($name='', $dest='')
{
// Output PDF to some destination
if($this->state<3)
$this->Close();
$dest = strtoupper($dest);
if($dest=='')
{
if($name=='')
{
$name = 'doc.pdf';
$dest = 'I';
}
else
$dest = 'F';
}
switch($dest)
{
case 'I':
// Send to standard output
$this->_checkoutput();
if(PHP_SAPI!='cli')
{
// We send to a browser
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="'.$name.'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
}
echo $this->buffer;
break;
case 'D':
// Download file
$this->_checkoutput();
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename="'.$name.'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
echo $this->buffer;
break;
case 'F':
// Save to local file
$f = fopen($name,'wb');
if(!$f)
$this->Error('Unable to create output file: '.$name);
fwrite($f,$this->buffer,strlen($this->buffer));
fclose($f);
break;
case 'S':
// Return as a string
return $this->buffer;
default:
$this->Error('Incorrect output destination: '.$dest);
}
return '';
}
如何解决这个问题呢。