Microsoft Flow – Convert any file to pdf and share as email attachment


In this post, I will be covering how we can share documents (non pdf) uploaded to SharePoint library in pdf format with users, using Microsoft Flow.

Lets take the default Documents library and upload some documents (excel, word or pdf) etc to it.

Create Flow

Get File

This trigger can be anything like when file is uploaded or when file is modified etc.

  • In the search box, type “for a selected file” and under triggers click on “For a selected file”
  • Select “Site URL” and “Library”

Get File Properties (Optional)

Required only if specific files need to be processed (based on file properties like file name etc.)

  • Click “New Step”. In the search box, type “get file properties” and under triggers, select SharePoint trigger “Get file properties”.
  • Select “Site Address” and “Library” .
  • Click on “Add dynamic content” below “Id” textbox. From the floating dialog, select “FileId”.
  • Click on “Save” to save the progress

Note: Catch in this step, “FileId” content is not present or dynamic content floating window will not show any output variables from previous step, if library selected is a “Modern” library. The workaround here is, after adding the second step, select any classic library from any Site Address in previous step and then you can see output variables in dynamic content of second step

Get File Extension (Optional)

Required only if files with specific extension need to be processed

  • Add new step “Initialize Variables”. Rename it to “Get File Name” and set the name to “filename”, type to “string” and value to “file name with extension”
  • Add new step “Initialize Variables”. Rename it to “Get FileName Length” and set the name to “namelength”, type to “Integer” and value to expression “length(variables(‘filename’))
  • Add new step “Initialize Variables”. Rename it to “Get Extension Length” and set the name to “extlength”, type to “Integer” and value to expression “sub(variables(‘namelength’),lastIndexOf(variables(‘filename’),’.’))
  • Add new step “Initialize Variables”. Rename it to “Get File Extension” and set the name to “fileextension”, type to “String” and value to expression “substring(variables(‘filename’),lastIndexOf(variables(‘filename’),’.’), variables(‘extlength’))”

Condition Check (Optional)

This step is required if above optional steps are used in the flow. This adds the logic to process files with specific extensions.

  • Add new step “Condition”. Set the parameters to check if file extension is not pdf.

Create File in Onedrive

All the steps defined under “If yes” block can be added directly after step “Get File”. if Optional steps are not used.

  • Under “if yes” block, click “Add an action” and search for “Get file content” and select the one for SharePoint. Set Site Address to “Site URL” and File Identifier to “Identifier” (dynamic content from Get File Properties)
  • Add new action “Create File” for Onedrive. Set folder path to “/” (for root folder or else select specific folder), file name to “File name with extension” (dynamic content) and file content to “File Content” (dynamic content).
  • Add new action “Convert File” for Onedrive. Set File to “Id” (dynamic content from Create File).
  • Add new action “send an email (V2)”. Set parameters like To, Subject, Body etc. Under attachments, select attachment name as “File Name” and attachment content as “File Content” (dynamic content from Convert File)

Delete File (Optional)

This step is required only if you want the file to be deleted which was created as part of the above process,

  • Add new action “Delete file” for Onedrive. Set parameter File as “Id” (dynamic content from Create File).
  • Click “Save” to save the progress.
  • Rename the flow to “pdf conversion” or any other name and click “Save”.

Complete Flow Diagram

Condition Block

Test Flow

  • Navigate to “Documents” library and select any file.
  • Under flow, select “pdf conversion”.
  • Verify the connections and continue.
  • Check the mailbox for file attachment and also the file in Onedrive

Hope this helps someone with same requirement.

Note: Since there is no “Convert File” action is available for SharePoint, Onedrive for business actions like “Create File”, “Convert File” etc. are used in the process.

Some actions are still in preview so use in production is not suggested. Check this link for preview program – https://flow.microsoft.com/en-us/blog/flow-preview-program/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s