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’ –
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.
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>')
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
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
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
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.
Now when we get the PDF file content, we need to assign it to a Object type variable, like following.
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
We can use ‘Send an email (V2)’ action to send an email with attachment.
Let’s save and test our flow by creating a new item in “Test” list , like following,
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,
And can see that email landed in mailbox,
With the contents like the following,
Entire workflow will look like,
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.
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.