Power Apps / Power Automate – Generate PDF with different font styles

19 steps

Introduction:

As of writing, the Convert file to PDF action of Power Automate does not render the font-family styling of HTML. This approach will solve that problem but it will need a premium license.

Prerequisites:

  1. Power Automate premium -Doing it the same as the tutorial will require you an additional Power Apps premium license per user. But with a workaround, you only need 1 Power Automate premium license no matter the number of users.

Step 1:

In this tutorial, we will use a SharePoint site named ‘Test Site’ with a URL of ‘https://[domain].sharepoint.com/sites/TestSite’. In the SharePoint site home page. Do the following:

  1. Click on ‘Documents’
  2. Click on ‘New’
  3. Click on ‘Folder’. Name the folder as ‘HTML Folder’.

Checking:

You should have an empty folder named ‘HTML Folder’ in your Shared Documents.


Step 2:

Create a blank canvas app in Power Apps and create a Power Automate flow by doing the following:

  1. Select the Power Automate tab from the left pane
  2. Click ‘Create new flow’

Step 3:

In the PowerApps (V2) trigger. Add the following inputs:

HTML Content


Step 4:

Add the action below:

Create file

Site Address:

https://[your domain]/sharepoint.com/sites/TestSite

Folder Path:

Shared Documents/HTML Folder

File Name:

index.html
File Content:

Step 5:

Go to settings of Create file. Turn Allow Chunking to Off so that the file will overwrite existing ones.


This is the part where we will create the zip file which is required by the Adobe PDF action. All credit goes to Paul Murana from TachyTelic. We have used the code provided on this website to create the set of flow actions.

Step 6:

Copy the code below to your clipboard:

{
  "id": "b7af8f36-c669-4d9f-bb7d-bf14-63edb83a",
  "brandColor": "#8C3900",
  "connectionReferences": {
    "shared_onedriveforbusiness": {
      "connection": {
        "id": "/providers/Microsoft.PowerApps/apis/shared_onedriveforbusiness/connections/shared-onedriveforbu-05f1da5e-297d-4a80-8df7-cb78-b654b8a3"
      }
    },
    "shared_sharepointonline": {
      "connection": {
        "id": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline/connections/de645f5680b74c47bbce762c8e2d06ac"
      }
    }
  },
  "connectorDisplayName": "Control",
  "icon": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=",
  "isTrigger": false,
  "operationName": "CreateZip",
  "operationDefinition": {
    "type": "Scope",
    "actions": {
      "accessToken": {
        "type": "Compose",
        "inputs": "@outputs('SharePointHTTP')?['body']['ListSchema']['.driveAccessToken']",
        "runAfter": {
          "SharePointHTTP": [
            "Succeeded"
          ]
        },
        "description": "Collects the Access Token required to access the files"
      },
      "Select": {
        "type": "Select",
        "inputs": {
          "from": "@body('SharePointHTTP')['ListData']['Row']",
          "select": {
            "name": "@item()['FileLeafRef']",
            "size": "@item()['SMTotalSize']",
            "docId": "@{item()['.spItemUrl']}&@{outputs('accessToken')}",
            "isFolder": "@if(equals(item()['FSObjType'],'1'), true, false)"
          }
        },
        "runAfter": {
          "accessToken": [
            "Succeeded"
          ]
        },
        "description": "Reformats the output of SharePoint HTTP action ready for submission to the Zip endpoint"
      },
      "Attachment_Items": {
        "type": "Compose",
        "inputs": {
          "items": "@body('Select')"
        },
        "runAfter": {
          "Select": [
            "Succeeded"
          ]
        },
        "description": "Additional formatting of the Select array"
      },
      "downloadZip": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_sharepointonline",
            "operationId": "HttpRequest",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
          },
          "parameters": {
            "dataset": "@body('SharePointHTTP')['ListSchema']['.mediaBaseUrl']",
            "parameters/method": "POST",
            "parameters/uri": "/transform/zip?cs=@{body('SharePointHTTP')['ListSchema']['.callerStack']}",
            "parameters/headers": {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            "parameters/body": "zipFileName=test.zip&guid=@{guid()}&provider=spo&files=@{encodeUriComponent(outputs('Attachment_Items'))}&oAuthToken="
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "Attachment_Items": [
            "Succeeded"
          ]
        },
        "description": "Submits the request to the media server and downloads the Zip file"
      },
      "StoreZip": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_onedriveforbusiness",
            "operationId": "CreateFile",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_onedriveforbusiness"
          },
          "parameters": {
            "folderPath": "/__PA_Test/Filled Word Docs",
            "name": "testFile.zip",
            "body": "@base64ToBinary(body('downloadZip')['$content'])"
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "downloadZip": [
            "Succeeded"
          ]
        },
        "description": "Stores the received Zip file in OneDrive",
        "runtimeConfiguration": {
          "contentTransfer": {
            "transferMode": "Chunked"
          }
        }
      },
      "settings": {
        "type": "Compose",
        "inputs": {
          "libraryPath": "/sites/PowerAutomateText/Shared Documents",
          "zipFolderPath": "/DeclarationTemplateFilled"
        },
        "runAfter": {},
        "description": "Set library path to the location of the SharePoint document library that contains the documents for compression. Set zipFolderPath to the relative path of the Folder that you want to archive."
      },
      "SharePointHTTP": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_sharepointonline",
            "operationId": "HttpRequest",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
          },
          "parameters": {
            "dataset": "https://accendo1.sharepoint.com/sites/PowerAutomateText",
            "parameters/method": "POST",
            "parameters/uri": "_api/web/GetListUsingPath(DecodedUrl=@a1)/RenderListDataAsStream?@a1=%27@{encodeUriComponent(outputs('settings')['libraryPath'])}%27&RootFolder=@{encodeUriComponent(concat(outputs('settings')['libraryPath'], outputs('settings')['zipFolderPath']))}",
            "parameters/body": "{\"parameters\": {\"RenderOptions\": 4103}}"
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "settings": [
            "Succeeded"
          ]
        },
        "description": "Retrieves the file and folder information required to create a Zip file"
      },
      "Create_file": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_sharepointonline",
            "operationId": "CreateFile",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
          },
          "parameters": {
            "dataset": "https://accendo1.sharepoint.com/sites/PowerAutomateText",
            "folderPath": "/Shared Documents",
            "name": "testWordFile.docx",
            "body": "@base64ToBinary(body('downloadZip')['$content'])"
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "StoreZip": [
            "Succeeded"
          ]
        },
        "description": "Stores the received Zip file in a SharePoint document library",
        "runtimeConfiguration": {
          "contentTransfer": {
            "transferMode": "Chunked"
          }
        }
      }
    },
    "runAfter": {},
    "description": "A scope to create a Zip file in Power Automate without the use of 3rd Party Connectors or Premium Actions"
  }
}

