JavaScript imposition - building an imposition configuration

The last article explored how to get started with a JavaScript runlist. We ended it with just boilerplate code to create a new Impose object and return it to pdfToolbox for processing. If you're following along, your code should now look like this:

// Create an imposition object
const imposition = new Impose({
	unit: "mm",
});

// Imposition logic missing...

// return the imposition object to pdfToolbox
imposition;
Click to copy

This article completes this into a working, albeit simple, imposition configuration. Several imposition concepts are explained along the way. In order to do this, we're using an example PDF file for a movie ticket, that needs to be printed on a larger sheet, essentially a 2-up configuration. To follow along, download this PDF file:

You can of course use other PDF files as well, but this article is going to assume the dimensions of the example file. It is 90 by 45 mm, with a bleed all around of 3mm.

Creating a sheet definition

In an imposition runlist, imposed sheets can only be created based on a sheet definition. So the first thing we need to create in our JavaScript is a proper sheet definition. The width and height of the sheets we want to create are:

const sheetWidth = 10 + 3 + 90 + 3 + 10;
const sheetHeight = 10 + 3 + 45 + 3 + 10 + 3 + 45 + 3 + 10;
Click to copy

This gives us enough space for two tickets (vertically), with 6 mm of bleed around each ticket, 10 mm of space outside of the tickets and 10 mm in between the two tickets. Creating the sheet definition is then easy:

const sheetDefinition = imposition.addSheetDef( sheetWidth, sheetHeight ); 
Click to copy

The "Impose" object in pdfToolbox has convenience methods to create sheet definitions, slots, sheets and more. As we already set the units we want to use when the Impose object was created, we can use the simple version of the "addSheetDef" function which only specifies the width and height of the sheet in the default unit.

Creating the slots

On this sheet, we need two slots as we want to place two tickets on each imposed sheet. To create these slots, we also have convenience functions. They take the ID of the sheet definition we just created as a parameter. The second parameter is the slot definition, which is a JavaScript object with properties for the slot. The definition of the first slot is shown in the code below, the second slot is identical, except for the bottom and left parameters.

const slot1 = imposition.addSlot( sheetDefinition, {
  "rect": { "bottom" : 10 + 3, "left": 10 + 3, "width": 90, "height": 45 },
  "bleed": { "bottom": 3, "left": 3, "top": 3, "right": 3 },
  "crop_marks" : { "left_bottom": "LB",
     "left_top": "LT",
     "right_bottom": "RB",
     "right_top": "RT",
     "length" : 5,
     "gap": 3 },
});
Click to copy

While slots can have many more properties, the ones in the code above are the important ones for this use case. Rotation, scale, positioning, and other properties can be left on their default values.

Creating imposed sheets

Now that we have a sheet definition with proper slots defined, we can use it to create an imposed sheet:

const sheet = imposition.addSheet( sheetDefinition );
Click to copy

The only parameter is the sheet definition. On this imposed sheet, we now place the first page of the input document into both slots (in this example, we handle only the one-page input document case, of course a loop can be created to repeat this for all other pages as well).

imposition.addPage( sheet, slot1, 0 );
imposition.addPage( sheet, slot2, 0 );
Click to copy

The "addPage" convenience function, uses the ID of the sheet and of the slot. The last parameter is the index of the page that must be placed into the slot. In the example above, that's page "0" as page indexes here are zero-based.

After doing this, our code is complete and we can test it by clicking the "Test" button. The full JavaScript code can also be downloaded below for your reference.