Search code examples
node.jshtml-pdfnode-html-pdf

How to render html tags/to generate dynamic tables using html-pdf


I want to generate a table from node.js to be printed in pdf format using html-pdf library but unfortunately my code won't work. The problem is because the html-pdf won't execute html tags. It execute the tags as string instead.

var tableBody = '';
for (i = 1; i < data.lenght; i++);
{
     tableBody = tableBody + '<tr>' +
                '<td>' + data[i].full_name + '</td>' +
                '<td>' + data[i].age + '</td>' +
                '<td>' + data[i].address + '</td>' +
             '</tr>';
 }
pdfParam.tableBody = tableBody;

then in html file, my code just like this.

<table>
  <tbody>
      {{tableBody}}
  </tbody>
</table>

Is there a way to solve this? Any answers and comments are really appreciated.


Solution

  • I used your code and it served me very well, I only made a couple of changes:

    var tableBody = '';
    for (i = 1; i < data.lenght; i++)(drop this semicolon)
    {
         tableBody = tableBody + '<tr>' +
                    '<td>' + data[i].full_name + '</td>' +
                    '<td>' + data[i].age + '</td>' +
                    '<td>' + data[i].address + '</td>' +
                 '</tr>';
     }
    (drop this param)
    

    in the var content (html):

    only ${tableBody} in the right place...