Cannot initialize FontManager because no font sources have been set.

0
wlei posted this 20 June 2019

            //Docentric document generation pdf
            using (Stream templateStream = new MemoryStream(bytes))
            {
                using (MemoryStream documentStream = new MemoryStream())
                {
                    DocumentGenerator dgPdf = new DocumentGenerator(xmlDoc);

                    //Byte for pdf
                    dgPdf.GenerateDocument(templateStream, documentStream, SaveOptions.Pdf);
                    byte[] bytesForPdf = documentStream.ToArray();

                    templateBase64String = Convert.ToBase64String(bytesForPdf);
                }
            }

System.Exception: 'Cannot initialize FontManager because no font sources have been set and hence no fonts are available. Use the 'FontManager.SetFontsSources' method to specify font sources.'

This occurs at the dgPdf.GenerateDocument() method.

1 Posts
1 Points
1 Comments
Order By: Standard | Newest | Votes
0
jles posted this 04 August 2019

Hi,

Our .NET Standard library behaves a bit differently than our .NET Framework library because the former one tends to be cross-platform. This means that our FontManager can’t automatically load system fonts simply because it doesn’t know how to and from where. Yes, on Windows system fonts are stored on c:\Windows\Fonts but this clearly isn’t the case with Linux or other systems.

So the solution is to always manually set the source (folders, files, …) for fonts used by the font manager. If you plan to run the application only on Windows, then you can just use the FontFolderFontSource and specify the system font path: Drawing.FontManager.SetFontsSources(new Drawing.FolderFontSource("c:\Windows\Fonts")); If not, you can provide fonts along with your application and provide that special application fonts folder (or multiple ones). Don’t forget that you must set font sources only once, before any processing takes place.

Best regards, Jure

154 Posts
294 Points

Our 225992 members have posted 342 times in 101 discussions