Wednesday, October 31, 2018

Creating PDF documents with images using Flow

One of the nice document generation features of Flow is to create PDF documents from structured data, such as HTML.  However, if you want to integrate images, you may not see the expected result.  For demonstration purposes, suppose you want to have a PDF image that only includes.  The flow may look something like the image below:


The Flow is broken down into 4 actions:

  1. Compose HTML File - used to compose the HTML body.  In it, you can see the HTML markup that points to an image (e.g. https://<YOUR TENANT>postmediacanoe.files.wordpress.com/2018/09/jackolanterns1000.jpg).
  2. Create file - this is where the HTML is converted into a temporary file, temp.html
  3. Convert file using path - here, the HTML file is converted into PDF.  Note that this action doesn't actually save the PDF file; it just converts the content.
  4. Create file 2 - now, I use the content from action 3 to save the PDF content into a PDF file.
If you look at the temp.html file, you will see the image as expected:

However, if you try to open the corresponding PDF file, the image is missing.

The reason for this is that the Convert file using path action is expecting all the content to exist within the source document (the HTML file).  So, the way to get around this is by extracting the content of the image and including it directly within the HTML body.  Let's have a look at the updated Flow:
I added the Get file content using path to capture the content of the Halloween.png file.  Next, I modified the Compose action to (specifically the <img ...> tag) to include the content of the image.  For the image content itself, you need to include the following expression:


body('Get_file_content_using_path')?['$content']


Note that the name of the body object may be different in your case.  Now, when you generate the PDF, you get the image included in it.

Final Thoughts

As you can see in the final image, the Halloween picture is larger in the PDF file than in the HTML file.  Make sure to structure your HTML carefully to properly size the images and other content.

0 comments:

Post a Comment