0.1.2 • Published 1 year ago

json-to-pdf v0.1.2

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
1 year ago

json-to-pdf

Generates PDF documents using PDF-Make and a basic JSON templating system similar to handlebars.

Usage

JSON templates are used which match the format of PDFMake document definition objects:

{
  "pageSize":"A4",
  "pageOrientation":"portrait",
  "pageMargins":[25, 25, 25, 25],
  "content":[
    "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Confectum ponit legam, perferendis nomine miserum, animi. Moveat nesciunt triari naturam.",
    {
      "text":"This text should appear on the second page",
      "pageBreak":"before"
    }
  ]
}

These templates can be expanded using data to create rich documents:

Template:

{
  "pageSize":"A4",
  "pageOrientation":"portrait",
  "pageMargins":[25, 25, 25, 25],
  "content":[
    "{{loremipsum}}",
    {
      "text":"{{page2.text}}",
      "pageBreak":"before"
    }
  ]
}

Data:

const data = {
  loremipsum: 'Lorem ipsum...naturam.',
  page2: {
    text: 'This text should appear on the second page'
  }
}

Code:

import * as jsonToPdf from 'json-to-pdf';

const pdfStream = jsonToPdf.renderPdfTemplate(template, data);
const fileStream = createWriteStream('./example.pdf');
pdfStream.pipe(fileStream);
pdfStream.end();

There are several helpers available to expand the JSON templates.

each

Repeats for each item in a supplied array

[
  "{{#each lines:line}}": {
    "text": "{{line}}"
  }
]

With data = ['a','b'] will inflate to:

[
  {
    "text": "a"
  },
  {
    "text": "b"
  }
]

if

Only includes value if supplied variable is truthy:

{
  "{{#if test}}": {
    "text": "{{text}}"
  }
}

With data = {test: true, text: 'show me'} will inflate to:

{
  "text": "show me"
}

unless

Like if, but will include value if the supplied variable is falsy:

{
  "{{#unless test}}": {
    "text": "{{text}}"
  }
}

With data = {test: true, text: 'don\'t show me'} will inflate to:

{}

equal

Only includes value if first supplied value equals the second:

{
  "{{#equal 6:{{test}}}}": {
    "text": "{{text}}"
  }
}

With data = {test: 6, text: 'show me'} will inflate to:

{
  "text": "show me"
}

notequal

Only includes value if first supplied value equals the second:

{
  "{{#notequal 6:{{test}}}}": {
    "text": "{{text}}"
  }
}

With data = {test: 6, text: 'don\'tshow me'} will inflate to:

{}

Fonts

As standard, the fonts available are:

  • Courier
  • Helvetica
  • Times
  • Symbol
  • ZapfDingbats

To use custom fonts, you can use the third argument of the renderPdfTemplate method to supply a font object:

const customFonts = {
  Roboto: {
    normal: './path/to/font/Roboto-Regular.ttf',
    bold: './path/to/font/Roboto-Medium.ttf',
    italics: './path/to/font/Roboto-Italic.ttf',
    bolditalics: './path/to/font/Roboto-MediumItalic.ttf'
  }
}

const pdfDoc = renderPdfTemplate(template, data, customFonts);

API Documentation

Functions

functionifyHeaderFooter(node) ⇒ DynamicContent | Content

Kind: global function
Category: Helpers
Since: v0.0.1

ParamTypeDescription
nodeDynamicContent | ContentThe current object node

Example

processDataNode({test: '{{a}}'}, {a: 'b'}, {})    //=> {test: 'b'}

getObjectValue(obj, path) ⇒ *

Kind: global function
Category: Helpers
Since: v0.0.1

ParamTypeDescription
objobjectThe object to search
pathstring | Array.<string>The path to traverse

Example

