Reputation: 2750
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)
Upvotes: 2
Views: 7896
Reputation: 2750
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.
Upvotes: 1
Reputation: 17790
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();
}
Upvotes: 0
Reputation: 326
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.
Upvotes: 2