我想通過jQuery發送一些html內容到tcpdf並創建pdf。 我有這個在我的index.php:發送變量到tcpdf jQuery
$('#test').click(function() {
var result='hello';
$.ajax({
type: 'POST',
url: 'pdf.php',
data: {result: result},
});
})
和pdf.php:
<?php
$result = '';
if(isset($_POST['result'])) {
$result = $_POST['result'];
}
require_once('tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle('Production sheet');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html = '
<h1>TEST</h1>
<table>
<tr><td>'.$result.'</td></tr>
</table>
';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('test.pdf', 'I');
?>
如果我定義變量結果($result = "Hello";
)和我直接運行pdf.php它的工作完美。似乎由於某種原因,我的文章沒有被pdf.php採納......有一些想法?
謝謝!
代碼確實看起來正確。您是否試圖在訪問tcpdf之前回顯$ result以檢查它是否正確傳遞?我可能太累了,但在代碼中看不到任何錯誤。 –
'alert(result);'正確顯示。如果我添加'成功:函數(數據){$('#testDiv')。html(數據);}'顯示我的變量'hello'...這裏有一些奇怪的... –
謝謝大家的幫助。埃米爾的回答做到了。 –