0

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.

Alex
  • 1,322
  • 1
  • 20
  • 44
  • 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 Jul 22 '15 at 16:12
  • Do you have your site scaled to Standard? or is this running on a Free site? – cory-fowler Jul 23 '15 at 03:19
  • 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. – flytzen Jul 27 '15 at 09:49

2 Answers2

5

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.

Stefan
  • 166
  • 2
0

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.

Community
  • 1
  • 1
Peter
  • 1,674
  • 4
  • 27
  • 44
  • Hi Peter, I'm having the same issue as the OP. All my searches direct me to the same link in PDFSharp's documentation, but this sample is *not* complete (it actually has a "TODO" on it). I have the TTF font available and I'm following the sample but I'm stuck where it says "this.fontFamilies". What is the "this" to which it is referring? – 08Dc91wk Aug 17 '15 at 19:43
  • If you're using PDFSharp 1.50 or later, use this instead: http://developer.th-soft.com/developer/using-private-fonts-with-pdfsharp-1-50-beta-or-migradoc/ – Peter Aug 17 '15 at 21:17