How to dynamically change barcode value via a script?

I have an HTML snippet that is used as a global footer across multiple print sections.

Within this HTML snippet is a barcode with a different value based upon the print section that is calling the snippet.

For example:

Section 1 has a barcode value of ‘PINS’

Section 2 has a barcode value of ‘FACE’

If I create a Text Script that sets the value of the barcode, the Text Script evaluates before the print sections load and the barcode value is the same between Section 1 and Section 2.

For example, if the barcode has a div id = “Code39Barcode” and the following Text Script is set:

Section 1 and Section 2 have a barcode of ‘PINS’.

I tried creating an individual .js file for each section and called it in the ‘Print Section Properties > Includes’ section to change the barcode, but, it just replaced the barcode with the text ‘PINS’.

The javascript used was the following:

document.getElementById(‘Code39Barcode’).innerHTML = ‘PINS’;

Is there a way to change the value of a barcode via a script so that each section has their own unique value?

There are multiple ways to achieve that. Here are some techniques:

Section selector
Create two scripts. Create one for each section and set the respective barcode value. Change the selector for each script so it includes the section name (section is an attribute of the underlying HTML). Eg.:

[section='Section 1'] #Code39Barcode

Section name in script
Check the section name in the script and use that in an if statement.

var barcodeVal;
if(merge.section.name == 'Section 1') {
  barcodeVal = 'FACE';
} else {
  barcodeVal = 'PINS';
}
results.html(barcodeVal);

Execution scope
Create two scripts. Create one for each section and set the respective barcode value. Subequently add two folders to the Scripts panel. Again, one for each section. Place the scripts in the corresponding folder. Right mouse click the first folder and select Properties, the Script Folder Properties dialog appears. Set the execution scope to the respective section (e.g. disabling the other sections and master pages). Click OK to store the value. Repeat these steps for the other folder.

Hope this is of some help,

Erik

1 Like

That last one is an awesome tip I did not know about. Nice!!

1 Like

Thanks, it is also a good technique to tune the performance of templates containing lots of scripts and having multiple sections and/or master pages.

1 Like

Excellent, thank you Erik, this helps greatly.

I was able to implement each technique successfully using the following:

Section selector

Section name in script

Execution scope

Nice, glad it worked. Cheers,

Erik

Slightly different issue in my case: the script is in Post Pagination and is trying to change the value of a QR code (#qr):

results.each(function(index) {

var field, result = "";

field = ((record.fields.strBookletNo + "_" + record.fields.ID + (this.info().pageNo).toString().padStart(2, "0")));
if (field !== "") result += field;

this.html(result);

});

Can you please advise how to prevent it from changing to text, @Erik ?

Hi @puszczyk,

I would say please make sure that you are changing the value of a QR code by a Standard Script rather than a Post Pagination Script because I assume that you cannot change the value of a QR code by a Post Pagination Script and because I am not able to reproduce the issue when I apply the shared JavaScript code to a Standard Script instead.

Hi @Marten, I need to do it in post because I need access to this.info().pageNo, as I know of no other way of accessing the section page number in Designer - unless there exists an equivalent of {#} in JS that I don’t know about.

A option you might concider in this case is to add the QR barcode element to each page by making use of a Job Creation Preset and Output Creation Preset. To do this you have to execute the following steps:

Job Creation Preset

  1. Create a new Job Creation Preset via: Connect Designer > File > Print Presets > Job Creation Settings…
  2. Check the option “include meta data” in the Configuration Selection window.
  3. Click on “Next” and go inside the Meta Data Options window to the Document Tags tab.
  4. Add the record fields “strBookletNo” and “ID” to the Documents Tags tab by clicking on the “plus” button on the right side of the list of Document Tags, select the option “Add field meta data”, select the record fields “strBookletNo” and “ID” and click on “OK”.
  5. Save the Job Creation Preset file by clicking on the “Finish” button.

Output Creation Preset

  1. Create a new Output Creation Preset via: Connect Designer > File > Print Presets > Output Creation Settings…
  2. Select the just created Job Creation Preset file in the Configuration Name select option on the bottom side of the Configuration Selection window.
  3. Click on “Next” and check the “Add additional content” option in the Print Options window.
  4. Click on “Next” and click on the “plus” button on the right side of the list of Additional Content in the Additional Content window and select the option: Barcode > QR Code.
  5. Click in the Additional Barcode wizard on the “plus” button on the right side of the Text input field, which you can find at the bottom of the Additional Barcode wizard, and select the option “Document meta data fields”. Here you have the option to add the record fields one by one to the Text input field. You also have the option to add the page number to the Text input field (“${page.nr}”). To be able to do this you have to select the option “Info Field” > “Document page number” instead of the option “Document meta data fields”.

The result in the Text input field will become something like:

${document.metadata.strBookletNo}_${document.metadata.ID}

The only thing that you will have to test is if you can use the methods “toString” and “padStart” in the Text input field. In case you can use these methods inside the Text input field then the result in the Text input field will become something like:

${document.metadata.strBookletNo}_${document.metadata.ID}${(page.nr).toString().padStart(2, "0")}

Thank you @Marten - managed to get the QR codes on.

Now the second stage. In output creation I use imposition as booklet and face down sequence, so that the first page of the PDF is the innermost spread. On the right hand side, in the bleed area, I need to add a barcode and human readable string with an ID ending in sheet number and sheet count of the output (so for a 40pp book it should go 0110 on page 1 of the imposed output, 0210 on page 3, 0310 on 5 etc.). Except variables relating to sheets are using the pre-imposition sheet numbers:

Any ideas?

Never mind, I was just tired and wasn’t thinking straight - my code was pointing to the wrong number.

Thanks again.