使用TCPDF打印pdf时遇到问题。故事板是,我有一个搜索表单,我将结果显示在表格中。我的目的是,当点击一个按钮时,用户可以在javascript中的window.open
浏览器的新窗口中将此表打印成PDF。我使用ajax发送一些数据,在这种情况下,我发送包含<tbody></tbody>
。现在,在ajax的成功,我不知道我应该怎么做。这是我的代码。
HTML
<tbody id="hasil-pencarian">
<tr>
<td>1</td>
<td>TMS/IT/06/001</td>
<td>Erika Julia Widiyanti</td>
<td>Marketing</td>
<td>14-06-2015 13:59</td>
<td>14-06-2015 14:00</td>
<td>Erika test 1</td>
<td>Jawab erika request 1<br></td>
<td>14-06-2015 15:00</td>
<td>15-06-2015 17:19</td>
<td class="center" width="10px">
<a class="btn btn-success"><i class="halflings-icon white print" id="print"></i>Print</a>
</td>
</tr>
<p class="center">
<button class="btn btn-small btn-primary" id="print-bottom">Print Data Di atas</button>
</p>
这是jquery上的ajax
$(document).on('click', '#print-bottom', function() {
var result = $("#hasil-pencarian").html();
$.ajax({
url: '<?php echo base_url() . 'it_team/control_it/generate_pdf' ?>',
type: 'POST',
data: {result: result},
success: function() {
//it still gives me a blank content of table if I use like this :
window.open("<?php echo base_url() . 'it_team/control_it/generate_pdf/' ?>", "_blank", "top=25, left=250, toolbar=no, width=1000, height=600");
},
error: function() {
alert('Error');
}
});
});
这是TCPDF
public function generate_pdf() {
if (($this->session->userdata('logged_in') != "logging") && ($this->session->userdata('logged_in') != "IT")) {
redirect('control_auth');
} else {
$this->load->library("tcpdf");
//Post from ajax
$html = $this->input->post('result');
$pdf = new tcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 8);
// This is for <th> header
$tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="1">
<tr>
<td align="center" width='1px'><b> No. </b></td>
<td align="center"><b>No_Req</b></td>
<td align="center"><b>Nama user</b></td>
<td align="center"><b>Dept</b></td>
<td align="center"><b>Masuk ke IT</b></td>
<td align="center"><b>Diterima IT</b></td>
<td align="center"><b>Keluhan</b></td>
<td align="center"><b>Tindakan IT</b></td>
<td align="center"><b>Estimasi</b></td>
<td align="center"><b>Tutup Request</b></td>
</tr>
EOD;
// Adding the content of table based from post ajax
$tbl = $tbl. $html;
$tbl = $tbl . "</table>";
$pdf->writeHTML($tbl, true, false, false, false, '');
$pdf->Output('example_048.pdf', 'I');
}
}