Html to Pdf library in Azure Function

[亡魂溺海] 提交于 2019-12-06 11:38:56

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.

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.

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!