top of page
Search
admin

Generate PDF document using Word Online and Sharepoint connectors in Microsoft Flow

Word Online is a premium connector in Microsoft Flow. It comes with two actions:

  1. Convert Word Document to PDF

  2. Populate a Microsoft Word template

Typical use case scenario would be to generate PDF from a Microsoft Word document template populated with data of your choice. This blog will briefly highlight the key steps on how to generate the PDF from a Word document template, and to deploy the Flow with configuration from one environment to another.


Build your Word document template

Firstly, a Word document template is needed. To build one, you would need the Developer feature enabled in Microsoft Word application.


To enable the Developer feature, simply click on File menu, then Options (at the bottom left). Tick Developer on the right column, then click Ok.

Once enabled, you should see the Developer tab next to View.

Under Controls section, you can add various type of controls. So, for this blog, I have quickly added a Date (Plain Text Control), and table containing No., Title, and Price columns (using a Repeating Section Content Control, and three Plain Text Controls).



Populate Microsoft Word template

Your document may be stored in OneDrive folder or a Sharepoint Site/Document Library folder. For this blog, I have chosen a folder in a Sharepoint Site.

In the Flow designer, add the Word action 'Populate a Microsoft Word template'. Then, simply select the Sharepoint location, document library and template file. The content controls of the template, ie. the Date, and Price list table component will be automatically generated for you.

This works fine if the Flow is for Production use for the selected Sharepoint site. To make the Flow action configurable for deployment from one environment to another, use Environment Variables.


Environment Variable

A Solution is used to deploy the Flow to a target environment. Add the Flow to a Solution, then add the Environment Variables to the solution.


Two Environment Variables are needed for configurable deployment:

  1. SharepointSite variable, of Sharepoint Data Source data type. Sample value https://[company].sharepoint.com/sites/mynewsite

  2. SharepointLibrary string variable for storing the reference of Sharepoint document library. Leave the value blank and save it.


Once the variables are created, go back to the Flow which has the explicit Sharepoint location configured. Then peek the code (click on three dots, then Peek Code), then copy the value of drive element, eg.

b!ziKKIOJ1c0eDlZl-Blq_nWyyo0xD7RNBjUT3abE6v2cMgHAtt_JKSJy1C00pTumi

and paste it as value for the SharepointLibrary environment variable.

As you migrate the Flow to another environment, you can use the same approach in the target environment to retrieve the value of the target Sharepoint library, simply by inspecting the drive element in the peek code screen.


DynamicFileSchema

Now, you would want to make use of the Environment Variables created.


As the Location, Document Library and File are now using configurable elements, you would notice that the auto-generated fields for each content control in the template have disappeared. This is kind of expected as the connector now has no 'awareness' of the document template to populate.


There is only one field named dynamicFileSchema available to supply your data to be mapped to each content control. Accepted value format is simply a JSON string. The key name for each content control can be retrieved via Peek Code screen from the previous step above.

For repeating content controls in your document template, use an array variable containing each elements as JSON string.


Generate PDF Document

Now, the Flow is capable of generating new Word document from a template.


To convert the document to a PDF format, the generated Word document needs to be saved to a location first.

Once saved, use the 'Convert Word Document to PDF' action to convert to PDF.

Then, save the pdf back to a folder in Sharepoint.


That's all for now.

Recent Posts

See All

Comments


bottom of page