.net - 如何结合使用 wkhtmltopdf 和 A​​zure Functions 将 HTML 转换为 IMAGE?

标签 .net azure azure-functions wkhtmltopdf html-to-pdf

我有一个 AF,我向它传递一个带有网站的参数,该函数创建一个带有转换为图像的 html 的 blob。究竟如何使用 wkhtmltopdf 执行此操作,因为我知道一些 .exe 需要上传到我的 Azure 函数附近的某个位置,但不完全理解整个过程。我从网站下载了 wkhtmltopdf 安装程序,然后将其安装在我的 Windows 计算机上。我需要在 Azure 上做什么?有教程或类(class)吗?

我正在使用这个:https://github.com/mrdaghestani/HtmlPI 但不明白这部分:

First you have to install wkhtmltopdf on application server (your developing system or client's systems)

如何在 Azure 上安装它以便我的 Azure Function 能够在那里工作?

[FunctionName("htmlToImage")]
public static async Task<HttpResponseMessage> 
Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = 
null)]HttpRequest req, TraceWriter log)
{
string url = req.Query["url"];

byte[] EncodedData = Convert.FromBase64String(url);
string DecodedURL = Encoding.UTF8.GetString(EncodedData);

string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
DecodedURL = DecodedURL ?? data?.name;

HtmlConverter htmlConverter = new HtmlConverter();
var filePath = htmlConverter.Convert(new GenerateSettings { Url = DecodedURL, OutputType = OutputType.Image });

//string BlobName = "myblob1";
//string ImageURL = await CreateBlob($"{BlobName}.png", (byte[])..., log);

var Result = new HttpResponseMessage(HttpStatusCode.OK);

//
// JSON
//
var oJSON = new { url = ImageURL, hash = BlobName };
var jsonToReturn = JsonConvert.SerializeObject(oJSON);

Result.Content = new StringContent(jsonToReturn);
Result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

return Result;
}

最佳答案

您可以通过 Kudu 访问 Function 应用的文件系统,您可以通过 https://{您的函数应用名称}.scm.azurewebsites.net 进行访问,然后选择“调试控制台”>“CMD”或“PowerShell”。

关于.net - 如何结合使用 wkhtmltopdf 和 A​​zure Functions 将 HTML 转换为 IMAGE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52929364/

相关文章:

c# - 如何获取 mscorlib.ni.lib (.Net Framework 3.5) 的 PDB 文件

c# - 使用 C# 禁用窗口的动画效果

Azure 服务总线主题触发器未命中断点

azure - 抛出异常 : 'System.InvalidOperationException' in Microsoft. Extensions.DependencyInjection

c# - 审核添加到 Azure 表存储的行

c# - 在 Azure Function 中覆盖 function.json 时出现问题

c# - 指定的端点均不可到达

c# - 使用异步线程的退出代码退出应用程序

.net-core - System.Runtime 4.2.1.0 构建 v2 Azure Functions 项目的 FileNotFoundException

c# - 使用 Autofac 在 Azure Functions 启动类中未解析 ILogger<T>