gpt4 book ai didi

c# - MigraDoc PDF 渲染器遇到 System.NullReferenceException

转载 作者:行者123 更新时间:2023-12-03 02:47:36 32 4
gpt4 key购买 nike

我在 Azure 上使用 MigraDoc 和 PDF 渲染器,但它抛出了 System.NullReferenceException。这是异常(exception):

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 类,并且已填充文本。

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 MyFontResolver();在我的代码中将字体解析器设置为类

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;
}
}

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

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()

感谢您提供的任何帮助

最佳答案

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

很可能您的应用没有访问 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 上运行。

关于c# - MigraDoc PDF 渲染器遇到 System.NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993964/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com