I use dompdf to create a pdf document (on the fly). I have to post any data too into this pdf, so I use ajax.
This is the 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 to post a lot data to create a pdf file to a php file like this.
This is the 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');
}
});
And this is the php to 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 is the html to be a 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 I make this html pdf will be openede into a new browser window in ajax success, any suggestion ? Thanks for the help, it so appreciated.