0
votes

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.

2
Do you have all the fonts you are using on the server? Maybe you have the fonts on your local computer but not on the server.Krikor Ailanjian
Do you have your site scaled to Standard? or is this running on a Free site?cory-fowler
It's been a very long time since I worked on this, hence not posting this as an answer. But I think it's a permissions problem. I think the way PDFSharp tries to load the fonts requires more privileges than it has in an Azure Website. Try running it in a Cloud Service instead and see if that is any better. In my scenario we had to install certain fonts, which certainly requires cloud service and a fair bit of shenanigans with startup scripts and elevated execution privileges.Frans

2 Answers

5
votes

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.

0
votes

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.