Reverse sheet numbers in booklet imposition for multi-page PDF clone

Hi all.

I have a process that uses a database source to pull PDF file paths and some product data, including an ID. I’m making heavy use of master pages to apply document-level information and change into barcodes and text.

There is only one section with appropriate settings to fronts, evens and odds - these are overlayed over a multi-page PDF that can be printed n times. This is achieved using a control script:

merge.template.contexts.PRINT.sections['Section 1'].restartPageNumber = true;

var printSections = merge.template.contexts.PRINT.sections;
merge.template.contexts.PRINT.sections["Section 1"].enabled = false;

var numClones = 1; // hardcoded here, but result of complex calculation in the original
var pdfPath = record.fields.InFilePath;

for( var i = 0; i < numClones; i++){
	addBackground(pdfPath);
}

function addBackground(pdfPath){
	var resourceUrl = 'file:///' + pdfPath;
	var clone = printSections["Section 1"].clone();
	clone.name = "clone_" + i;
	clone.background.source = BackgroundResource.RESOURCE_PDF;
	clone.background.url = resourceUrl;
	clone.enabled = true;
	printSections["Section 1"].addAfter(clone);
}

So far, so good, but now comes the hard part.

The resulting PDFs are imposed as booklets in output, but they need barcodes based on the following logic:

  • on every even page between 2 and half of the document’s page count it needs to display a QR code and a Code 128 barcode 2mm outside of the trim area
  • their value should consist of “NN” + records.fields.ID (using 9999 as example) + [reverse sheet number] + [sheet count]

Sheet count I got via standard script with a text selector @Sheets@:

var pdfPath = record.fields.InFilePath;
var pdf = resource(pdfPath);
var pdfPageCount = resource(pdfPath).pages;
var pdfSheetCount = pdfPageCount / 4;
var result = ("" + pdfSheetCount);
results.text(result);

Now the only headache is the reverse sheet number. The numbering should go from the middle of the booklet on the left hand sides of the inners.

So, for a 52-page booklet, the barcode on page 26 should say NN99990113, since it’s the first sheet of 13 counting from the middle of the booklet; page 24 should be NN99990213, then the numbering should progress onwards to page 2 (NN99991313). The code I have is as follows, but it’s missing the crucial component:

var field, barcodeValue, result = "";

var pdfPath = record.fields.InFilePath;
var pdf = resource(pdfPath);
var pdfPageCount = resource(pdfPath).pages;
var pdfSheetCount = pdfPageCount / 4;

var pageNumberOfTheClone = 26
//the magic 26 needs to be replaced with whatever the current page is

if((pageNumberOfTheClone > (pdfPageCount / 2)) || (pageNumberOfTheClone % 2 == 1))
{ 
	logger.info("BLANK");
	barcodeValue = "";
}
else
{
	logger.info("NOT BLANK");
	var reverseSheetNumber = pdfSheetCount + 1 - (pageNumberOfTheClone / 2);
	
	barcodeValue += "NN";
	barcodeValue += record.fields.SetID;
	barcodeValue += reverseSheetNumber;
	barcodeValue += pdfSheetCount;
}

result = barcodeValue;

results.text(result);

Should there be multiple clones, the barcode sets would be identical across them all.

Any help with this will be very much appreciated, as I’ve been at this for days and it is slowly driving me insane…

I believe I’d probably approach this in a two pass Connect document setup.

Step one would simply perform your data merge and the output preset could be levereged to add the barcodes you’d need by accessing the native sheet count variables available there.

Step two would them take that document and run it through an imposition process (have the data mapper skip content creation and just go straight to the output imposition settings).

Unless I’m not understanding something, it’s not going to work here, as I still need the value of SetID during imposition step… This is really doing my head in.

Your setID looks like it’s currently part of your datamapper, no? If that’s the case, you can pass it forward to your first output via Connect Metadata. I have a more extensive write-up here: Explanation different levels for output - Designer - Upland OL User community

I had to park this for the longest time but managed to get the correct value using a postpagination script. While I can change a human readable value of an html object, I am unable to change the value of a QR code dynamically - it just replaces it with a string upon preview. Any ideas?