2

Azure functions apparently doesn't yet support System.Drawing (sanbox info). Running a function with a dependency on it throws the following message:

System.Drawing is not supported on this platform.

I was originally using Select.HtmlToPdf in a WebApp to create PDF documents from HTML. But, since moving the PDF generation to an Azure function, that is no longer an option.

Also, the reccomended library is wkhtmltopdf, but that doesn't seem to have a .netstandard2.0 version.

How would you accomplish PDF generation using Azure Functions (C#)?


Update: the function is running on an S1 - App Service Plan.

Update2: using the method shown on OdeToCode gives the error Qt: Could not initialize OLE (error 80010106)

Ovi
  • 2,620
  • 7
  • 34
  • 51

3 Answers3

2

I made a solution recently generating PDF files from HTML pages. I also had problems finding an appropriate framework that would run within an Azure function. But i found that using wkhtmltopdf, i could generate PDF documents from HTML pages.

Here is a link: https://wkhtmltopdf.org/

I found that the minimum app service plan i could run with was: B1 service plan. This won't work with consumption plan. I did this using the experimental Powershell mode, but the same should be possible using C# or any other of the supported languages.

I am not thrilled with the PDF output, but it gets the job done. There are a lot of options to use, but it didn't seem to do much of a difference for me, other than portrait or landscape mode.

Filip Lindboe
  • 326
  • 1
  • 4
  • 1
    Thanks @Filip, did you happen to share the source somewhere? I actually ended up having my Azure Function call and AppService API that does the PDF rendering, but I'd love a single solution to the problem. – Ovi Oct 21 '18 at 16:03
1

At this point I created a new netcore2.0 Azure AppService API that supports Select.HtmlToPdf and will probably move this functionality to a container if it still doesn't work with netcore3.0 in a little while. The function just calls the API with some parameters and can wait for the PDF generation async.

As Jerry has mentioned, it might work with DinkToPdf. However, the template html docs must not contain JavaScript, so this solution is not valid for my use-case.

Ovi
  • 2,620
  • 7
  • 34
  • 51
0

For DinkToPdf, the error Qt: Could not initialize OLE (error 80070005) is caused by javascript tags or references. See the test done by Travis. When js is removed Function returns PDF successfully but the error message persists.

I find another lib OpenHtmlToPdf works for Azure Function in S1(B1+ as @Filip mentioned) App service plan. Just change BuildPdf method from the sample offered by @Scott. Haven't tested its performance.

private static byte[] BuildPdf(string html)
{
    return OpenHtmlToPdf.Pdf.From(html).Content();
}
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • Thanks @Jerry, I can't remove the javascript in my html doc that is used for PDF generation. At this point I'm having the Azure Function do an API call to an WebService that supports `Select.HtmlToPdf` and will probably move this functionality to a container if it still doesn't work with `netcore3.0` in a little while. – Ovi Oct 26 '18 at 14:23
  • 1
    @Ovi My mistake, I should have clarified that the js tag restriction is in `DinkToPdf` not `OpenHtmlToPdf`. Never mind, since you have found a valid solution, this can be an alternative option to try if interested. BTW, just find another package and I will do some tests. If it works, I will add it to my solution just for more people to refer. Anyway, good question! – Jerry Liu Oct 26 '18 at 23:19