JavaScript controlled fixups - how to build
When using the "Arbitrary JavaScript controlled Fixups" feature, the most difficult part is to find the correct syntax for the fixups you want pdfToolbox to create for you. Luckily there are a number of tools you can use to make your life easier. This article describes all of them so you can effectively use them. To follow along, create a new fixup, search for the "Arbitrary JavaScript controlled Fixups" fixup type and select it. You should end up with an empty JavaScript editor.
The tools we're looking for are available from the "i" button in the right hand corner of the JavaScript editor. We're going to be looking at five of these menu items in this article.
While we're not going to focus here on the bottom three menu items, these are identical to what is available for regular JavaScript variables. There's a separate help item that talks about this functionality: article.
Insert base Fixup...
To use this functionality, start with the following code in the JavaScript editor:
const configuration = <place cursor here>;
configuraration.
The code creates a configuration object. Put the text cursor at the spot marked with "<place cursor here>" in the example code. (the angular brackets <> and the text in between can of course be deleted from the code; they're just there so you would know where to place your cursor for the next step). Now click the "Insert base Fixup..." menu item from the "i" button.
This opens up a regular fixup editor window. Configure this fixup exactly how you want it. For this example, configure a simple fixup as follows:
Configure the settings of the fixup exactly as you need them to be, and configure also the information on the left hand side of the editor (name, description, custom ID....). When the fixup looks exactly as you want it, click the "OK" button. The JavaScript code that represents the fixup you just configured, is now inserted into the JavaScript editor, at the location of the text cursor.
You can now format this inserted code to fit the rest of your code, and adjust it to add any additional logic you need.
As fixups often use preflight checks in "Apply to" properties (as also visible in the example), there is a corresponding menu item called "Insert base Check..." in case you need JavaScript that represents a check.
Insert configured Fixup...
An even better way to get started, is to use "Insert configured Fixup". Using this is quite similar to using a base fixup, with the exception that you build the fixup you want to insert first. Follow these steps:
- Create a new fixup.
- Configure it exactly how you want. Make sure it has correctly-configured properties, a good name and description, and so on.
- Save the fixup and test it on some documents to make sure it behaves exactly as you want.
- Create a new fixup.
- Search for and select "Arbitrary JavaScript controlled Fixups".
- In the JavaScript code editor, insert the same base code as used for "Insert base fixup".
- Click the "i" button, and then "Insert configured Fixup...".
- A window opens that lets you select the fixup you had previously configured and tested. Select it and click OK.
The JavaScript code that represents the fixup you just configured, is now inserted into the JavaScript editor, at the location of the text cursor. You can now format this inserted code to fit the rest of your code, and adjust it to add any additional logic you need. The advantage of this method, is that you can prepare your 'template' fixup and test it properly before generating the JavaScript code for it.
As fixups often use preflight checks in "Apply to" properties (as also visible in the example), there is a corresponding menu item called "Insert base Fixup..." in case you need JavaScript that represents a check.
Open list of all base Fixups
One additional problem is how to know what the names for the fixups are, and what values each property supports. This information can be found by using the "Open list of all base Fixups" menu item under the 'i' button. It opens a very long page in you favorite web browser. In there you can for example search for "rotate_pages" and you'll find all properties and values for that fixup.