Alex
Alex

Reputation: 1322

Error accessing Fonts in Azure Web App when using PDFSharp

I am using PDFSharp to dynamically generate PDF's in my web app. The web app works fine locally however, when I push the app to Azure (as a Web App), I get the following exception:

{"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.

Upvotes: 0

Views: 2453

Answers (2)

Stefan
Stefan

Reputation: 166

Azure App Service (aka Azure Websites) enforces a number of more restrictive security constraints than a cloud service or IaaS (VMs). One of the things that is blocked is access to much of the GDI API surface area, which includes some font manipulation. 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.

Upvotes: 5

Peter
Peter

Reputation: 1715

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. 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.

Upvotes: 0

Related Questions