PowerApps – Create Beautiful HTML/PDF Reports

Today we are back again…. Turning our boring HTML Table from PowerApps into a beautiful CSS Table with embedded images and with my limited HTML and CSS knowledge make it…. customer friendly

 

Do you know the initial setup?

If not please find my previous blog post below:
Create HTML/PDF Reports from PowerApps

Updating our previous flow:

The very first thing we are going to need to do is add a new string variable with a name we can remember!

PowerApps - initialise variable header content

Now we have our empty string… Lets do some HTML!

WAIT… But Ben… I dont know HTML….

Have no fear! W3 Schools is here! HTML Tutorial

< !DOCTYPE html >
< html >
< head >
< title > HTML Report < /title >
< meta charset=”UTF-8″ >
< meta name=”viewport” content=”width=device-width, initial-scale=1″ >

Now if I can say anything…. Please do NOT copy the code below and expect it to work… The reason for this…. Bloody Quotations!
This is a quotation that wont work
This is a quotation that will work "

Special thanks to Reddit User ‘u/Knovar’ for experiencing this little bug so we can add it to the blog steps 🙂

Just make sure your quotes, symbols and special characters are on point as sometimes HTML and Website text editors like to just change things!

Now you will notice I am not adding the < style > tag here. This is for two reasons:

1. I am going to add quite a bit of CSS here… (Don’t know CSS? W3 Schools to the rescue! CSS Tutorial) The one thing variables in PowerAutomate does not like is lines and lines of visible text.

2. I don’t want to have to scroll or search 17 times looking for the once piece of HTML or CSS that I am trying to edit or change!

Next we are going to create another empty string variable.. This time instead of header content… you guessed it… var_HTMLStyle.

< style >
/*Set the Page size*/
@page {
size: A4;
}

/* Style the body */
body {
font-family: Arial;
margin: 0;
}
#limit {
max-width: 550px;
word-wrap: break-word;
}

/* Page Content */
.content {padding:20px;}

#customers {
font-family: “Trebuchet MS”, Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}

#customers td, #customers th {
border: 1px solid #ddd;
padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #264c79;
color: white;
}
< /style >
< /head >

The above code sets some CSS Styles for my tables and my body content as well as a very important step…

It sets the page size as ‘A4’
If you intend on exporting this to PDF… You will love this little gem..

Okay home stretch we are going to replicate the above two steps..

But we are going to create a var_HTMLBody this will include
thetag as well as some text if you desire

for us it will just be < body >

Then we are going to create a var_HTMLTable

This is the fun bit… The below piece of code (which will be the content of our var_HTMLTable String variable) Defines the tables Header, the tables CSS Style and the table header columns. Notice we do not yet close the table HTML

< h2 > Ben’s Money Breakdown < /h2 >
< table id=”customers” >
< tr >
< th > Name < /th >
< th > Cost < /th >
< /tr >

We are now going to create an apply to each loop from our previous ParseJSON and do a little Append to string magic 🙂

At this stage you can delete the previous Create HTML Table control as we are about to be replacing this.

First Create the apply to each and give it the ‘body’ content from the ParseJSON Response from earlier.

apply to each & append

As you can see above we have added an Append to string variable action… Now the magic…

< tr >
< td > @{items(‘Apply_to_each’)[‘description’]} < /td >
< td > @{items(‘Apply_to_each’)[‘purchase’]} < /td >
< /tr >

This builds our table row… One at a time… in the correct order…. 🙂

It should look like this:

apply to each filled out

Now after our apply to each we have to add some important steps.
It joins all our string variables together to make our finished HTML

The steps are as follows:
1. Close our Table with < /table >
2. Close our body with var_HTMLTable < /body >
3. Close our HTML Header with var_HTMLStyle var_HTMLBody < /HTML >

It should look something like below:
finish HTML

Finally use the var_HTMLHeader as the file content for your Create File action from the first Blog

create file

Now lets run this with content from before:

new table with css

Old Table for comparison:

old table without css

Hope this helps!

Taking your PowerApps and PowerAutomate Skills the NextStep

Ben Signing off!

PowerApps – Create HTML/PDF Reports

Do you get shivers every time your client or boss asks for a report of that beautiful PowerApps Gallery you built for them?
This post will give you the tools you need to create beautiful PDF reports with and without images using our wonderful PowerPlatform and a little code magic.

The very first thing you are going to want to do is setup a Gallery or Collection in PowerApps <– This is what we will be using as our base for our report.
Lets take a look at my very basic app that tells some real truths:

Items

As you can see we have three columns – Description, Purchase and Notes
We are going to export the 4 rows of a snapshot of my life (dont tell my wife) to PowerAutomate where we will formulate our amazing HTML.
The code to do this is quite simple – but first we need to create our flow.

Items

