Step by step - Learn how to use callas productscallas pdfChip What is new in 1.1Passing variable information to HTML template using "--import" on the command line

Passing variable information to HTML template using "--import" on the command line

As of pdfChip 1.1, it is possible to pass through variable information from the command line into the pdfChip specific "cchip" user data object. The idea is to associate a key on the command line with a file containing a JSON expression. This implies that a file with such JSON expression must exist before its content can be handed over as a command line parameter.

The "--import" command line parameter

Command line argument

--import=<key>:<file.json>

or

--import=<key>:<file.js>

The parameter --import  is optional. If present it must also provide a <key> and point to a file <file.json> containing a JSON expression.

  • <key> must be a string that can be used as a valid key in JavaScript
  • <file.js> resp.  <file.json> must be an absolute or relative file path.

Several instances of the --import parameter may be present in a command line call, each creating their own data object.

JSON expression or Javascript variable

The variable information may be provided as a JavaScript file or a JSON file:

  • Where a JavaScript file <file.js> is provided it must contain a single variable assignment. The JavaScript variable name will be ignored, and only the JSON expression is used.
  • Where a JSON file <file.json> is provided it must contain single JSON expression.

Using the variable information in JavaScript

The variable information provided in <file.js> or <file.json> will be associated – using the <key> value – with the pdfChip specific data object cchip.user.

Example

  • pdfChip command line call:
pdfChip ./in.html --import=addresses:./address-list.json  ./out.pdf
  • content of the variable information as a JSON file address-list.json:
{	
	contacts [
		{
			"fullname" : "John Doe",
			"zip" : "12345",
			"city" : "Big Town"
		},
		{
			"fullname" : "Mary Miller",
			"zip" : "54321",
			"city" : "Small Town"
		},
		... 
	]
}
  • content of the variable information as a JavaScript file address-list.js
var some_variable_name = {	
	contacts [
		{
			"fullname" : "John Doe",
			"zip" : "12345",
			"city" : "Big Town"
		},
		{
			"fullname" : "Mary Miller",
			"zip" : "54321",
			"city" : "Small Town"
		},
		... 
	]
}
  • retrieving data from the addresses JSON expression
var first_city = cchip.user.addresses.contacts[0].city