Three ways to convert HTML to PDF using Microsoft Flow

The ability to generate PDF’s as part of a business process is a common one – mainly driven by compliance. A common way to do this is to create a HTML file and then convert that to PDF.

<TLDR>Go to the conclusion for a summary of the approaches</TLDR>

In Microsoft Flow, there has been options for doing this for a while now. For a start, there are two 3rd party flow actions available that are part of a broad suite of tools for managing and manipulating documents. They are Muhimbi PDF and Plumsail Documents.

image

Both offerings are simple to set up and use. They also allow some configuration and tuning. At the time I wrote this article, both allow you to specify page size and orientation, and Muhimbi has a couple of extras like letting you make password protected PDF’s. As they are both commercial tools, these will come at a cost which depends on how many PDF’s you produce. But in saying that, the cost is not particularly excessive.

Below I show you both actions in use. Each one is followed by an action to save the PDF into my OneDrive – easy peasy…

image

But I am cheap Paul!

For those of you who do not have a budget, or are simply cheap-assed, there is also the OneDrive Convert File action. This one works by saving a HTML file to OneDrive (or OneDrive for Business). You then pass that file into the Convert File action and save the resulting PDF. So instead of two steps like the ones above, you have three.

image

Head over to John Liu’s page for a great example of this technique…

Now there is only one teeny problem with this. Not so long ago Microsoft broke it and as I type these lines, it remains broken but with a commitment to get it fixed…

image

Now by the time you read this it may well be fixed, but you need to be aware of another limitation with this approach. Unlike Plumsail and Muhimbi, this converter does not honour css page breaks. Therefore your PDF can end up looking pretty ugly as content wraps over pages in ugly ways…

Is there another option? Why bother?

So you might be thinking, okay so just use one of the commercial offerings while Microsoft sorts out a fix? After all, even if it costs you a few backs, you can always go back to the cheap version later.

I indeed attempted this but I had an issue with my Flow that precluded it. I had no problem signing up for both Muhimbi and Plumsail, but when I added the actions to my flow, I was met with this type of error. My flow simply did not like using 3rd party connections it seemed.

Unable to process template language expressions in action ‘Convert_HTML_to_PDF’ inputs at line ‘1’ and column ‘2336’: ‘The template language expression ‘json(decodeBase64(triggerOutputs().headers[‘X-MS-APIM-Tokens’]))[‘$connections’][‘shared_muhimbi’][‘connectionId’]’ cannot be evaluated because property ‘shared_muhimbi’ doesn’t exist, available properties are ‘shared_sharepointonline, shared_onedriveforbusiness’. Please see https://aka.ms/logicexpressions for usage details.’.

Now this error is the subject of an open case with Microsoft so I will update this post when I get an answer. <update> It turns out that for flows with a PowerApps trigger, you need to disconnect and reconnect it to PowerApps to start working</update>. But in the meantime I had a deadline and had to demo PDF creation to a client. So I decided to make an Azure function and call it from flow – after all it sounded like a perfect scenario for that technology right?

Now I won’t cover the Azure function stuff in depth here, except to say I tried a heap of HTML to PDF approaches and not a single one worked properly. Eventually I worked out that Azure functions restrict the use of GDI+ libraries. Quoting from the linked article…

For the sake of radical attack surface area reduction, the sandbox prevents almost all of the Win32k.sys APIs from being called, which practically means that most of User32/GDI32 system calls are blocked. For most applications this is not an issue since most Azure Web Apps do not require access to Windows UI functionality (they are web applications after all).

However one common pattern that is affected is PDF file generation

Eventually though I was able to ascertain that if you provision your azure functions using an app service plan instead of a consumption plan, it will work. The reason for this is the latter runs on dedicated virtual machines.

Of course now you are up for hosting costs for your app plan. Unless you already have an Azure function app provisioned for other purposes, this is no longer free.

Once I got past the Azure function issues with GDI support, I was easily able to find and use a pre-existing HTML to PDF function found here. This uses a tool called wkhtmltopdf which is a pretty powerful PDF generation library. I simply added the necessary files and configuration and was able to test it successfully in minutes.