Above you see two things:
1. The first is our PowerApps trigger. This is what allows us to call and send data to the PowerAutomate Workflows (Flows).
2. The second is an Initailise Variable step with Type Array and a nice name. We at NSC use var_ to start our variables so they are all the same scheme.

The beautiful thing about the PowerApps Connector is that it also does something else for us. As you can see it lets us Ask in PowerApps as a dynamic value option. This creates a PowerApps Variable with the name of the control:

Items

Notice the name of the variable varitemsFromPA_Value and look at the name of the initialise variable step: var_itemsFromPA_ <– take note of this.. when you are dealing with 100’s of these, your naming scheme will save your headache!

So quickly recapping… We have a PowerApp with Data and a Workflow with a setup ready to receive it… Lets join them.

Items

I have added a button to my PowerApp > I have clicked on my button > I have clicked actions in the top header > I have clicked Power Automate and Now I simply have to select my requested flow.

Items

I have now selected createReportDemo (My Workflow’s name and its adding to my button)

Items

Oh No! It failed to register… This is something you WILL run into when you are playing with Flow and PowerApps… The trick here. PowerApps can accept and receive only one thing… Text (strings, words, characters, bits) it only deals with Text…

So when we created our variable as type array that puts a request to PowerApps to send an Array reponse. It cannot complete this action so it FAILS! Don’t fear though. This isn’t a stopping point; we just have to change our functions slightly

To Remove a PowerApps Variable you have to remove the Trigger from the Flow:

Items

ItemsItems

Okay we have cleared that Variable… Now lets change the variable type:

Items

Now lets re-request our PowerApps Variable:

Items

Okay now we are ready to head back to the PowerApp.


BOOM!

Items

We are now ready to start adding our code to the no code solution! 🙂

In the function bar at the top of the PowerApp if you select this…

Items

It shows you your beautiful NAMED variable that it is asking for

These are mandatory. If you ask for two you must provide both responses. Hence why I showed you how to delete incorrect variables above… cause god dammit man we are only human!

The function layout is as follows:

`createReportDemo.Run(JSON(expenses, JSONFormat.IgnoreUnsupportedTypes))`

My collection here is called expenses (which contains all the columns and data)

We are using Ignore Unsupported Types because I dont want to deal with NULL value checking today you can read more about this here:
JSON Functions
We will cover JSON in more depth in blog posts to come!

Items

So we now have a button that will send all our data from PowerApps to PowerAutomate in a JSON format and a PowerAutomate Workflow that will grab that response and put it into a string variable for us… GREAT!

Time for the HTML…. WRONG we have to do some dumb stuff first…

Your beautiful button you created earlier… Press it.. Press it now!

Hopefully it will run your flow and you will get a succeeded response like below:

Items

We are now going to open that response:

See that beautiful Value there… right now it contains [{BLAH BLAH BLAH

This is our JSON code and what we need to copy for this step:

[
{“Notes”:””,”URI”:””,”description”:”Cat”,”editValue”:”false”,”purchase”:133},
{“Notes”:””,”URI”:””,”description”:”Wife”,”editValue”:”false”,”purchase”:999},
{“Notes”:””,”URI”:””,”description”:”Baby”,”editValue”:”false”,”purchase”:200},
{“Notes”:””,”URI”:””,”description”:”Ben”,”editValue”:”false”,”purchase”:1}
]

Okay now we have our sample code lets jump into the Workflow’s edit mode:

Insert a _Parse JSON_ action and provide the content as the var_ variable we defined earlier

Items

Now see that Schema is required … SMASH Generate from Sample and dump our copied notes from our succesful run in there

Items

Then click done!

Items

Now our JSON is entered we can continue

Add a new step – Create HTML Table

The Content for this is the Dynamic Body of our Parse JSON

I have added a comment so you can see the real expression (if you care)

Items

Okay home stretch…

This Create OneDrive for Business connector might cause you some grief… its not you… its Power Automate…

There are two solutions to resolve this error:
onedrive error

  1. Export your flow… then simply re-import your flow….. Yup…
    OR
  2. Delete your flow…. then delete your OneDrive for Business Connector from the connectors screen on the left…. Create the OneDrive for Business Connector again and finally recreate your flow

Add a Create OneDrive for Business File:

Items

The Filename MUST contain .HTML here

The File Content is the body response of our Create HTML Table

Next Add a Convert File (Preview) Action:

Here our *File: is the ID of the above created File and Type is as you request. However as the title suggests this is to PDF!

Items

Finally…

Add a Create File action… 

Here we are going to just play match the names and use the File Name and File Content of our Convert File action:

Items

And Thats it…

This final step will create our PDF and save it to the defined location. You can fancy this up but that will be part two of this post 🙂

Here is the output of this basic PowerApp and PowerAutomate Workflow:

Items
Items

Hope this helps! Read the next step here: PowerApps – Create Beautiful HTML/PDF Reports

Taking your PowerApps and PowerAutomate Skills the NextStep

Ben is now in trouble… Signing off!