首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MigraDoc PDF渲染器遇到System.NullReferenceException

MigraDoc PDF渲染器遇到System.NullReferenceException
EN

Stack Overflow用户
提问于 2019-09-18 13:30:47
回答 1查看 2K关注 0票数 1

我在Azure上使用MigraDoc和PDF渲染器,它正在弹出一个System.NullReferenceException。这是一个例外:

代码语言:javascript
运行
复制
System.NullReferenceException: Object reference not set to an instance of an object.
at PdfSharp.Fonts.OpenType.OpenTypeFontface.CetOrCreateFrom(XFontSource fontSource)
at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(String familyName, FontResolvingOptions fontResolvingOptions)
at PdfSharp.Drawing.XFont.Initialize()
at MigraDoc.Rendering.FontHandler.FontToXFont(Font font, PdfFontEncoding encoding)
at MigraDoc.Rendering.ParagraphRenderer.get_CurrentFont()
at MigraDoc.Rendering.ParagraphRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at EmailQueue.AddCopyToEFolder.<Run>d__3.MoveNext()

这是我在发布时使用的代码。document属于MigraDoc.DocumentObjectModel.Document类,并已填入文本。

代码语言:javascript
运行
复制
PdfDocumentRenderer pdf = new PdfDocumentRenderer(true);
MemoryStream stream = new MemoryStream();
pdf.Document = document;
pdf.RenderDocument(); //<-- This is where the exception occurs
pdf.Save(stream, false);

编辑:我已经实现了一个MyFontResolver类,如下所示,我使用GlobalFontSettings.FontResolver = new ();在代码中将字体解析器设置为类

代码语言:javascript
运行
复制
public class MyFontResolver : IFontResolver
    {
        public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
        {
            // Ignore case of font names.
            var name = familyName.ToLower();

            // Deal with the fonts we know.
            switch (name)
            {
                case "ubuntu":
                    if (isBold)
                    {
                        if (isItalic)
                            return new FontResolverInfo("Ubuntu#bi");
                        return new FontResolverInfo("Ubuntu#b");
                    }
                    if (isItalic)
                        return new FontResolverInfo("Ubuntu#i");
                    return new FontResolverInfo("Ubuntu#");

                case "janitor":
                    return new FontResolverInfo("Janitor#");
            }

            // We pass all other font requests to the default handler.
            // When running on a web server without sufficient permission, you can return a default font at this stage.
            return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);
        }

        private byte[] LoadFontData(string name)
        {
            var assembly = Assembly.GetExecutingAssembly();

            // Test code to find the names of embedded fonts - put a watch on "ourResources"
            //var ourResources = assembly.GetManifestResourceNames();

            using (Stream stream = assembly.GetManifestResourceStream(name))
            {
                if (stream == null)
                    throw new ArgumentException("No resource with name " + name);

                int count = (int)stream.Length;
                byte[] data = new byte[count];
                stream.Read(data, 0, count);
                return data;
            }
        }

        public byte[] GetFont(string faceName)
        {
            switch (faceName)
            {
                case "Arial#":
                    return LoadFontData("MyProject.fonts.arial.arial.ttf"); ;

                case "Arial#b":
                    return LoadFontData("MyProject.fonts.arial.arialbd.ttf"); ;

                case "Arial#i":
                    return LoadFontData("MyProject.fonts.arial.ariali.ttf");

                case "Arial#bi":
                    return LoadFontData("MyProject.fonts.arial.arialbi.ttf");
            }

            return null;
        }
    }

我现在得到一个独立的错误,我认为我的字体解析器应该修复。

代码语言:javascript
运行
复制
System.InvalidOperationException: Microsoft Azure returns STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L) from GetFontData. This is a bug in Azure. You must implement a FontResolver to circumvent this issue.
at PdfSharp.Drawing.XFontSource.ReadFontBytesFromGdi(Font gdiFont)
at PdfSharp.Fonts.PlatformFontResolver.CreateFontSource(String familyName, FontResolvingOptions fontResolvingOptions, Font& font, String typefaceKey)
at PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
at PdfSharp.Fonts.PlatformFontResolver.ResolveTypeface(String familyName, Boolean isBold, Boolean isItalic)
at EmailQueue.AddCopyToEFolder.MyFontResolver.ResolveTypeface(String familyName, Boolean isBold, Boolean isItalic)
at PdfSharp.Fonts.FontFactory.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(String familyName, FontResolvingOptions fontResolvingOptions)
at PdfSharp.Drawing.XFont.Initialize()
at MigraDoc.Rendering.FontHandler.FontToXFont(Font font, PdfFontEncoding encoding)
at MigraDoc.Rendering.ParagraphRenderer.get_CurrentFont()
at MigraDoc.Rendering.ParagraphRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at EmailQueue.AddCopyToEFolder.<Run>d__3.MoveNext()

谢谢你能提供的任何帮助

EN

回答 1

Stack Overflow用户

发布于 2019-09-18 13:51:29

看起来,您必须对文档中使用的字体使用IFontResolver接口。

很可能您的应用程序没有访问安装在Azure服务器上的TTF文件的权限,或者没有安装所需的TTF。

另请参阅:

https://forum.pdfsharp.net/viewtopic.php?f=8&t=3244

https://forum.pdfsharp.net/viewtopic.php?f=8&t=3073

编辑:回答编辑的问题:您的字体解析器将所有未处理的请求传递给PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);,这显然不适用于Azure。因此,将此调用替换为异常,并确保代码在本地计算机上运行而不引发该异常--然后它也应该在Azure上运行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57993964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档