2016-04-25 39 views
1

私はdompdfを使ってpdfドキュメントを作成しています。私はこのpdfにもデータを投稿しなければならないので、私はajaxを使います。ajax successで新しいウィンドウを開き、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)); 
} 
} 

、私はこのようなPHPファイルにPDFファイルを作成するために、多くのデータを投稿する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'); 
} 

これは、PDF私はこのHTML PDFは、AJAX成功に新しいブラウザウィンドウにどんな提案をopenedeされることができますどのよう

<!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です? 助けてくれてありがとう、それはとても感謝しています。ただ

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

あなたはそれが成功した伝えるためにAJAXを使用した場合、あなたが必要としますPDFをサーバーにローカルに保存し、返されたAJAXの保存場所への参照を提供します。そこにあなたもユーザーを誘導します。 – MackieeE

+0

正確には重複していませんが、この[link](http://stackoverflow.com/questions/9399354/how-to-open-a-new-window-and-insert-html-into-it-using-jquery)あなたが求めているものを解決します。 – Dacklf

答えて

0

使用ディスクにPDFファイルを保存するためのwindow.open

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

PHP店舗$POSTデータをセッションで他のPHPスクリプトを聞かせて、get_pdfと言うと、generate_pdf_laporanこのデータを使用してPDFをレンダリングする機能を使用します。 get_pdfに正しいMIMEタイプを設定することをお勧めします

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

:この場合、あなたのsuccess機能は、のような単純なものでしょう

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

Brother、それは作成中でないpdfファイルです。/ –

+0

@FadlyDzilはファイルをディスクに保存してから、そのファイルのパスをajaxから返します。 – madalinivascu

+0

ファイルを保存したくありません。 –

0

あなたは、ファイルシステム内のpdfファイルを永続化するために避けたい場合は、あなたのAJAXハンドラmembers/megumi/cek_list_wire_rod/generate_pdf_laporan必要があります。

関連する問題