Pictures in DOCX templates

DOCX templates have multiple value formatters. One of them is picture formatter. You can use it to dynamically insert pictures into DOCX documents.

Let us review a few cases examples:

{{value}:picture(100,100)}

Insert pictures from public URL

Library

The formatter accepts URLs. The URL must be publicly anonymously available (no authentication).

For the template from the picture above to work you need to use JSON object like this:

{
    "publicUrl": "https://plumsail.com/docs/documents/v1.x/_images/plumsail-logo.png"
}

Insert pictures from Base64 string

Library

Also, the formatter supports Base64 strings as pictures. Thus, for the template from the picture above to work you will need to pass something like this:

{
    "base64String": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIoAAAAyCAYAAACDHkN8AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAB3RJTUUH5AYDDA8z7aYtxwAACOdJREFUeJztnG9sE/cZx79JuByJTVKKXajjkhSnEknAFgxthKglVdaQjgotlaq9AaSplVZ4NQl4OQ3EqxVV24uJPxLbC4I0oTCYMpgWV5Q0IwTU/JkNdgjxQZy4dpwzJJc4di7nP3vh2LNjOzn77pcSdB/JSnz3u9/z2P7e83ue584uiEajUSgorEDhj+2AwtpAEYqCKNaRNtDzVxvGBiZRsVMDQ4MOFTs2kTapQADiQhHmw5gYnsLE8BT6r4+gtrkS9UdrULa5lLRpBRkhvvQI86GU53azE+0nuvG0+wfSphVkZBWEEk7bNuMN4PbZh4pY1hDEhRLiQ1n33T77EI4eN2kXFGTgR4koydy7/BizkwHSbihIhLhQgtP8svunXH7cu2wj7YaCRIgLZdozt+KYJ3fH8ej2c9KuKEiAqFACUzyEYPYcJRlFKK82RIUy7fGLHusdmVbE8gpDVCice+VlJxlFKK8uZIUiIj9JxjsyDY/9JSFvFKRAVCgTT6ZyPsYzpAjlVYSsUIZz/9A99hcEPInhsrDoOP0ALgtLzMZawGZ24i9H/p3TMcQuCnKeOQS5hZyPExtReL8A9hmXtp1WUdAayjMeM27xgelxY3erIWe/1iosw4FWUykXYe2dTsxM5NbkJCaUieHclx0AmGWDWAiEUFy6vGssM432k//Jul9v0qL+yHboTdq8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JyHUUVH8o6p4K85F71RCMQHNcQGrmGsCf7XfBioXYcB/2zs5LnUSBL3n0UAAi7vkXY24vwROyRK1TtF6Dr/5CveYVVRJJQkokucAh7ehD29CDC9iEyO4ZoMPMtkYUbt4Oq/QLU9l/LYVphFZBNKAqvN8ovVyuIQhGKgigUoSiIQhGKgigUoSiIQhGKgij+B4V7vYMAPildAAAAAElFTkSuQmCC"
}

Insert picture from Base64 string in Power Automate

A common situation is getting photos from the camera in PowerApps or pen input for signatures, converting it to Base64, and then inserting it into a docx template. In general, you can insert any image if you can get its Base64 code in Power Automate flow or even using some third-party services.

Let’s check out the next example.

We will create an invoice from the DOCX template, insert a logo, and convert the result to PDF.

The invoice template and logo reside in a SharePoint document library.

Library

We will use two Plumsail actions in my flow: Create DOCX from template and Convert DOCX to PDF.

Power Automate (Microsoft Flow)

We keep the DOCX template and the Logo in a SharePoint library.

This action gets the template from the document library:

DOCX template

And this action gets the picture logo from the document library:

Logo

Then we use Create DOCX from template action to create an invoice from the template.

In the template we need to add the tag:

{{img}:picture}

to the place where we need to insert the logo.

To correctly insert the Logo to the invoice we convert the SharePoint action result into base 64 code. To achieve this we can use next expression

base64(body('GetLogo'))

This is the JSON:

{
    "img":"base64(body('GetLogo'))",
    "invoiceNumber": "432",
    "company": {
        "email": "sales@sample.com",
        "address": "3 Main St.New York NY 97203 USA",
        "phone": "202-555-0131"
    },
    "date": "2018-05-21",
    "items": [
        {
            "product": {
                "name": "Monitor",
                "price": 99
            },
            "quantity": 10,
            "cost": 990
        },
        {
            "product": {
                "name": "Stepler",
                "price": 12.44
            },
            "quantity": 1000,
            "cost": 12440
        }
    ],
    "total": 18872.94
}

On the next step, we need to use Convert DOCX to PDF action and create a file in the SharePoint library.

Create PDF

This is the resulting PDF with the company logo

Result PDF

You can download the DOCX template here.