Step 7:

Add an action in the flow, then click on the ‘My clipboard’ tab. Paste the copied code by pressing Ctrl+V on your keyboard.


Step 8:

An action called ‘CreateZip’ should appear. Select this action.


Checking:

You should have a scope named ‘CreateZip’. If you open it, there are 8 actions inside of it.

Note: If there are yellow triangles on the actions, you need to create or select an existing connection by clicking on it.


Step 9:

In the action named ‘settings’, change the Inputs parameter to:

{
  "libraryPath": "/sites/TestSite/Shared Documents",
  "zipFolderPath": "/HTML Folder"
}


Step 10:

In the action named ‘SharePointHTTP’, change the Site Address parameter to:

Test Site - https://domain name].sharepoint.com/sites/TestSite

Note: Choose from dropdown


Step 11:

Delete the last two actions named ‘StoreZIP’ and ‘Create file 2’.


Step 12:

Add the following action:

Convert Static HTML to PDF

If you do not have an Adobe account yet, create one in https://www.adobe.com/go/getstarted_powerautomate

Once created, provide the Client Id and Client Secret in the appropriate fields.


Step 13:

Once the Adobe account is set up, change the following parameters:

Convert Static HTML to PDF

File Name

Generated PDF.zip

File Content (paste in Expression tab)

base64ToBinary(body('downloadZip')['$content'])

Include Header Footer (optional)

No

Step 14:

Add the action below:

Create file 2

Folder Path

/

File Name

Generated PDF.pdf

File Content


Step 15:

Go to settings of Create file 2. Turn Allow Chunking to Off so that the file will overwrite existing ones.


Step 16:

Add the action below:

Create share link

File

Link Type

Edit

Link Scope

Organization

Step 17:

Add the action below:

Respond to a Power App or flow

link =


Checking:

Rename the flow as Generate PDF using Adobe. Save the flow and exit. You should return from the Power App studio.

The flow named Generate PDF using Adobe should be in your Power Automate pane.


Step 18:

On the canvas of your Power app, insert an HTML Text named HTMLText1. Change the following properties:

HTMLText property

"
<div style='font-family:Times New Roman; font-size:25px'>TIMES NEW ROMAN</div>
<div style='font-family:Arial; font-size:25px'>ARIAL</div>
<div style='font-family:Garamond; font-size:25px'>GARAMOND</div>
<div style='font-family:Cursive; font-size:25px'>CURSIVE</div>
"

Step 19:

Insert a button named Button1. Change the following properties:

Text property

"Generate PDF"

OnSelect property

Launch(GeneratePDFusingAdobe.Run(HtmlText1.HtmlText).link)

Conclusion:

To test it out, activate play mode. click on the ‘Generate PDF’ button and wait for it to open a PDF on the other tab. You will see that the different family fonts got rendered properly unlike in the built-in Power Automate action of ‘Convert file’.


Did this article help? Let us know how we can improve. Send us a message by clicking the “Contact Us” button below.

 

Article last updated on July 18, 2024


Need expert guidance on Power Apps?