Power Automate Auto-Print PDF’s – Part 1 – The Cloud Bit

The Power Automate Flow

The Problem

A project I am working on with my employers has recently presented a need to automatically print delivery notes from the warehouse management system. Unfortunately there is not an API available to us, nor are the delivery notes localised anywhere – they simply exist at the moment of printing them. This requires a solution.

The Solution

Possibly not the most elegant of solutions, but so far, extremely reliable.

It was noted that when the warehouse finalise loading, an automatic email can be generated to send the delivery note direct to the customer with the generated PDF attached. What this meant, is that we could ‘intercept’ the PDF on sending and generate some kind of process that would print the document. For this I can leverage both Power Automate Flow and Power Automate Desktop. As this process potentially runs 24/7, it’s essential PAD Unattended RPA add-on licensing (or whatever similar licensing is available… I’ll be honest, I think it’s a mess but it’s also the wild-west frontier) is used.

The How-to

I don’t want to teach anyone to suck eggs, this may be useful to no one, but it may be useful to anyone who is looking to automatically print documents en-masse.

Firstly, I wanted to make sure that we were always privy to the automatically generated email. In this environment we use Office 365, but depending on your email service of choice, results may vary. Within Exchange Admin Center under Mail Flow > Rules, I created a new rule which basically says if the subject of an email matches a specific string of text and the email also includes a PDF attachment, it should BCC in our Power Automate ‘service’ 365 account mailbox.

With that out the way, we could go to work on a Power Automate Flow.

Fairly straight forward flow, the trigger is essentially if the email is received from the specified email address and contains an attachment, run the flow.

As I am only interested in PDF’s and Power Automate unfortunately sees things like images in a signature as part of the attachements, I have created an Apply to Each condition for the attachments that says if the Attachments ends with ‘PDF’ run the next step, otherwise do nothing.

If we hit the ‘If yes’ criteria, this is where things get a little more exciting.

For this flow, we need the local server to send the file to the printer, therefore we somehow need to get the file on to local storage. As the server already has PAD installed for various other tasks, the server has the Microsoft On-Premise Gateway installed (we’ll cover this another time) – essentially though, we can connect via secure credentials from the cloud to the local machine’s network share.

I really like this as once the Gateway is setup, it’s extremely simple to use and gives you a fair bit of granularity in creating the files. In any case, I have created a folder for ‘ToDo’ print jobs (kill me for my naming conventions) and essentially the print job is the PDF attachment, named exactly as it is included in the email.

Once the file has been created, we can use the PAD flow connector for the same server to trigger off the print job.

I’m going to to cover PAD off in part two, but there’s a few things going on here.

You can’t do this part of the flow unless you have the PAD flow created, the connector needs to know what flow to run and it has to be available for it to appear in the drop down list (I think that’s fairly common of most Power Automate flows) but I just wanted to share something here for info.

In the first step, you can see a parameter called FileName – this is an input variable created within the Power Automate Desktop flow. On the second condition you can see a dynamic content option called ‘TeamsNotification’ – this is an output variable also created in the flow. This is a super neat feature which allows you to send variables from the cloud and desktop flows back and forth to one another. Once you’ve started using that, it really opens up potential for a lot…

Anyway, my Condition 2 contains a bit of text that is generated back from the PAD flow – essentially a confirmation of whether the job has worked successfully or not. For monitoring purpose whilst this is new, I am leveraging this result in to a message within Teams which leads me on to the next step…

Depending on the message inside of he TeamsNotification, determined by the Condition 2, we branch off in to a yes/no scenario.

In theory, you don’t need to do this, however I had chosen to do this because I wanted to add some formatting to the Teams message to show when it really was a problem. This is very easily done by highlighting the dynamic content as you would normal text and just applying the text formatting to it.

The reason we did this was for a small team of people to keep an eye on the frequent running flows for a short period just to check things are working as expected. This can be a bit annoying, but essentially it’s our jobs so we’re used to it – when the flow runs though, I get the following notification from our service account:

A couple of ways to do this – my preference was to do it via a group chat, rather than channel… but that’s based purely on my own taste.

Finally, the last part of the flow is to move the received email in to a folder for archiving:

Very simply using the Move email (V2) and selecting the current Message Id and moving it to specific folder in the recipient mail box.

To be honest, the flow is solid and has ran thousands of times without error – one thing I should investigate more is error handling in Power Automate Flow (this is in my long list of jobs to do) however there is some logging done in the PAD flow which I will cover in Part Two.

Is there a better way of doing this? Have I missed anything glaringly obvious? Please leave me a comment if you think there’s a better way of doing this or if it has helped you at all!

Part Two can be found here! Power Automate Auto-Print PDF’s – Part 2 – The Local Bit

One thought on “Power Automate Auto-Print PDF’s – Part 1 – The Cloud Bit

Leave a Reply

Your email address will not be published. Required fields are marked *