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…