Home » PDFs in PowerApps: Use a PowerAutomate flow to OneDrive

PDFs in PowerApps: Use a PowerAutomate flow to OneDrive

  • by
powerapps-powerautomate-pdf-flow
Want to do a random act of kindness? Share this post.

As you could have guess from my previous blog post, I started using Microsoft’s Power Platform intensively for a project in healthcare. A thing that surprised me is how easy it is to generate PDFs, for example for generating specific lists, using PowerAutomate. That is, if you’re fond of OneDrive. I certainly am no expert in these technologies, but this short tutorial is something I can imagine many people are looking for.

The OneDrive part

First, create a new folder on your OneDrive, where you will be exporting the PDF files to.

The PowerAutomate part

Although, you’re able to find Microsoft PowerAutomate (previously known as Microsoft Flow) at this link, you can also find it in your PowerApps environment. I will start from the latter. First, create an Instant flow, from blank.

You will be triggering this flow from PowerApps, so obviously, choose PowerApps. I named my flow ‘GeneratePDF’.

Add a new step to the flow by clicking New Step. Search for ‘create file’ and select the OneDrive for Business Create file action.

Let’s make both the file name and the content dynamic by asking for both in PowerApps. In my opinion, Microsoft still has some work on PowerAutomate as some elements of the UI aren’t really intuitive. You will have to press See more to create another piece of Ask in PowerApps dynamic content. Once all fields are filled in, you can save and add another step.

The file that we will be creating will be an HTML file, because that’s how we can add markup to our PDF. However, we don’t want a HTML file. To convert it to a PDF, use the Convert file action. The File we want to convert is the output from the previous step of our flow. You can access this by selecting id through the dynamic content window. Once again, save and

Finally, create another Create file action. Select the File name and the File content from the dynamic content window.

You can save this flow and head over your App.

The PowerApps part

I created an example app in PowerApp. It has a Database icon that loads data into a collection. It has a gallery that lists the data in the collection. It has a combobox where users can choose an item from the collection and gets its DefaultSelectedItems from the selected item in the gallery, through a context variable. Finally, two labels to show you the problematic behavior of the combobox.

This is the collection, which is created at the OnSelect of the icon.

ClearCollect(
    TestCollection,
    Table(
        {
            fruit: "apples",
            count: 5
        },
        {
            fruit: "pears",
            count: 3
        },
        {
            fruit: "cherries",
            count: 2
        }
    )
);
UpdateContext({selectedFruit: ""})

The Items of the gallery are filled with the collection TestCollection. Once someone selects an item from the fallery, it preloads the combobox with the selected item, by loading the selectedFruit variable with its own value.

Select(Parent);
UpdateContext({selectedFruit: ThisItem.fruit})

To set the OnSelect property of the Document icon, navigate to Action in the menu and by selecting Power Automate.

Select the flow that you just created, and then we can fill it with the following function. The first part of the Run() function contains the value for the Dynamic Content we need as the File name in our flow. The second part is a piece of HTML. It contains the default html, body and table tags and then… it concatenates all the rows of the TestCollection into a sequence of <tr> and <td> that create the rows and the cells of the table.

GeneratePDF.Run(
    Concatenate(TextInput1.Text,".html"),
    "<html><body><table>" & Concat(
        TestCollection,
        "<tr><td>" & fruit & "</td><td>" & count & "</td></tr>"
    ) & "</table></body></html>"
)

In our OneDrive folder we specified earlier, we can now find the PDF that has the following content.

Obviously, besides the table, we did not specify any other markup. But feel free to add a title, or a logo, or anything you want.

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Want to do a random act of kindness? Share this post.

Leave a Reply

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