How do i add javascript in TCPDF?

s i can’t use css to make my auto numbering for my tables with css using TCPDF.I’m trying to use Javascipt to make this work. I read the documentation [this] and this this(Example 053 : javascript functions · TCPDF) on how to add javascript into my tcpdf. But i do not know why it does not work and it shows the error of Parse error: syntax error, unexpected 'var' (T_VAR) in C:\filepath\XAMPP\htdocs\filepath\customerinvoice.php on line 211 Can i know how can i define my ‘var’ in tcpdf? Or is not possible to add javascript in TCPDF? Please help me with this issue. Any help will be apprecialted. Thanks in advance

These are my codes

    $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 width= "30%" colspan="3" style="font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
         <td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
         <td width= "15%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
           '.$irow['quantity'].' '.$irow['quantity_unit'].'
         <td  width= "15%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
          RM '.$irow['original_price'].'
        <td  width= "20%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;">
            RM '.$irow['price'].'


This is the javascript i added in to implement auto numbering for my tables

    $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->Output('customerinvoice.pdf', 'I');

The php $js variable in the examples are being assigned a string consisting of the javascript, using php’s heredoc syntax - PHP: Strings - Manual

The <<<EOD and EOD; are the heredoc delimiters being used in the examples. You must use heredoc delimiters around the string you are assigning to the $js variable too.

I added the <<<EOD and EOD; but my auto numbering function does not work as well. Can i know what is the mistake I did wrong here. I checked the syntax and context already and it is correct.

After researching this, the javascript you include in a pdf document (which is why the method is named → IncludeJS()), allows you to perform actions in the already generated pdf document. Here’s a link to the JavaScript for Acrobat API -

This javascript is not there to operate on the html in the DOM (Document Object Model), because there is in fact no html present in the generated pdf document to operate on. The ->writeHTML() method takes the input html, parses it, and renders the lines, text, links, colors, images, … as the equivalent needed to produce the pdf document.

To dynamically number html table rows that then get converted and added to the pdf document, you will need to generate the number using php code as you loop to produce the data in $htmlcontent. (Edit: which I see you were just shown on one of the other help forums you are posting this on.)

Sponsor our Newsletter | Privacy Policy | Terms of Service