Attach a print section multiple times to an email context

Let me start by explaining what I have working now.
I have an email context and I have several print sections: lets start with Invoice and Duplicate Stamp.
My xml has basically this structure:

<invoices>
  <invoice>
    <invoicenumber>I123456</invoicenumber> 
    <originalpath/>
    <invoicelines>
      <invoiceline>
        <product>book 1</product>
      </invoiceline>
      <invoiceline>
        <product>book 1</product>
      </invoiceline>
    </invoicelines>
  </invoice>
</invoices>

The Invoice section is enabled by a control script and the Duplicate Stamp section is disabled by another control script. This combination sends a simple email with an invoice attached as pdf.
However, when the invoice has been generated before and we now want to send a duplicate, workflow makes sure that the originalpath node contains an url to the original invoice. In that case a control script disables the Invoice section and and enables the Duplicate Stamp section. This section doesn’t generate an invoice, it only generates a stamp. This control script places the original invoice in the background.

var sectionName = 'Duplicate Stamp';
var section = merge.template.contexts.PRINT.sections[sectionName];
if (!section) throw "'" + sectionName + "' not found";
section.enabled = (isBilling && isDuplicate);
if (section.enabled) {
	section.background.source = BackgroundResource.RESOURCE_IMAGE;
	section.background.allPages = true;
	section.background.url = originalPath;
}

This works perfectly fine.

A second setup we have in place (actually in the same template, but using a different datamapper) is reminders. The xml now looks like this:

<reminders>
  <reminder>
    <remindernumber>R234567</invoicenumber> 
    <reminderlines>
      <reminderline>
        <invoicenumber>I123456</invoicenumber>
        <originalpath>path to original invoice I123456</originalpath>
      </reminderline>
      <reminderline>
        <invoicenumber>I654321</invoicenumber>
        <originalpath>path to original invoice I654321</originalpath>
      </reminderline>
    </reminderlines>
  </reminder>
</reminders>

In this case both the Invoice section and the Duplicate Stamp section are disabled and a third section is enabled: Reminders. This combination sends a simple email (obviously with another text than in the case of an invoice) with a reminder pdf attached. This reminder pdf basically lists all overdue invoices. The originalpath nod is simply ignored.

This also works perfectly fine.

And now comes the hard part. What I now want to do is not only add the reminder section as a pdf, but also a copy of each invoice with the stamp on it.
I am aware of Dynamically add multiple attachments article, but following that article would simply attach a copy of each invoice without putting the stamp on it.

Is there a way to attach the stamped invoices directly, without having workflow first generate each duplicate and have the template attach these pregenerated stamped duplicates?

Thanks for any suggestion.

Hello @Roebie , welcome to our online community!

As for the question asked; is it not just better to create the PDF output for the email attachment separately from creating the email content and use the Dynamic attachments: creating file names based on data fields approach for adding the email attachment in all three cases?

I could do that, but then the dynamic attachments come before the attached print context (the Reminders section), which I explicitly don’t want. Is there a way to have the attached print context first and the dynamic attachments last?

No, the order of email attachments* is fixed and cannot be changed.

*The order of email attachments is as follows:

  • Inline image attachments (in the order in which they are encountered in the email).
  • The contents of the Print context, in the form of a single PDF attachment.
  • Attachments specified in the head of the email. These can be dynamic.
  • The output of the Web context, as a self-contained HTML file.

Source: Email attachments - OL Connect 2025.1 Help

Actually, there is, kind of…
Just like I can create the pdf output for the duplicated invoices separately from the email, I can also create the pdf output for the reminder print section separately. Then, in a third step, I create the email (with “Attach Print context as PDF” unchecked) and I add both the print section output and the duplicates output as dynamic attachments. And by doing this, I decide the order.
Maybe not the most efficient way of doing it, but it gives me the result I’m after.