JavaScript based configuration Quick Fix

Things that can be done with Quick Fix can be done very fast. Sometimes you can only take full advantage of that when you are using JavaScript. The "JavaScript based configuration" Quick Fix can be used as follows:

let cfg = {
	… Your Quick Fix JSON serialisation 
};
cfg; //<- return your Quick Fix object
Click to copy

The JSON serialisation for the existing Quick Fixes are documented here

Example of a JavaScript based QuickFix as Process Plan steps

In this example we have a single page PDF with 10 objects. What we want to do is to split this page so that each page in the result PDF has only one object.

We know that we can set the Crop Box to the dimensions of an object, so what we want to do is to duplicate the first page, set the Crop Box to the dimensions of the first object, then duplicate the first page again for the second object and so forth. We also know that we can determine the dimensions of an object using a Check.

So what we could now do is to create a Check that finds any objects and then loop over the result list in a Process Plan duplicating the pages and setting the Crop Box. We could use a Quick Fix for setting the Crop Box because that is much faster than a Fixup. The problem is that the solution is not really quick because all of the overhead for our Process Plan based loop.

A much smarter way is to first create as many duplicates from the first page as we need (as many objects as we have minus one because we can use the existing page). Then we create a list of Quick Fix instructions, one for each page and for the respective object that should be visible on that page and then we run Quick Fix.

The Process Plan "Single out page objects"

Step 1 to 3

1. Check "Any page object" is a single Check using the property "Is any page object". It creates a result object.

2. Variable / JavaScript "PageObjects-init" initializes app.vars.objects with all hits (all objects) and creates a variable "app.vars.nbhit" with the number of objects minus one, that is the number of pages that we need to create.

app.vars.objects = app.doc.result.checks[0].hits;

app.vars.nbhit = app.vars.objects.length - 1;

3. Action "Duplicate pages" duplicates the first page according to what has been defined in app.vars.nbhit.

Step 4 "Set page boxes using Quick Fix"

This is a Quick Fix that uses "JavaScript based configuration". It first creates its own list of instructions and then applies them on the PDF file. Let us see what it does:

const margin=5;

Specifies a margin around each object. The value is 5 [pt].

let instructions= [];

Defines an empty array that will contain our instructions (one per object/page).

Then comes a for loop that runs over our app.vars.objects array and sets the Crop Box to the dimensions (plus margin) of the first object on the first page and so forth.

for (let i = 0; i < app.vars.objects.length; i++) { 

  let llx = (app.vars.objects[i].llx - margin);

  let lly = (app.vars.objects[i].lly - margin);

  let urx = (app.vars.objects[i].urx + margin);

  let ury = (app.vars.objects[i].ury + margin);

The variable "page" is used to specify the page for the current set of coordinates. It is used in the "page_selector" object for the instructions below.

  let page = (i + 1).toString();

  instructions.push({

     "bottom" : lly,

     "from_box_or_absolute" : "absolute",

     "left" : llx,

    "page_selector" : page,

    "right" : urx,

    "top" : ury,

    "unit" : "pt",

    "when" : "always",

    "which_box" : "CropBox"

  });

}

Now we have created an array "instructions" in the syntax that is expected by the Quick Fix to set page geometry boxes and that has a set of coordinates for the CropBox and a page_selector for the page it should be used on.

We now put this into a full Quick Fix configuration:

let cfg = 

{

  "quickfixes" : 

    [

      {

        "instructions" :  instructions,

        "quickfix" : "set_page_box",

        "version" : "1.0"

      }

    ]

};

And finally we establish that configuration by calling it:

cfg;

The result