我正在编写一个用于 PDF 转换的 Azure 函数,它依赖于 DataLogics PDF 转换和用于密码生成的 Nuget 包 (mlkpwgen)。
功能有
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using System;
using MlkPwgen;
using Datalogics.PDFL;
using System.Diagnostics;
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
PDFConversion();
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
public static string PDFConversion()
{
using (Library lib = new Library())
{
String sInput = @"C:\Users\Kunal\Downloads\Indian Management.pdf";
String sOutput = @"C:\Users\Kunal\Downloads\WatermarkedOutput.pdf";
Document doc = new Document(sInput);
string ownerPassword = PasswordGenerator.Generate(length: 32);
string userPassword = PasswordGenerator.Generate(length: 32);
doc.Secure(PermissionFlags.Print | PermissionFlags.HighPrint, ownerPassword, userPassword);
WatermarkParams watermarkParams = new WatermarkParams();
watermarkParams.Rotation = 45.3f;
watermarkParams.Opacity = 0.15f;
watermarkParams.TargetRange.PageSpec = PageSpec.AllPages;
WatermarkTextParams watermarkTextParams = new WatermarkTextParams();
Color color = new Color(0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f);
watermarkTextParams.Color = color;
watermarkTextParams.Text = "Centre Code - Unit - 0101";
Font f = new Font("Arial", FontCreateFlags.Embedded | FontCreateFlags.Subset);
watermarkTextParams.Font = f;
watermarkTextParams.FontSize = 80f;
watermarkTextParams.TextAlign = HorizontalAlignment.Center;
doc.Watermark(watermarkTextParams, watermarkParams);
doc.EmbedFonts();
doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
Process.Start(@"C:\Users\Kunal\Downloads\WatermarkedOutput.pdf");
return sInput;
}
}
}
}
我收到以下异常
“System.Private.CoreLib:执行函数时出现异常:
功能1. Datalogics.PDFL:类型初始值设定项
“Datalogics.PDFL.PDFLPINVOKE”引发异常。 Datalogics.PDFL:
“SWIGExceptionHelper”的类型初始值设定项引发异常。
Datalogics.PDFL:无法加载 DLL 'DL150PDFLPINVOKE':指定的
找不到模块。 (HRESULT 异常:0x8007007E)。”
相同的代码作为控制台应用程序可以正常工作。我在这里缺少什么?
如果修复硬编码文件名仍然没有帮助,则该错误听起来像是权限异常。
Azure Functions 在应用服务上运行,该服务有一个用于所有代码的沙箱,其中不允许某些调用。例如。 GDI32 被 PDF 生成库广泛使用。
阅读更多内容Azure Web 应用程序沙箱 https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#pdf-generation-from-html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)