We use PDFSharp (the GDI+ build) in one of our web applications. In one PDF exporter we’re using a non-system truetype font and it works like a charm in our dev environment but crashes when we run it in production.
The key difference (I think) between our dev and production is that our production servers are running on Windows Server 2008 64bit while our dev is running 2008 32bit. I wrote a tiny test program to debug.
try
{
new XFont("ocrb10", 10, XFontStyle.Regular, new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always));
}
catch (Exception exc) { Console.WriteLine(exc.StackTrace); }
The error message is InvalidOperationException: Internal error. Font data could not retrieved.
at PdfSharp.Fonts.OpenType.FontData.CreateGdiFontImage(XFont font, XPdfFontOptions options)
at PdfSharp.Fonts.OpenType.FontData..ctor(XFont font, XPdfFontOptions options)
at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font, XPdfFontOptions options)
at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font)
at PdfSharp.Fonts.FontDescriptorStock.CreateDescriptor(XFont font)
at PdfSharp.Drawing.XFont.get_Metrics()
at PdfSharp.Drawing.XFont.Initialize()
at PdfSharp.Drawing.XFont..ctor(String familyName, Double emSize, XFontStyle style, XPdfFontOptions pdfOptions)
I built PDFSharp from source and added some debug code in order to understand what’s happening. The problem is that the pinvoke call to GetFontData returns -1 (GDI_ERROR). The PdfSharp author has added comments about this in FontData.cs where the error occurrs (search for GDI_ERROR) but he didn’t find a proper resolution either.
// Line 138 in FontData.cs, this GetFontData returns -1 here when
// running as a web application on a 64bit windows host (regardles of WOW64
// being enabled or not)
int size = NativeMethods.GetFontData(hdc, 0, 0, null, 0);
Now, the problem for me is that I am unable to reproduce this error in any environment when I run the code as an console application. I’ve tried toggling WOW64 on and off for the application pool and I’ve tried running the application pool under my own credentials in case there was any permission related problems but to no avail.
The WPF build of PDFSharp works great btw and it’s quite possible that we’ll just switch to that if we don’t find any solution but I’m really curious as to what could cause this.
Can anyone please help me with further debugging steps? In what ways are the environment different when running in IIS/ASP.NET versus a console application when it comes to PInvokes?
It is very common for developers to rely on GDI+ for retrieving font metrics, however according to MSDN:
In FontData.cs I found the following:
This is why GDI+ build of PDFSharp is not working in services while the WPF build does work as you stated on your question.