getObjectValue({a: {b: {c: 1}}}, 'a.b.c')    //=> 1
getObjectValue({a: {b: {c: 1}}}, 'a.b.c.d')  //=> undefined
getObjectValue({a: {b: {c: 1}}}, 'a..b..c')  //=> undefined
getObjectValue({a: {b: {c: 1}}}, 'c')        //=> undefined
getObjectValue({a: {b: {c: [1,2,3]}}}, 'a.b.c.1')        //=> 2
getObjectValue({a: {b: {c: 6}}}, 'a.b.c.toFixed(2)')        //=> '6.00'

getTemplateLiteral(val) ⇒ string

Kind: global function
Category: Helpers
Since: v0.0.1

ParamTypeDescription
valstringThe string to process

Example

getTemplateLiteral('test')        //=> ''
getTemplateLiteral('{{test}}')    //=> 'test'
getTemplateLiteral('{{{test}}}')  //=> '{test}'
getTemplateLiteral('{{}}test')    //=> ''
getTemplateLiteral('{{}}')        //=> ''

isArrayFunction(element) ⇒ boolean

Kind: global function
Category: Helpers
Since: v0.0.1

ParamTypeDescription
elementobjectThe object to look for array function key

Example

isArrayFunction({a: 1, '{{#each a:b}}': {b: 2}, c: 3})    //=> true
isArrayFunction({a: 1, b: 2, c: 3})    //=> false

nestedKeyValue(obj, key) ⇒ *

Kind: global function
Category: Helpers
Since: v0.0.1

ParamTypeDescription
obj*The object to search
key*The key to find

Example

nestedKeyValue({a: {b: {c: 1}}}, 'c')  //=> 1
nestedKeyValue({a: {b: {c: 1}}}, 'd')  //=> undefined
nestedKeyValue({a: {b: {c: 1}}}, 'b')  //=> {c: 1}

inflateLiterals(template, data, settings) ⇒ string

Kind: global function
Category: Main
Since: v0.0.1

ParamTypeDescription
templatestringThe string to search and replace within
dataobjectThe data to search for replacement values
settingsSettingsSettings to control how they are

Example (Ignores plain text)

inflateLiterals('test', {}, {})    //=> 'test'

Example (Replaces one or more placeholders)

inflateLiterals('here is a {{val}}', {val: 'test'}, {})    //=> 'here is a test'
inflateLiterals('here is another {{val}}{{val}}', {val: 'test'}, {})    //=> 'here is a testtest'

Example (Can extract positional values from arrays)

inflateLiterals('here is a {{val.1}}', {val: ['test1', 'test2']}, {})    //=> 'here is a test2'

Example (Or will comma separate full arrays)

inflateLiterals('here is a {{val}}', {val: ['test1', 'test2]}, {})    //=> 'here is a test1, test2'

processDataNode(val, data, settings) ⇒ unknown

Kind: global function
Category: Main
Since: v0.0.1

ParamTypeDescription
valunknownThe current object node
dataobjectThe data to search for replacement values
settingsSettingsSettings to control how they are

Example

processDataNode({test: '{{a}}'}, {a: 'b'}, {})    //=> {test: 'b'}

processFunction(functionKey, object, data, settings) ⇒ unknown

Kind: global function
Category: Main
Since: v0.0.1

ParamTypeDescription
functionKeystringThe function to perform
objectobjectThe object to be inflated by the function
dataobjectThe data used to inflate the object
settingsSettingsSettings to control how items are processed

Example

processFunction('{{#each items:item}}', {text: '{{item}}'}, {items: ['a', 'b', 'c']}, {})    //=> '[{text: 'a'},{text: 'b'},{text: 'c'}]'

renderPdfTemplate(docDefinition, data) ⇒ stream

Kind: global function
Category: Main
Since: v0.0.1

ParamTypeDescription
docDefinitionExpandableDocDefinitionThe JSON string or object definition to inflate
dataobjectThe data to use for inflated values

© 2023 Dataclear Ltd Documented by jsdoc-to-markdown.

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago