Dynamic Background Script

I’d like to use the Dynamic Background Script to pull in an external PDF to a print section, BUT, only if that PDF exists.

What’s happening currently is that documents with no external PDF get blank pages for that print section.

Is there a method to produce a print section with a dynamic background IF that background PDF exists on disk, and if not, skip that print section?


I don’t think there is a native way to check if a file exists in the template. I have tried to look for some documentation but with no luck, or maybe I just gave up too soon. But instead, you could make a bool in the datamapper and try to read the data file and see if you can read it otherwise skip the record.
An example script would be like this, but you can spice it up to make it work for your user case.

var fileExists = false;
try {
  fileExists = true;
  logger.info("File Exists");
catch(error) {
	logger.info("Empty Void!");

You can check if a file exists using the resource function. It’s only available in template scripts, not in data mapper scripts. You could use it in a control script to conditionally disable a print section.

Edit: I should add that the resource function used to support only relative paths and file: based urls. As of 2023.2 it also supports http: and https: based urls.

1 Like

Thanks for the tips!! Here’s my final Control Script. We send out monthly statements, well, monthly, but send out tax forms once a year. In the month of January, to save on postage, we’d like the monthly statements (which run multiple times a month) to include the tax form. For a variety of business reasons, not every statement will have a tax form.

var field, result = "E:\\pbCCMData\\GUILD\\st3\\PrintBackups\\1098\\Split\\";
var currMonth;
currMonth = new Date().getMonth();

merge.template.contexts.PRINT.sections["st3_1098"].enabled = false;

if (currMonth == 0)

  field = record.fields.LoanNumber;
  if (field !== "") result += field;
  field = merge.template.parameters.RunDate;
  if (field !== "") result += field;
  field = record.fields.AddrConcat;
  if (field !== "") result += "_" + field + "_A.pdf";

  if (resource(result))
    merge.template.contexts.PRINT.sections["st3_1098"].enabled = true;

    var background = merge.template.contexts.PRINT.sections["st3_1098"].background;
    background.source = BackgroundResource.RESOURCE_IMAGE;
    background.allPages = false;
    background.start = 2;
    background.end = 2;
    background.url = result;