Generate PDF from DOCX template and SharePoint form fields

Contents:

Description

In this example, you will find a step-by-step instruction on how you can create Work Order Form from the template and send its PDF version to a specific email.

result file

We are going to do this with the help of:

Form

We will create 2 SharePoint Lists:

  • Work Order (parent)
  • To-do (child)

The Work Order List will store the information about the customer and the list of tasks. To bind the task list to the Work Order form we will use List or Library control.

Note

For more information about the control, please refer to Create and bind associated items or documents on Modern SharePoint Forms article.

In this example the To-do List has the following fields:

  • Description (text field)
  • Due date (date field)
  • Assigned To (choice field)
  • Status (choice field)
  • Work_order_id (lookup value referred to the "Work Order" list)

We are going to create simple New and Edit forms for our tasks with all the fields, excluding Work_order_id field - it will get set automatically, and we don’t want users to change it manually.

Data Source

The Work Order List Edit form will have an option to send the result as PDF file to a specific email. For this, we will add Send PDF field (Yes/No) and an Email field (Single line).

In addition, we want the Email field to be disabled if the checkbox is unchecked. The following code will do the job.

fd.spRendered(function() {
    function SendEmail() {
        if (fd.field('SendPDF').value) {
            // Setting field Email as editable
            fd.field('Email').disabled = false;
        } else {
            // Setting field Email as read-only
            fd.field('Email').disabled = true;
        }
    }
    // Calling SendEmail when the user changes Send PDF field
    fd.field('SendPDF').$on('change',SendEmail);

    // Calling SendEmail on form loading
    SendEmail();
});

We also add List or Library control to the form, where in Data Source we specify the Child list, View and Lookup filed. Editing property should be set to "Dialog".

As a result, the Edit Form is ready and looks something like this.

Edit form

Flow

Create a new Flow from blank that will start with SharePoint connector - When the item created or modified. Specify the address of your site and the name of the list.

The final Flow will look like this:

Flow

We'll create it step by step.

Add Initialize variable action. We’ll need it later to hold SharePoint Items. Specify the name, "Type" is array, and the value is blank.

Initialize variable

The Flow will create PDF and send email only if "Send PDF" is checked, so we add a condition first.

condition

"If no" section will stay blank and in "If yes" section we will add the following steps:

First, we get file content of the template file, in this case, .docx. You need to specify the SharePoint site URL and path to your file. You can use different connectors to get files from other locations, such as Box or Dropbox, for example.

File Content

We already have a template file prepared. To find out how to create your own template file, please, have a look at Create DOCX from template article to understand how the templating engine works.

Next, we will get all the items from the child list with Get items action and filter them by Parent Item's ID.

Get Items

As we need to get multiple items we do the following:

  1. Add "apply to each" control;
  2. In "Select an output from previous steps" specify a value from "Get items" action;
  3. Add "Compose" action to define current item properties;
  4. Then add "Append to array variable", select the variable name from the drop-down, and value is the output from "Compose" action.

Apply to each

Now it’s time to create the file from the template and convert it to PDF. That are two actions from Plumsail Documents.

First, we will Create DOCX from Template:

DOCX from template

And then Convert DOCX to PDF:

Convert DOCX to PDF

Eventually, we want to Send an email to the address specified in the form and attach the result PDF file to it.

Send email

We can also store the result PDF file in the SharePoint library. For that, we add a Create file action, select the site address, folder path, file name, and file content.

Save file

You can save the DOCX file as well. It can be saved to any location, such as:

  • SharePoint
  • Salesforce
  • Box
  • OneDrive
  • Google Drive
  • Dropbox
  • SFTP
  • File System