PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

System.IO.DirectoryNotFoundException when querying GlobalFontSettings.FontResolver

Open Schullebernd opened this issue 5 years ago • 2 comments

I use the PDFSharpCore lib in my Xamarin Forms App for iOS and Android. After countless tries with other libs, this one works. But there is a samll problem with the FontResolver that should be fixed.

I use my own fonts. The fonts are embedded in the shared project as EmbeddedRessource. To load them I implemented my pwn FontResolver.

But when I query for an already existant FontResolver, I get an DirectoryNotFoundException.

System.IO.DirectoryNotFoundException: 'Could not find a part of the path '/Library/Fonts'.'

// Init the own FontResolver if not done before
if (GlobalFontSettings.FontResolver == null)
{
        GlobalFontSettings.FontResolver = new FontResolver();
}

In the if statement above the DirectoryNotFoundException gets thrown. The reason is: In the getter of GlobalFontSettings.FontResolver an implementation of FontResolver is automatically created, if no resolver is already existant. And this one wants to have a Folder '/Library/Fonts'.

I resolved it by removing the if statement and manually checking if I already assigned my own FontResolver.

I'm not sure what's the best solution for this. But in my opinion there should not be created a standard FontResolver in the getter of GlobalFontSettings.Fontresolver.

Edit: On Android (7.0) the DirectoryNotFoundException looks like this:

System.IO.DirectoryNotFoundException: 'Could not find a part of the path '/usr/share/fonts/truetype'.'

On iOS (13.x) the DirectoryNotFoundException looks like this:

System.IO.DirectoryNotFoundException: 'Could not find a part of the path '/Library/Fonts'.'

Schullebernd avatar Feb 20 '20 23:02 Schullebernd

I am seeing the same issue on Xamarin Android simulator. Any idea when this will get fixed? Works fine on UWP.

brandbogard avatar Apr 17 '20 14:04 brandbogard

@brandbogard Because I couldn't wait until this is fixed and a new version will be available on nutget, I forked the library, changed it in a way that you can create your own nuget package and use it in your Xamarin project. You can find it here: https://github.com/Schullebernd/PdfSharpCore It should work very easy. The only thing that has to be mentioned is that you should use external fonts and not those from the system. Just embed a font in the shared project of your xamarin app and load it from there for the PDF generation. I explained all this in the start page of my fork. The fork also support MigraDoc which I now use to generate my very table oriented docs.

Schullebernd avatar Apr 18 '20 16:04 Schullebernd