2017-07-21 62 views
-1

我想保留一个按钮来下载pdf文件。但库文件显示在视图中。有人请帮助我。提前感谢。 控制器代码:如何在codeigniter中包含pdf下载

public function pdf() 
{ 
    //load library 
    $this->load->library('pdf'); 
    $pdf = $this->pdf->load(); 
    // retrieve data from model 
    $data['DocFile'] = $this->Docauth->get_items(); 
    $data['DocFileName'] = "items"; 
    ini_set('memory_limit', '256M'); 
    // boost the memory limit if it's low ;) 
    $html = $this->load->view('Docauth_v', $data, true); 
    // render the view into HTML 
    $pdf->WriteHTML($html); // write the HTML into the PDF 
    $output = 'Docauth' . date('Y_m_d_H_i_s') . '_.pdf'; 
    $pdf->Output("$output", 'I'); // save to file because we can 
    exit(); 

视图文件

<div class="form-group">                
    <label class="control-label col-md-3">View File</label> 
    <div class="col-md-5"> 
     <input name="DocFile" id="DocFile" class="form-control"` type="text" > 
    </div> 
    <a href="Docauth/pdf"><span class="glyphicon glyphicon-plus"></span></a> 
</div> 
+1

https://stackoverflow.com/questions/30615451/download-a-pdf-file-instead -of-displays-it-with-codeigniter – RiggsFolly

+0

您是否尝试在输出之前发送头信息(content-type,content-disposition)? – BenRoob

+1

更重要的是,你尝试过谷歌搜索'“pdf下载codeigniter”'。它确实产生了大约1,000,000个东西供您查看 – RiggsFolly

回答

1

Output功能I将文件发送内嵌到浏览器。您需要使用D选项:

$pdf->Output("$output", 'D'); 

此处了解详情:http://www.rubydoc.info/gems/rfpdf/1.17.1/TCPDF%3AOutput

@ PARAM字符串:DEST

目的地,发送文档。它可以采用以下值之一的 :

I:将文件内联发送到浏览器(默认)。如果可用,插件使用 。当您在生成PDF的链接上选择 “另存为”选项时,将使用名称给出的名称。 D:发送到浏览器并强制按名称给出的名称为 的文件下载。 F:保存到名称为名称的本地服务器文件中。 S:将文档作为字符串返回。名称被忽略。

FI:相当于F + I选项

FD:相当于F + d选项