Dynamically loading a PDF with a generated filename

For every customer, I have the “customer_id”, which is a string of 5-7 characters.

When creating the document, I need to dynamically insert a special PDF for each customer.

The name of the PDF is a fixed Path (e.g. “C:/pdf/”) plus the customer number formatted as a 8-digit value, padded with "0"s.

Example: customer number “12345” leads the the PDF “C:/pdf/00012345.pdf” - thus this path needs to be set as the “src”-attribute of an image (I guess…)

There are two “challenges”:

  1. how do I format the number?

I tried to a JavaScript - file that implements a “sprintf” and “vsprintf”-function, but I can not call then; they are unknown. Next try was something like substr() - as there was no change for the image, I can not tell if that one worked as expected.

  1. how do I set the attribute?

I tried with a fixed full path (to see if that would work), but no change for the image.

results.attr(‘src’, “C:/pdf/00012345.pdf”)

So: how can I generate the correct name for the PDF and apply this name to the image?

Is there any support for debugging the JavaScript-code? Unfortunately, I even didn’t manage to display a simple alert() to see the values of my varaibles.

Hello Thomas,

  1. The following zeroPad(num,places) function adds leading zeros to your customer number:
var filename = zeroPad('12345',8) + '.pdf';

function zeroPad(num, places) {
    var zero = places - num.toString().length + 1;
    return Array(+(zero > 0 && zero)).join("0") + num;
}
  1. The script line to set the image should include the file: protocol:
results.attr('src','file:/C:/pdf/00012345.pdf');
  1. You can write logger info. The result is shown in the Info panel (the blue ‘i’ in the lower right corner (adding the info to a paragraph would do the trick too ;). To write info to the logger:
logger.info('Hello World!');

Hope this is of some help.

Erik

Hi Eric,

these answers helped a lot. I was able to create the correct full filename and set it as the ‘src’-Attribute for the image (including the “file:/”-protocol).

I must admit that it took us some time to understand the code of your “zeroPad”-function, but finally we got it :wink:

To test the template, I renamed one of those customer-PDFs and created output. As expected, the PDF was missing and thus could not be included.

Is there any hint how error handling has to be done in such cases? Will the engine raise an exception or signal to tell that this document is invalid?

How can the document be ‘invalidated’ somehow to prevent such a document beeing printed and sent to the customer?

Horrido,

Thomas

I’ve grabbed the zeroPad function from the web so it’s not my code :wink:

Alternative methods are:

function pad(num, size) {
    var s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}

Or

function pad(num, size) {
    var s = "000000000" + num;
    return s.substr(s.length-size);
}

Functionality to log records that contained errors or that didn’t produce any output will be added in a future version. The idea is that you could pass that information to a separate process in Workflow and take action on the report.

For now you could run a Preflight from within the Designer (choose: Context > Preflight).