Ahh PDFs, the document type businesses love and developers hate…especially when the ability to create a PDF using existing data isn’t handled out of the box in PowerApps. Luckily for us, there is a neat way to get around this limitation by utilizing Flow and some basic HTML!
Recently, a client asked us to enable users to generate a PDF version of a report that was commonly viewed inside of a PowerApp. By utilizing the sorting and filtering capabilities that are native to PowerApps, we were able to deliver the requested report, and then extend the solution by adding the ability to download a PDF of their generated report.
A HTML version of the data inside PowerApps was created, which was then sent to Flow. From inside Flow, we were able to utilize the ‘Create File’ and ‘Convert File’ actions to convert the HTML to a PDF!
Let’s Do It!
The first step is getting our data into HTML format inside of PowerApps. In our scenario, we created a table version of the data that looks like this:
We set a variable named tableHTML that pulled in data from our data source based on the filters that the user selected:
It may not be the prettiest code, but it gets the job done! You’ll see in the snippet above, the Concat function is being used to create a row for each item inside of our table. For each cell inside the row, we use the value from the data source (with some formatting for date values and for multiple select options).
In our scenario, our client specifically requested a table, but you can format your data in HTML any way you’d like. You can also add an inline stylesheet at the top of you HTML string by just using the <style> tag and setting the css in there.
Now that we have out TableHTML set, it’s time to send that data to Flow to do the conversion!
Create the Flow
Inside our Flow, we used the PowerApps trigger to allow our Flow to be called directly from our PowerApp:
Since we’re only receiving one input from PowerApps (the HTML string) we only need to have one variable initialized when the Flow starts:
The variable is of type string, and is now set to the HTML that we’re sending from PowerApps (we’ll cover how to actually send the data to Flow from PowerApps later in this post).
Now that we have the HTML string, we want to create a HTML file. For this, we’ll use the OneDrive ‘Create File’ action and set the File Content to our HTML variable:
Now that the HTML file has been created, we can convert that file to a PDF by using the OneDrive action ‘Convert file using path’ (setting the File Path to the Path from the Create HTML file action):
Now we want to Create the actual PDF file by using another ‘Create File’ action and set the File Content to be the content of the conversion action:
Now that we have the File created, we’ll need to create a shareable link that we can send back to PowerApps:
The final step in the Flow is to Respond to PowerApps with our newly created share link.
Call the Flow from PowerApps and Handle the Response
Back inside our PowerApp, we need to connect our Flow we just created. Under the action tab, select “Flows” and connect the flow you just created:
We now have our Flow connected and we’ll need to call it somewhere inside of the PowerApp. We address this by creating a button that we’ll call “Download PDF” that will handle the call and response from Flow:
Inside of the OnSelect method of this button, we have the code to generate the HTML shown earlier in this post as well as the following code:
This snippet Sets a variable “PDFResponse” to be to result of the Flow we created. Since the response is a link to our newly created PDF document, “PDFResponse” is a link to the document that we can use inside of the “Download” action to download the PDF to the users machine!
You have now successfully created a PDF report directly from PowerApps, give yourself a nice pat on the back!
Let us know your thoughts in the comments below!
Mary · July 30, 2020 at
Hi, good Blog,
But for me PDF format missing, if i have 6 columns ,only able to see first 3 columns , why so, what i have to do to get exact replicate of html?
please do needful
Ramesh · September 25, 2020 at
Does it make a difference if we store the pdf in SharePoint (from Onedrive) and passing the weburl of SharePoint? My flow return blank to Power Apps and nothing happening on button click. When I checked the return variable value in Power Apps it shows blank.