image

Finally all I needed to do to call this function was to create a HTTP action in Flow like so…

image

Yay! I had my PDF’s!! Even better, this approach does not have the page break issues that the built-in one does!

Conclusion (and comparison)

So here is a little table that summarises the approaches…

 

Method Cost Page Breaks Features Complexity
OneDrive Convert File Action Free No Basic Low
Plumsail HTML to PDF action Not Free Yes Medium Low
Muhimbi HTML to PDF action Not Free Yes Medium+ Low
Azure function Not Free1 Yes Advanced Medium2

 

1 You will have to pay for the azure function app subscription, but many orgs will have one already so might be very low.

2 I marked this as medium if you are doing basic stuff, but if you want to do stuff like set page size and orientation, you are having to edit code directly so could be classified as High.

 

Now for my real use-case, I would likely use one of the commercial offerings, but if the organisation was going to do a lot of PDF generation, then the Azure function approach could be quite cost effective. Additionally, expanding the code to deal with additional options might also be justified.

I think the key point is that I was able to quickly work around this issue and deliver good outcomes for my client. So they are not adversely impacted while I wait for the various issues to be resolved.

Thanks for reading

Paul Culmsee

www.hereticsguidebooks.com

23 Comments on “Three ways to convert HTML to PDF using Microsoft Flow

  1. Very interesting comparison. I came across the same issue trying to convert SharePoint app HTML to PDF and ended up using JSPDF but it is very basic compared to the paid for solutions. I wasn’t aware of the OneDrive option, nice blog post!

  2. Im impressed, I should say. Pretty rarely do I come across a blog thats each informative and entertaining, and let me let you know, youve hit the nail on the head. Your blog is significant; the problem is some thing that not sufficient men and women are talking intelligently about. Im seriously happy that I stumbled across this in my search for one thing relating to this issue.

    Office 365 Support

  3. Hi Paul, great post. I’m just getting started with Azure functions, so would you mind providing a bit more information on how you got wkhtmltopdf to work? i.e. which files to include and so on?

  4. Hi dear, I tried to make it through the one drive, and saved a .html file in the one drive, however, in flow the option “HTML file” is not coming, what should i do to get it ?

  5. The corresponding set of tools for performing the various file manipulations allows you to perform the operations faster. However, some problems are inevitable.

  6. Do you know of a way to convert an HTML table to an image format rather than .pdf? Complicated use case, but the abbreviated version is I want to place the resulting table image into a word doc through automated image content control via Flow and then convert the full word doc to a .pdf.

  7. Im getting error:
    [Error] run.csx(6,7): error CS0246: The type or namespace name ‘OpenHtmlToPdf’ could not be found (are you missing a using directive or an assembly reference?)

    What I’m missing here?

  8. Can you explain a bit more how to use azure function.
    ” I simply added the necessary files and configuration and was able to test it successfully in minutes.” What files do you add and where?

  9. Hi, I tried your method with the Azure function. I do get my PDF but the itch is that all special caracters aren’t shonw correctly like ( é, à, è, ë) that we use often in france.

    I tried to put it in utf8 encoding but con’t figure out how to do it correctly.

    Thanks

  10. I tried to use One Drive convert file for converting an HTML file to PDF file but table style, image & colors are not coming in PDF however HTML is as desired.

  11. I had the same problem, I guess it can only handle basic html, use the azure function, it worked for me.

  12. Does anyone know how to control page breaks for html text input in Powerapps as i am merging 3 html text inputs to create one pdf and i want all html text inputs on different pages i.e. 1st html text input in 1 or maybe 2 pages then 2nd html text input on other page i.e. on 2nd or 3rd depending how many pages html text input 1 takes.
    it wont take any CSS whatsoever. Any help is highly appreciated.

  13. Hi Team,

    while i convert the html to document its creating in json format and when i attach file it says wrong format.any idea about it

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.