codelearner codelearner - 4 months ago 22
Node.js Question

How to generate Array of items in html template for creating pdf in Node Js?

I am using html-pdf for pdf creation on my project. i can generate all the details into the pdf. but the thing is i can not be able to create List of items in array in html(which will be used for pdf conversion).

      Since i cannot use javascript to get the array of items, i am unable to get it. if there is a tweak to use javascript in html template,please post it here. Since the array of items is known, i can use it easily.

Suppose if the array length is 3 . i can use like

array[0].name ,array[1].name, array[2].name
but here i don't know the length of the array which consists of how many items. Can anyone please help me here to generate the table of items which is generating dynamically.

Answer

You cannot use Javascript in template, but you can use Javascript on it.

The idea is to make a parser which will :

  • read your template
  • replace a keywork (for example : <customTable arrayName="myCustomArray" fields="['name']" />) with a generated HTML array in Node.js.
  • use this template with html-parser.

I write an example of parser in VanillaJS here : https://jsfiddle.net/rbuuzcre/5/

Comments