Codeigniter中的FPDF在下载时会出现网络错误

时间:2013-11-06 09:14:09

标签: php codeigniter fpdf

如何在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文件中提供的输出()

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 '';
}

如何解决这个问题。

0 个答案:

没有答案