Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate

Hi friends, in one of my previous blog posts I explained how to convert HTML to PDF using PDFShift API. PDFShift is a paid product and can be used in specific scenarios 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.
 
I have created a sample SharePoint list with name as “Test” with columns ‘Title’, ‘Name’ and ‘Address’ –
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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.
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
After 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>')
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
After creating HTML file in OneDrive, we need to use ‘Convert file using path’ connector of OneDrive to convert HTML file to PDF file.
 
File Path: Path of the HTML file created in previous step
Target Type: PDF
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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.
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
Now when we get the PDF file content, we need to assign it to a Object type variable, like following.
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
We can use ‘Send an email (V2)’ action to send an email with attachment.
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
Let’s save and test our flow by creating a new item in “Test” list , like following,
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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,
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
And can see that email landed in mailbox,
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
With the contents like the following,
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
Entire workflow will look like,
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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.
 
Microsoft Power Platform - Convert HTML To PDF Using OneDrive Actions In Power Automate
 
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.


Similar Articles