Fixup: Dynamic spot color mapping with Arbitrary JavaScript Controlled Fixups

While a lot of things can be done in pdfToolbox without JavaScript support, in some cases it's unavoidable. A prime example is something like spot color mapping (changing the name and/or color mix of a spot color) of a variable number of spot colors.

Because this can be challenging to set up in JavaScript, this article introduces three different ways this can be done that you can simply adjust without knowing too much JavaScript.

Arbitrary JavaScript controlled what?

pdfToolbox contains a way to use JavaScript to create checks and fixups. To find them, create a new fixup and search for the fixup type: "Arbitrary JavaScript controlled Fixups". Instead of regular user interface elements, you'll only see an area where you can write JavaScript.

Using this fixup type, you can write JavaScript that creates a specific JavaScript object as the result. This object is interpreted by pdfToolbox, and real fixups are created from it.

Demo file

Download the demo file belonging to this article here:

This file contains two spot colors:

  • "Green" used as an accent color,
  • "CutContour" used for the cut contour (shown as magenta in the file).

Using JavaScript and an array

The most straightforward way to set up the Arbitrary JavaScript controlled Fixups is by sticking to JavaScript (at least if you're comfortable with JavaScript). To do so, the JavaScript begins with a small list:

// A JavaScript array that contains the remappings we want to do
const mappings = [
	{
		sourceName: "Green",
		destinationName: "Magenta",
		destinationColor: [ 0, 100, 0, 0 ]
	},
	{
		sourceName: "CutContour",
		destinationName: "Dieline",
		destinationColor: [ 100, 0, 0, 0 ]
	}
];
Click to copy

This JavaScript array contains the spot color to search for, and what to remap it to (both the new name and the new alternate color values).

Download the example here and try it out on the demo file:

Using a JSON file

But what if we separated the list of colors from the actual fixup? That way, we can modify an external file with color mappings, and just pass the path to this file as a variable to the fixup?

Surely, that must be easier to maintain?

Well yes, that is possible. In order to do so, we create a small JSON file. Then we pass the path to it to our JavaScript controlled fixup and read the content of the file on the fly. Download the JSON file, and the modified fixup that takes advantage of it here:

Using a CSV file

If you're not that comfortable with JavaScript, a separate JSON file is still annoying. A CSV file would be much easier, right? Well, that can be accomplished as well in much the same way.

Because pdfToolbox contains a real JavaScript engine, you can easily read a CSV file and re-create the color mappings from that, instead of from a JSON file.

The CSV file used and the example to use it can be downloaded below again: