下载PDF文件,而不是用CodeIgniter显示它


Download a PDF file instead of displaying it with CodeIgniter

我正在使用CodeIgniter和R&OS PDF类生成PDF文件。但现在的问题是,pdf显示到浏览器。相反,我希望它被下载。下面是我的代码:

$this->load->library('cezpdf');
        $data['users'] = $this->user->get_all_ayant_droits();
        foreach($data['users'] as $user) {
            $db_data[] = array('name' => $user->nom, 'department' => $user->Department, 'status' => $user->Status);
        }
        $col_names = array(
            'name' => 'Noms et Prenoms',
            'department' => 'Département',
            'status' => 'Status'
        );
        $this->cezpdf->ezTable($db_data, $col_names, 'Ayant droit Base Loisirs de Kribi', array('width'=>550));
        $this->cezpdf->ezStream();  

控制器发送要下载的文件缺少什么?

您可以将参数传递给函数ezStream

$this->cezpdf->ezStream(array $options);

$options 'compress' => 0/1启用压缩。对于压缩级别,请在第一个点使用$this->options['compression'] =。默认值:1'download' => 0/1显示内联(在浏览器中)或作为下载。默认值:0

你可以使用下载助手https://ellislab.com/codeigniter/user-guide/helpers/download_helper.html?

$this->load->helper('download');
$data = $this->cezpdf->ezStream(); 
force_download("PDF_filename.pdf", $data); 

您也可以通过设置适当的头值来使用输出变量。

$this->output->set_header('Content-Disposition: attachment; filename="PDF_filename.pdf"');
$this->output->set_content_type('application/pdf')
             ->set_output($this->cezpdf->ezStream());

这里通过设置内容类型为 application/pdf,这样浏览器识别内容为pdf,而 content - disposition: attachment强制下载文件。

希望这对你有帮助。对不起,我的英文很差。