System.Private.CoreLib:无法加载文件或程序集“System.Threading.AccessControl”。系统找不到指定的文件

问题描述 投票:0回答:1

我正在构建一个函数,可以从字符串在内存中生成 PDF。我安装了 HtmlRenderer.PdfSharp 来完成该功能。当我将以下函数添加到我的代码中并再次运行时,我遇到了问题(我什至没有在我的主函数中调用此函数)

        private static byte[] GeneratePdf(string htmlContent)
        {
            byte[] res = null;
            using (MemoryStream ms = new MemoryStream())
            {
                var config = new PdfGenerateConfig();
                config.PageSize = PageSize.A4;
                var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(htmlContent, config);
                pdf.Save(ms);
                res = ms.ToArray();
                return res;
            }
        }

当我调用 HttpTrigger 函数时,出现下一个错误:

 System.Private.CoreLib: Exception while executing function: Function1. System.Private.CoreLib: Could not load file or assembly 'System.Threading.AccessControl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

这是我的项目配置:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
      <ImplicitUsings>enable</ImplicitUsings>
      <Nullable>enable</Nullable>
      <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
      <PreserveCompilationReferences>true</PreserveCompilationReferences>
      <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="HtmlRenderer.PdfSharp" Version="1.5.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.32" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.32" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
    <PackageReference Include="RazorLight" Version="2.3.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
    <ItemGroup>
        <None Remove="Views\Shared\template.cshtml" />
    </ItemGroup>
    <ItemGroup>
        <EmbeddedResource Include="Views\Shared\template.cshtml">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </EmbeddedResource>
    </ItemGroup>
</Project>

按铃吗?

.net azure-functions pdf-generation
1个回答
0
投票

我正在构建一个从 hmtl 内容生成 PDF 的函数

生成 pdf 表单 html 字符串的替代方法是使用以下代码:

using System.IO;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using DinkToPdf;
using System.Net;

public static class RithPdfGeneratorFunction
{
    [Function("RithGeneratePdf")]
    public static async Task<HttpResponseData> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req,
        FunctionContext executionContext)
    {
        var logger = executionContext.GetLogger("Rithwik is Generating PDF");

        try
        {
            var rithhtmlContent = await new StreamReader(req.Body).ReadToEndAsync();
            var pdfBytes1 = RithGeneratePdf(rithhtmlContent);

            if (pdfBytes1 != null)
            {
                var response = req.CreateResponse(HttpStatusCode.OK);
                response.Headers.Add("Content-Type", "application/pdf");
                response.Headers.Add("Content-Disposition", "inline; filename=output.pdf");
                await response.Body.WriteAsync(pdfBytes1);
                return response;
            }
            else
            {
                return req.CreateResponse(HttpStatusCode.InternalServerError);
            }
        }
        catch (Exception ex)
        {
            logger.LogError($"Error: {ex.Message}");

            return req.CreateResponse(HttpStatusCode.InternalServerError);
        }
    }

    private static byte[] RithGeneratePdf(string htmlContent)
    {
        var rithconverter = new BasicConverter(new PdfTools());
        var doc = new HtmlToPdfDocument()
        {
            GlobalSettings = {
                PaperSize = PaperKind.A4,
            },
            Objects = {
                new ObjectSettings() {
                    HtmlContent = htmlContent,
                }
            }
        };
        return rithconverter.Convert(doc);
    }
}

csproj:

    <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="DinkToPdf" Version="1.0.8" />
    <PackageReference Include="HtmlToPdfConverter.Core" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
    <PackageReference Include="System.Data.OleDb" Version="8.0.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

Output:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.