2014-09-23 48 views
0

我正在使用dompdf庫將我的html內容轉換爲pdf。
我發送html內容從視圖到控制器與ajax發佈請求。
在這個控制器中,我打電話給dompdf助手類來將我的html轉換爲pdf文件並保存在客戶端。客戶端無法保存PDF文件dangpdf

這裏是我的Ajax請求:

$.ajax({ 
type: "POST", 
url: "<?php echo $this->config->base_url('reports/exportPDF')?>", 
data: 
{ 
report : totalHtml 
}, 
success: function (response) { 
console.log(response); 
},  
error: function (err) { 
//console.log(err); 
      } 
}); 

這裏就是我提出我的HTML內容我控制器代碼:

$this->load->helper('url'); 
$this->load->helper(array('dompdf', 'file')); 
$html = $this->input->post('report'); 
$data = pdf_create($html); 

最後,這是我DOMPDF助手庫集成代碼:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
function pdf_create($html, $filename='', $stream=TRUE) 
{ 
    require_once("dompdf/dompdf_config.inc.php"); 

    $dompdf = new DOMPDF(); 

    $htmlData = $dompdf->load_html($html); 
    $dompdf->set_paper("a4", "landscape"); 
    $dompdf->render(); 
    $dompdf->stream("report.pdf"); 

} 
?> 

根據dompdf文檔,下載對話框應該出現,但我沒有樂得到它。
我應該如何使用dompdf將pdf文件保存在客戶端?

在此先感謝。

+1

這是不容易做到你想通過AJAX做的二進制文件的新窗口視圖。相反,您應該正常提交「reports/exportPDF」(如果您需要將內容發佈到服務器上,請創建一個隱藏表單)。 – BrianS 2014-09-25 03:13:21

+0

建議通過[dompdf ajax](http://stackoverflow.com/search?q=dompdf+ajax)和[pdf ajax](http://stackoverflow.com/search?q=pdf+ajax)quetions – BrianS 2014-09-25 03:16:19

+0

但如果你真的想通過AJAX做到這一點:http://stackoverflow.com/q/16086162/264628 – BrianS 2014-09-25 03:17:48

回答

0

當您發送ajax呼叫時會發生什麼? 文件打開頁面或有錯誤? 我通常發送響應打開時,我真想頭,迫使瀏覽器打開保存對話框

$this->output->set_header('Content-Type: application/octet-stream'); 
+0

我在ajax成功函數中獲得了pdf響應,沒有錯誤。另外,當我嘗試在服務器上保存文件時,它的工作原理非常完美但是我想要下載pdf的對話框。 – nagesh29 2014-09-23 12:57:49