PDF document from multiple images with Power Automate

The automatic generation of various documents is a very common use case for Flow. It has a built-in function (Populate a Microsoft Word template), which you can use to dynamically create Word files using your own template.

In an earlier post, I went through how to use it to create invoice attachments.

Let’s make it a bit more difficult this time. Let’s create a flow that produces a PDF document that includes n images. This could be, for example, an inspection report with pictures.

Images attached to the document are retrieved from rows in a SharePoint list.

Let’s get started.

Option 1 – Utilize a Word template

The task sounds really simple. Create a template with Word that begins with a slot for dynamically populated text content. At the end of the template is a Repeating Section Content Control, which contains a picture element for the image, as well as a text element for the image name.

Inside the repeating section, we can use flow to add 0 to n images. Easy!

However, we quickly find that the otherwise great Populate a Microsoft Word template feature does not support the placement of picture elements inside a repeating section.

That’s it then. We have to come up with something else.

Option 2 – Create an HTML document and convert it to PDF

Let’s try a bit more nerdy approach. Flow creates an html document that is converted to PDF in the end.

Let’s start with Flow by initializing a variable into which we form the content of our document in html format. Let’s add a few paragraphs of text to it.

Next, we retrieve all the images to be attached to the document.

  • Get all rows in the SharePoint list (Get items)
  • For each line, get all its attachments (Get attachments)
  • Finally, add a reference to that image in html file

Finally, let’s fill the html file and save it to OneDrive, from which it is then converted to PDF and forwarded by email.

Great! Or is it?

Well, those images don’t come into the PDF. The images are stored in SharePoint and cannot be accessed by PDF conversion.

Option 3 – An HTML document into which the images are embedded to

Even with a few failed attempts, the situation is not as bad as it might seem. Images can be embedded directly into an html file, so they can be included in the final PDF.

Let’s change the flow we made a little bit. After retrieving attachments

  • Get the contents of the attachment (Get attachment content)
  • Embed the content part of the content as text to the html page

At the same time, the width of the images is forced to 200 pixels. This way they are all the same width in the final document.

The same as text:

<img src="data:image/png;base64,@{body('Get_attachment_content')?['body']?['$content']}" width="200" />

And just like that we have a PDF document with n pictures at the end.

But…

Editing an HTML file to look good, let alone following the organization’s official visual guide, can be challenging. In addition, OneDrive’s Convert to feature only works with files smaller than 2MB.

2MB. That is one photo taken with a mobile phone. Even this approach does not seem so functional after all.

But if the html approach feels like a good solution otherwise, there is help available from encodian functions.

  • Images can be resized using (Resize image) so they do not take up space
  • HTML -> PDF conversion can be done with custom function (Convert HTML to PDF), which does not have a file size limit

However, Encodian functions require a separate subscription.

Option 4 – Create separate documents from the images and merge them

If we want to utilize the Word template, we have one more option left. What if we created a Word template with space for one image. We then create a separate Word file for each image, which we eventually merge as one file.

The most visually pleasing result is obtained if there are multiple images in one template. But for the purposes of this blog post, let’s simplify and go with one image per document.

Like so.

For the first page of the document, we create a separate template.

In flow, we start by filling in the first page of the document, after which we add the generated document to an array-type variable (doc array). All documents to be merged are collected in this variable in JSON format, including the following fields.

  • fileName (file name, not relevant to the end result)
  • fileContent (contents of the Word file)
  • wordInputFormatMode (This defines how to format the result. We use the keepSourceFormatting option, to keep the document’s own formatting)

The rest is very similar to our html approach. This time, each attachment is created as its own Word document, which is then added to the doc array variable.

All generated Word documents are merged with Encodian’s Merge Word Document– connector, then converted to PDF using OneDrive’s connector, and finally sent via email.

And so we have a PDF document.

Except…

In the Word template, all of the images are forced to a certain size. Therefore, this will not work if the aspect ratios of the images are not the same.

Option 5 – Create documents from the images with Encodian connector and merge them

As our last resort, let’s try Encodian’s Replace Text with Image feature. It can be used to replace a string in Word (or PDF) document with an image. In which case (presumably) the images are allowed to have different aspect ratios.

Let’s create a template for one image. It looks like this.

Now in Flow, the images are first reduced to 300 pixels wide (images that are too big will not fit in the document in the horizontal direction). The image is then saved to OneDrive and its contents are immediately retrieved from there. This is a lazy way to make sure that the file content is in a format that is appropriate for the connectors.

Finally, the ** image ** text in the template document is replaced with the reduced image.

And now the aspect ratios are showing as intended!

Summary

This exercise was an excellent example of creating solutions with Flow and Power Apps. There are usually several different implementation options, and each has its own pros and cons.

Which of the methods presented was the best one? Well, it depends

  • Is it possible to use a premium connector (Word Online (Business))?
  • Is it possible to use 3rd party connectors (Like Encodian or Plumsail)?
  • Does the html approach meet the document’s layout/visual requirements?
  • Are the images always in the same aspect ratio?

It is also possible to combine the presented techniques creatively. For example

  • The actual document is created dynamically with Word Online using a template and then converted to PDF
  • Image attachments are made into an html document that is then converted to PDF
  • These two PDF documents are merged into one file

Interested in reading our latest Power Platform blog posts?

You’re welcome to subscribe to our email newsletter: Forward Forever Monthly. Max 1 email per month, with a curated list of latest insights and articles on Power Apps, Power Automate, Power BI.

Leave a Reply