Debugging JavaScript Variables

pdfToolbox provides features that are designed specifically to make debugging of Script Variables easier:

  • Console
  • Serialisation of pdfToolbox JavaScript objects
  • Variable values are listed when "Log Profile Execution" is active

Console

The Console is available below the Script Editor if "Show evaluation result" in enabled.

It works in the same way as in other JavaScript editors.

Since JavaScript "snippets" may be used at various places in a Profile it is sometimes required to temporarily copy code from somewhere else into the first lines of the Script Variable you are currently working on to make sure that the Variable has the same information as when it will be used in the Profile context.

In the example above the check that would fill the app.doc.result.checks[0].hits array has to be manually performed before the code in this Variable can be debugged. Only then the variable "loc_triggerarray" has a meaningful value. It is printed to the Console via console.log using "pretty printing" with the optional parameters null,"\t".

Serialisation of pdfToolbox JavaScript objects

In order to access information in any of the pdfToolbox JavaScript objects you may need to visualise the structure of these sometimes complex objects. The most complex object is the app object since that is the parent object for all other more specific objects.

This and all other pdfToolbox JavaScript objects can be serialised by the JSON.stringify function, if used with the optional parameters null,"\t" they are formatted nicely.

This screenshot shows parts of the app object after that has been output via console.log(JSON.stringify(app,null,"\t")) and copied from the Console into the Sublime editor. The part above shows the app.doc.result.checks part with hits and their trigger values.

Variable values listed in a logfile created via Log Profile Execution

If you are using Script Variables in a Process Plan you may need to know how Variable values are changed throughout processing. You enable logging via the options menu in the pdfToolbox main window.

If switched on a logfolder will be created that amongst other things includes a log file "process.log". The other contents of this folder are explained in further detail in a different article of this documentation: http://help.callassoftware.com/m/pdftoolbox9-en/l/656888.

All Variables that are stored in app.vars so that they are available throughout a Process Plan are listed for each of the steps of the Process Plan with their current values.

This screenshot shows that the variable "hitarray" had two entries in step 3 and was empty in step 4. (The empty lines before, between and after these two steps have been added to make the example more readable.)

Debugging with MS Visual Studio Code (version 10.1)

You can use MS Visual Studio Code to debug your JavaScript variables. Then the pdfToolbox specific environment is exported to MS Visual Code so that you can truly debug in the pdfToolbox "environment".

In order to set this up you need to:

  1. Install nodeJS (https://nodejs.org/en/download/)
  2. Install MS Visual Code (https://code.visualstudio.com/) in the default location
  3. Mac Only: Install MS Visual Studio Code command-line tools.
    On Windows and Linux this should be automatically available after installing Visual Studio Code. On macOS, you need to open "View" -> "Command Palette…" and select  "Shell Command: Install 'code' command in PATH" (further information on this can be found here).

Then you can use MS Visual Studio Code for debugging.

Click on the pen symbol that will open your JavaScript in an external editor. If you have done the above steps, it will then open your JavaScript in MS VS Code and you have all features of that debugger including break points etc.