0

We are using the pdfMake javascript framework to render HTML to a PDF document.

pdfMake.org

Given that a simple working document definition object is rendered thusly:

var docDefinition = {
   content: { stack: [
      { text: 'foo', style: 'normal', margin: [0,1,0,0] },
      { text: 'bar', style: 'bold', margin: [0,1,0,0] }
   ]}
}

And given that objectives is an array of HTML elements, we are attempting to parse said elements and return the stack definition via a function. Like so:

var docDefinition = {
   content: { stack: parseSection(objectives) }
}

Here is a greatly simplified summary of the function:

function parseSection(section){

   var stack = []
   ...
   str1 = "{ text:'" + fooVar + "',style: 'normal', margin: [0,1,0,0] }"
   str2 = "{ text:'" + barVar + "',style: 'bold', margin: [0,1,0,0] }"

   stack.push( str1 )
   stack.push( str2 )
   ...

   return stack

})

Problem is, pdfMake will render as strings verbatim str1 and str2 rather than process the definitions.

What else can we do (or what else can we look for) to deliver the stack definition as an array (object?) that pdfMake's document definition can understand and render correctly?

Steve Mock
  • 165
  • 1
  • 8

1 Answers1

0

You are passing a string. Construct and return objects instead. Like so:

function parseSection(section){

   var stack = []

   obj1 = {
      text: fooVar,
      style: 'normal',
      margin: [0,1,0,0]
   }

   obj2 = {
      text: barVar,
      style: 'bold',
      margin: [0,1,0,0]
   }

   stack.push( obj1 )
   stack.push( obj2 )

   return stack

})
Steve Mock
  • 165
  • 1
  • 8