php - Open a new window in ajax success to display the pdf -
i use dompdf create pdf document (on fly). have post data pdf, use ajax.
this 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)); } }
so,i use ajax post lot data create pdf file php file this.
this 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 new window }, error: function () { alert('terjadi masalah di server saat print laporan'); } });
and php use dompdf.
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'); }
this 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>
how can make html pdf openede new browser window in ajax success, suggestion ? help, appreciated.
if avoid persist pdf file in file system, ajax handler members/megumi/cek_list_wire_rod/generate_pdf_laporan
should store $post
ed data in session let php script, let's get_pdf
, use generate_pdf_laporan
function render pdf using data. in case success
function simple as:
success: function (response) { window.open('/members/megumi/cek_list_wire_rod/get_pdf', '_blank'); }
it recommended set correct mime type in get_pdf
:
header('content-type: application/pdf');
Comments
Post a Comment