问题描述
因为我不能使用 css 使用 TCPDF 为我的表格自动编号,我正在尝试使用 Javascipt 来完成这项工作。我阅读了有关如何将 javascript 添加到我的 tcpdf 中的文档(this 和 this)。但我不知道为什么它不起作用,它显示了 Parse error: Syntax error,unexpected 'var' (T_VAR) in C:\filepath\XAMPP\htdocs\filepath\customerinvoice.PHP on line 211
的错误我可以知道如何在 tcpdf 中定义我的“var”吗?或者不能在 TCPDF 中添加 javascript?请帮我解决这个问题。任何帮助将不胜感激。提前致谢
这是我的代码
$htmlcontent .='
<table cellpadding="7" style="max-height: 1000px;">
<tr class="receiptcontent">
<td style="font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
</td>
<td width= "30%" colspan="3" style="font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
'.$irow['product_name'].'
</td>
<td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
'.$irow['product_code'].'
</td>
<td width= "15%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
'.$irow['quantity'].' '.$irow['quantity_unit'].'
</td>
<td width= "15%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
RM '.$irow['original_price'].'
</td>
<td width= "20%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
RM '.$irow['price'].'
</td>
</tr>
</table>
';
}
$js =
var table = document.getElementsByTagName('table')[0],rows = table.getElementsByTagName('tr'),text = 'textContent' in document ? 'textContent' : 'innerText';
for (var i = 0,len = rows.length; i < len; i++) {
rows[i].children[0][text] = i + ': ' + rows[i].children[0][text];
};
// Add Javascript code
$pdf->IncludeJS($js);
$pdf->writeHTML($htmlcontent);
ob_end_clean();
$pdf->Output('customerinvoice.pdf','I');
解决方法
请用双引号将js语句括起来:
改变
$js =
var table = document.getElementsByTagName('table')[0],rows = table.getElementsByTagName('tr'),text = 'textContent' in document ? 'textContent' : 'innerText';
for (var i = 0,len = rows.length; i < len; i++) {
rows[i].children[0][text] = i + ': ' + rows[i].children[0][text];
};
到
$js ="
var table = document.getElementsByTagName('table')[0],len = rows.length; i < len; i++) {
rows[i].children[0][text] = i + ': ' + rows[i].children[0][text];
};
"