Introduction:
Recently I was working on Microsoft Flow and one of the interesting actions is ‘Convert File’ which can take HTML files as input and convert them into a PDF file. You can hook this Flow to a SharePoint List (via SPFX extension) and can be used to generate a PDF file on the selected list item. Refer to the screen below. The user selects the SharePoint list item; The ‘Download As PDF’ button appears and when clicked, it opens the PDF version of the selected list item with some custom branding.
Step1:
As you can see from the below image, Flow is triggered via HTTP request, and we pass the List Item ID as a query string parameter. Using the Compose action and trigger function grab the list item ID from the request and pass that value to the ‘Get Item’ action which queries the targeted list. Store retrieved content in a variable of type Object. We will use this variable at a later stage.
Step2:
Let us apply some custom logo so it appears when we generate the PDF file. Add a ‘Get File’ action and grab an image from the SharePoint document library. Convert this image to dataURI format, so HTML can render it.
Step3:
At this stage, we have the image and the list data. Create the HTML table using the Compose action. To keep things simple, pull only a few columns from the queried list and use them to create the final HTML table.
Before PDF conversion, Flow needs this HTML to be saved the same where. In my case, I store it in my OneDrive library. Once you have the HTML, provide the URL in the Convert file action.
Once you have the PDF file, add an HTTP response action, and send the PDF file back to the requestor
Here are the complete Flow actions.
Step 4: Testing:
Save the changes and test the Flow. The best way to test this is using Postman. Copy the URL from the HTTP request and append it with the valid Item ID. Click on Send and make sure you get the PDF file back with all the values and image.
Step 5: Execute this Flow via SPFx Command set extensions.
Now we have the Flow up and running. It is time to hook this to a SharePoint list via SPFx extensions. If you are new to SPFx extension I refer you to read this Microsoft blog which details how to get started. Look for the Command Set file and insert the code below. Grab the selected Item ID and call the Flow. Also, make sure the PDF command is visible only when one item is selected in the list.
Scope for improvements:
You can use OData query and query only the columns you need. Also, you can use actions like Parse JSON to read the output. There is an excellent blog from John Liu on this. http://johnliu.net/blog/2017/12/taking-pdf-snapshot-of-list-items-for-approvals-with-flow
Another thing you need to be careful of here is with the call. As you can see the Flow HTTP request is anonymous. Anyone with a valid Item ID can execute the request. Store only non-secured contents in the list or add additional validation in the Flow, so it executes only when it is called via SPFx extensions.
We hope you found this blog useful in learning how to download SharePoint list item as PDF using Microsoft Flow using help from SPF extensions. Please reach out to us so that we can put our decades of SharePoint and Microsoft 365 expertise and experience to work for your organization’s Digital Transformation journey.