简体   繁体   English

使用PDFSharp时在Azure Web App中访问字体时出错

[英]Error accessing Fonts in Azure Web App when using PDFSharp

I am using PDFSharp to dynamically generate PDF's in my web app. 我正在使用PDFSharp在我的Web应用程序中动态生成PDF。 The web app works fine locally however, when I push the app to Azure (as a Web App), I get the following exception: 该Web应用程序在本地运行良好,但是,当我将该应用程序推送到Azure(作为Web应用程序)时,出现以下异常:

{"message":"An error has occurred.","exceptionMessage":"Internal error. Font data could not retrieved.","exceptionType":"System.InvalidOperationException","stackTrace":" at PdfSharp.Fonts.OpenType.FontData.CreateGdiFontImage(XFont font, XPdfFontOptions options)\r\n at PdfSharp.Fonts.OpenType.FontData..ctor(XFont font, XPdfFontOptions options)\r\n at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font, XPdfFontOptions options)\r\n at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font)\r\n at PdfSharp.Fonts.FontDescriptorStock.CreateDescriptor(XFont font)\r\n at PdfSharp.Drawing.XFont.get_Metrics()\r\n at PdfSharp.Drawing.XFont.Initialize()\r\n at PdfSharp.Drawing.XFont..ctor(String familyName, Double emSize)\r\n at Spiro.Services.OrderService.GetOrderLabel(Int32 id, Nullable`1 quantity)\r\n at Spiro.Web.Controllers.WebApi.V1.OrderController.GetOrderLabel(Int32 id, Nullable`1 quantity)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

I create the text as follows: 我创建文本如下:

var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Calibri", 30);


gfx.DrawString(rows[i], font, XBrushes.Black,
    new XPoint(XUnit.FromMillimeter(10),
               XUnit.FromMillimeter(10)),
               XStringFormats.TopLeft);

I can see that the Azure server has a bunch of fonts installed - I'm stuck as to what the problem is... Thank you for your help in advance. 我可以看到Azure服务器已安装了一堆字体-问题出在哪里,我感到困惑...谢谢您的提前帮助。

Azure App Service (aka Azure Websites) enforces a number of more restrictive security constraints than a cloud service or IaaS (VMs). 与云服务或IaaS(VM)相比,Azure App Service(又名Azure网站)实施了更多限制性更高的安全约束。 One of the things that is blocked is access to much of the GDI API surface area, which includes some font manipulation. 被阻止的事情之一是可以访问GDI API的大部分表面区域,其中包括一些字体操作。 As other folks have noted, if the same code works on a plain IaaS VM, a cloud service, or even a local desktop/laptop, then the problem you are running into is a hard block on the underlying GDI calls. 正如其他人指出的那样,如果相同的代码可以在普通的IaaS VM,云服务甚至本地台式机/笔记本电脑上运行,那么您遇到的问题就是对基础GDI调用的硬性限制。

If you have access to the font file and you can deploy that with your project, you could load that font file in as detailed in PDFSharp's documentation here . 如果您有权访问字体文件并且可以将其与项目一起部署,则可以按照PDFSharp文档中的此处的详细说明加载该字体文件。 This would be the preferred way to ensure that the font is available, regardless of the environment you deploy to. 无论部署到哪种环境,这都是确保字体可用的首选方法。

You will also find some more information on this SO post . 您还将在此SO帖子中找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PDFsharp MigraDoc字体在Azure Web App中不起作用 - PDFsharp MigraDoc Fonts not working in Azure Web App 在PDFsharp中使用专用TTF字体 - Using Private TTF Fonts in PDFsharp PDFSharp WPF 1.32私人字体不适用于Azure Blob存储 - PDFSharp WPF 1.32 private fonts will not work with Azure Blob storage 使用SignalR时的Azure服务总线或仅Azure Web应用 - Azure service bus or just Azure web app when using SignalR 无法在Azure WebSite中使用PDFsharp使用XFont - Cannot use XFont using PDFsharp in Azure WebSite 在 Azure 应用服务 Web 作业中使用 X509KeyStorageFlags.EphemeralKeySet 时出错 - Error when using X509KeyStorageFlags.EphemeralKeySet in Azure App Service Web Job 访问Azure AD B2C锁定的Azure功能应用程序时收到403错误 - Receiving 403 error when accessing Azure function app locked down by Azure AD B2C 在开发机器上运行Azure Web App时数据库访问错误 - Database access error when running azure web app on dev machine 尝试在Azure App服务上使用WEB API时出错 - Error when trying to use WEB API on Azure App service Azure web 应用程序出现 BadImageFormatException 错误 - BadImageFormatException error on Azure web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM