2016-04-25 86 views
1

我使用dompdf創建PDF文檔(即時)。我必須將任何數據也發佈到這個pdf中,所以我使用ajax。在ajax成功打開一個新窗口以顯示pdf

這是PHP DOMPDF:

<?php 

class Dompdfgenerator 
{ 
public function generate($html,$filename){ 
    define('DOMPDF_ENABLE_AUTOLOAD', false); 
    require_once("./vendor/dompdf/dompdf/dompdf_config.inc.php"); 

    $dompdf = new DOMPDF(); 
    $dompdf->load_html($html); 
    $dompdf->render(); 
    $dompdf->stream($filename.'.pdf',array("Attachment"=>0)); 
} 
} 

所以,我用ajax發佈了大量的數據來創建一個PDF文件到PHP文件是這樣的。

這是AJAX:

var result = $("#hasil-pencarian").clone().find('td:last-child').remove().end().html(); 

$.ajax({ 
url: "<?= site_url('members/megumi/cek_list_wire_rod/generate_pdf_laporan') ?>", 
type: 'POST', 
data: { 
    result: result, 
    id_pendukung: $('.printLaporan').attr('data-id') 
}, 
dataType: 'json', 
success: function (response) { 

    open a new window 


}, 
error: function() { 
    alert('Terjadi masalah di server saat print laporan'); 
} 
}); 

這是使用DOMPDF的PHP。

public function generate_pdf_laporan() { 
    $this->load->library('dompdfgenerator'); 

    $data= array(
     'result' => $this->input->post('result') 
    ); 

    $html = $this->load->view('members/megumi/check_list_of_wire_rod/v_laporan_check_list', $data, true); 

    $this->dompdfgenerator->generate($html, 'contoh'); 
} 

這是HTML是一個PDF

<!DOCTYPE html> 
<html> 
<head> 
<title>Report Table</title> 
<style type="text/css"> 
#outtable{ 
    padding: 20px; 
    border:1px solid #e3e3e3; 
    width:600px; 
    border-radius: 5px; 
} 

.short{ 
    width: 50px; 
} 

.normal{ 
    width: 150px; 
} 

table{ 
    border-collapse: collapse; 
    font-family: arial; 
    color:#5E5B5C; 
} 

thead th{ 
    text-align: left; 
    padding: 10px; 
} 

tbody td{ 
    border-top: 1px solid #e3e3e3; 
    padding: 10px; 
} 

tbody tr:nth-child(even){ 
    background: #F6F5FA; 
} 

tbody tr:hover{ 
    background: #EAE9F5 
} 
</style> 
</head> 
<body> 
<div id="outtable"> 
    <table> 
    <thead> 
     <tr> 
      <th class="short">No</th> 
      <th class="normal">First Name</th> 
      <th class="normal">Last Name</th> 
      <th class="normal">Username</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php echo $result ?> 
    </tbody> 
    </table> 
</div> 

我怎樣才能讓這個HTML PDF將openede到阿賈克斯成功的一個新的瀏覽器窗口,任何建議? 感謝您的幫助,它如此讚賞。

+0

如果你使用AJAX來告訴它是成功的,你需要在這種情況下,您的success函數是簡單將PDF保存在服務器本地,並在返回的AJAX中提供對保存位置的引用;你也可以指導用戶。 – MackieeE

+0

不完全重複,但這[鏈接](http://stackoverflow.com/questions/9399354/how-to-open-a-new-window-and-insert-html-into-it-using-jquery)解決你所要求的。 – Dacklf

回答

0

使用window.open

window.open("http://path_pdf_file"); 

PHP爲PDF格式保存到磁盤:

$output = $dompdf->output(); 
    file_put_contents($filename.'.pdf', $output); 
+0

兄弟,這是飛PDF文件它不是創建/ –

+0

@FadlyDzil保存在磁盤上的文件,然後從ajax返回該文件的路徑 – madalinivascu

+0

我不想創建這些文件保存硬盤 –

0

如果你想避免持續的PDF文件中的文件系統,你的Ajax處理members/megumi/cek_list_wire_rod/generate_pdf_laporan應該只是在會話中存儲$POST ed數據以讓另一個php腳本(比如get_pdf)使用generate_pdf_laporan函數使用此數據呈現PDF。

success: function (response) { 
    window.open('/members/megumi/cek_list_wire_rod/get_pdf', '_blank'); 
} 

建議設置正確的MIME類型get_pdf

header('Content-Type: application/pdf');