Microsoft Power Platform: Convert HTML to PDF using OneDrive Actions in Power Automate

Entire Flow
Entire Flow

Hi friends,

In one of my previous blog post I explained how to convert HTML to PDF using PDFShift API. PDFShift is paid product and can be used in specific sacnorao like converting to PDF by passing the URL of the page and other complex sacnorao.

If you do not have any complex requirement and you have data coming from SharePoint or anywhere from Office 365 ecosystem, then you can user OneDrive actions to convert HTML to PDF.

Following example will show you how to read the data for newly added list item and convert it to PDF and send an email it as attachment. OneDrive license needed for users.

I have created a sample SharePoint list with name as “Test” with columns ‘Title’, ‘Name’ and ‘Address’ –

Temp list columns and data
Test list columns and data

Created a flow with ‘item created’ event. Selected my SharePoint list and added ‘Get item’ activity to get the newly created item. You can change it according to your requirement and according to suitable event for your requirement.

Trigger workflow on item created
Trigger workflow on item created

After this getting the list item, we can create a HTML for that item. I used following formula to get the HTML string for list item –

Value : concat(‘<html><body><table height=”100%” width=”100%” style=”background-color:powderblue”><tr><th>Title</th><th>Name</th><th>Address</th></tr><tr><td>’,outputs(‘Get_item’)?[‘body/Title’],'</td><td>’,outputs(‘Get_item’)?[‘body/Name’],'</td><td>’,outputs(‘Get_item’)?[‘body/Address’],'</td></tr></table></body></html>’)

Initialize variable for HTML string
Initialize variable for HTML string

Once we get created the HTML for list item then we need to use ‘Create file’ action of OneDrive to create a HTML file.

File Name: concat(outputs(‘Get_item’)?[‘body/Name’],’.html’)

File Content: HTML string created in previous step

Create HTML file
Create HTML file

After creating HTML file in OneDrive, we need to use ‘Convert file using path’ connector of OneDrive to convert HTML file to PDF file. Please note that this action is “Preview” state.

File Path: Path of the HTML file created in previous step

Target Type: PDF

Convert HTML file to PDF
Convert HTML file to PDF

Now save the converted PDF content to a PDF file using ‘Create File’ action of OneDrive.

Folder Path: root (/)

File Name: concat(outputs(‘Get_item’)?[‘body/Name’],’.pdf’)

File Content: File Content from previous step

Create PDF file from converted HTML content
Create PDF file from converted HTML content

Now as we have created a PDF file, we need to get the content of the file to send it as attachment in email.

To get the PDF content we can use ‘Get file content’ action of OneDrive.

File: ID of the file created in previous step.

Get PDF file content
Get PDF file content

Now when we get the PDF file content, we need to assign it to a Object type variable, like following.

HTML and PDF file gets created in OneDrive root
Initialize variable Object to hold PDF file content

As we wanted to send the PDF file as attachment to email, we need to create a Array type variable and add the PDF file content we gathered in previous step using JSON format.

Name: concat(outputs(‘Get_item’)?[‘body/Name’],’.pdf’)

ContentBytes: We got from previous step

HTML and PDF file gets created in OneDrive root
Initialize variable of Array type and append to array variable

We can use ‘Send an email (V2)’ action to send an email with attachment.

HTML and PDF file gets created in OneDrive root
Send an email (V2) with attachment

Let’s save and test our flow till this point by creating new item in “Test” list , like following –

Temp list columns and data
Temp list columns and data

Wait for some time to get the workflow triggered.

Now if we check in OneDrive root, we can see that HTML and PDF file gets created at root –

HTML and PDF file gets created in OneDrive root
HTML and PDF file gets created in OneDrive root

And can see that email landed in mailbox-

Email received in Inbox
Email received in Inbox

With the contents like following –

Received pdf in email
Received pdf in email

Entire workflow will look like –

Entire Flow
Entire Flow

As you can see that we are creating HTML and PDF file in OneDrive root, if we don’t want those files and wanted to be deleted at the end we can use ‘Delete File’ action from OneDrive. We will call the ‘Delete File’ action twice, once for HTML file and then again for PDF file like following.

HTML and PDF file gets created in OneDrive root
HTML and PDF file gets created in OneDrive root

Now, no file will occupy space in OneDrive as those will get deleted at the end of the flow. I have exported the flow and you can download and import in your tenant.

I have attached the exported flow Download and rename it to .zip

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: