Create multiple PDFs (in a loop) and attach all of them to one email

  • 11 January 2023
  • 5 replies

Badge +6

Here’s what I’m trying to do: within a Loop for each action, generate a PDF document - then attach each PDF from the loop to a single email.

I’m creating a workflow for an employee incident report (for example, when an employee slips and falls at work). There are three elements to it: the employee’s own report, their supervisor’s report, and one or more witness reports (depending on how many witnesses there were).

The employee and supervisor reports are no problem since there is only one of each. For the witness report(s), I’m using the Loop for each action for each witness named. Within the loop, a component workflow is called to get the data from each witness (the parent and child workflows run concurrently). The component workflow data is passed back to the parent, and I use that to create a PDF document for each witness. (I’m not creating the PDF in the child workflow because I don’t believe a PDF can be passed back to a parent.)

Once all the reports are generated in PDF, I want to send all of them to our HR department in a single email. I just don’t know how to collect the witness PDFs so that they can all be attached in the email. Ideally, each PDF would be a separate attachment, but it would be okay if they were combined. (I don’t know if that’s even possible.)

I wondered if there was a Collection action that could be involved somehow, like doing an Add item to collection action after each witness PDF is generated, but PDFs are already Collections and I don’t believe you can create a “collection of collections,” as it were.

Any suggestions?

5 replies

Userlevel 3
Badge +9

Let me try to answer you via an example as I have been in a somewhat similar situation.

I store my generated PDF files in a SharePoint library as intermediate storage (and archive) and in the following example I load them in. I also load two files into a collection (witness reports in your terms). Then I send an email with the individual files as well as the collection of files.

Note that the filename is determined by the name embedded in the PDF and not the variable name.

In detail:

The email I receive contains all files. Both the individual files A and B and also the files C and D from the collection:

Hope this helps.

Badge +6

Thanks, @WouterT! I’m sure this will be a big help to me … once I can get the process started. I have to confess that I don’t know how to store the generated PDFs into a SharePoint library. I’ve added attachments to a list before, but I guess I’ve never done this. What action are you using to get the generated PDF to your SharePoint library? I’ve tried several and none seem to be what I want.

Userlevel 3
Badge +9

From what I read you already did so very well. Maybe these extra steps explain:

  1. When I generate the document I get the output in a collection of files:
  2. Then I get the first file from the collection:
  3. After which I can store that file wherever I want on SharePoint by using the Store A File action:

    Please don't hesitate to ask more if you want. Happy to help.

Badge +6

Thank you for that. I have to confess I’m still a bit confused, though.

Looking at your example, it looks like you know how many files you have for the collection of files (fileC and fileD) and you set your Run parallel paths action accordingly. In my case, I won’t know; there could be none, there could be several.

I also don’t understand, in your follow-up message, why you’re getting the first file from the collection. What about the second, third, etc.?

Userlevel 2
Badge +7

I may be at a loss here jumping into the middle of this, but do you have a need to store the PDF’s in Sharepoint?  If you’re just trying to attach all the files in a single email, I would have thought in your for each loop, after you generate the document you would pull it out of the collection (index 0), then add it to a separate collection that will store all the files to be emailed.