c# - Azure 是否支持 NReco pdf 生成器?

标签 c# azure azure-functions

我已经创建了一个用于 pdf 生成的 C# Azure 函数,并且正在使用 NReco pdf 生成器,但它无法在 Aazure 上运行。

您能否建议一种让它在 azure 中运行的方法?

我已通过 NuGet Package Manager Console 安装了 NReco Pdf 生成器,但收到以下错误:

"Access to the path 'D:\Program Files (x86)\SiteExtensions\Functions\1.0.12599\wkhtmltopdf' is denied.

这是抛出异常的堆栈跟踪:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
at System.IO.Directory.CreateDirectory(String path)
at NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs()
at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput[] htmlFiles, String inputContent, String coverHtml, String outputPdfFilePath, Stream outputStream)
at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfFromFile(String htmlFilePath, String coverHtml)
at VRProductions.Repository.PdfGenerationRepository.<SendMail>d__1.MoveNext()

这是用于生成 pdf 的代码:

var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdfFromFile(blockBlob.Uri.AbsoluteUri, "");

最佳答案

我能够使用以下函数代码生成 pdf..看看这是否有任何帮助..Azure 函数 V1

using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace FunctionApp41
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");


            var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
            var pdfBytes = htmlToPdf.GeneratePdfFromFile("<file_path_including_sas_token>", "");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Content = new StreamContent(new MemoryStream(pdfBytes));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

            return response;
        }
    }
}

如果您从 Visual Studio 本地运行此函数..将生成文件,并将 pdf 加载到浏览器中或要求保存。

http://localhost:7071/api/Function1

关于c# - Azure 是否支持 NReco pdf 生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55789672/

相关文章:

azure arm 模板将 azure key Vault 扩展部署到 VM

Azure SQL 数据库更新性能

java - 如何根据用户定义的计划调用 Azure Function

c# - Azure Blob 触发函数存储帐户连接

c# - 使用 C# 运行交互式命令行 exe

c# - 如何在 ASP.NET Web API 中为 Json.NET 设置自定义 JsonSerializerSettings?

c# - switch语句是如何执行的?

c# - 如何修复对象和其他初始化程序的 ReSharper 缩进

c# - Azure 函数中的 EventGridTrigger - 日志依赖项注入(inject)不起作用

azure - 从 Power BI 触发 Azure 函数