Jump to content

How do i add javascript in TCPDF?


sashavalentina

Recommended Posts

As 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 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 

This 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>

        <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>

';
}

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->IncludeJS($js);
    $pdf->writeHTML($htmlcontent);  
    ob_end_clean(); 
    $pdf->Output('customerinvoice.pdf', 'I');

 

Link to comment
Share on other sites

4 hours ago, sashavalentina said:

I'm trying to use Javascipt to make this work.

If only PHP had a way you could increment a count.

$i = 0;  
$content = '
    <style>
    td {
        font-weight: normal;
        font-size: 8px;
        line-height: 12px;
        color: #000000;
    }
    </style>
    
    <table id="receiptTable" style="max-height: 1000px;" >
        <tr>
         <td width = "7%" >
            '. ++$i .'  
            </td>
        
            <td  width= "8%"  >
                '.'xxxxxx'.'
            </td>
            
             <td  width= "10%" >
               '.'xxxxxx'.'
            </td>
             <td   width= "30%">
               '.'123'.' '.'Kg'.'
            </td>
             <td  width= "20%" >
              RM '.'123.45'.'
            </td>
            <td  width= "20%" >
                RM '.'123.45'.'
            </td>
        </tr>
        <tr>
         <td width = "7%" >
            '. ++$i .'  
            </td>
        
            <td  width= "8%"  >
                '.'xxxxxx'.'
            </td>
            
             <td  width= "10%" >
               '.'xxxxxx'.'
            </td>
             <td   width= "30%">
               '.'123'.' '.'Kg'.'
            </td>
             <td  width= "20%" >
              RM '.'123.45'.'
            </td>
            <td  width= "20%" >
                RM '.'123.45'.'
            </td>
        </tr>
        <tr>
         <td width = "7%" >
            '. ++$i .'  
            </td>
        
            <td  width= "8%"  >
                '.'xxxxxx'.'
            </td>
            
             <td  width= "10%" >
               '.'xxxxxx'.'
            </td>
             <td   width= "30%">
               '.'123'.' '.'Kg'.'
            </td>
             <td  width= "20%" >
              RM '.'123.45'.'
            </td>
            <td  width= "20%" >
                RM '.'123.45'.'
            </td>
        </tr>
    </table>
';

OUTPUT

image.png.24dcbb9e8ca1d31647e91f2fcf7347cd.png

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.