Skip to content

Font Resolver can't resolve error font #351

@GlSimon

Description

@GlSimon

Hi,
I am developing for a project on azure, therefore using PDFSharp Core, no system font resolver is available and I have implemented my own. It's mostly a dummy, returning the same font for all requests.

It is working when I create a document using PDFSharp Core, however when using MigraDoc I get this error

Error Type: InvalidOperationException Error Message: The font 'Courier New' cannot be resolved for predefined error font. Use another font name or fix your font resolver. See https://docs.pdfsharp.net/link/migradoc-font-resolving-6.2.html and https://docs.pdfsharp.net/link/font-resolving.html for further information. Stack Trace: at MigraDoc.Rendering.DocumentRenderer.PredefinedFontsAndChars.CreateFont(String familyName, Double emSize, XFontStyleEx style, String propertyDescription) at MigraDoc.Rendering.DocumentRenderer.PredefinedFontsAndChars.get_ErrorFont() at MigraDoc.Rendering.DocumentRenderer.PredefinedFontsAndChars.CreateAllFixedFonts() at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely) at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages() at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument() at line of code

There seems to be an issue connected only to the error font. I am creating the font resolver before creating the document

    public MemoryStream createMigraDocPDF(String input)
    {
        // Register font
        GlobalFontSettings.FontResolver = new EmbeddedFontResolver("myproject.Fonts.Roboto-Regular.ttf");

        //MigraDoc.PredefinedFontsAndChars.ErrorFontName = "Roboto";
        //diffference when using this vs not is just the name of the font that could not be resolved in the error message

        // Create document
        var document = new MigraDoc.DocumentObjectModel.Document();
        document.Info.Title = "Test Export";

        var section = document.AddSection();

        var style = document.Styles[StyleNames.Normal];
        style.Font.Name = "Roboto";
        style.Font.Size = 10;

        var paragraph = section.AddParagraph();
        paragraph = section.AddParagraph("A sample document that demonstrates the\ncapabilities of ");
        // Render document

        var renderer = new PdfDocumentRenderer(true)
        {
            Document = document
        };
        renderer.RenderDocument();

        var stream = new MemoryStream();
        renderer.PdfDocument.Save(stream, false);
        stream.Position = 0;

        return stream;
    }

My Custom Font resolver is as follows

public class EmbeddedFontResolver : IFontResolver
{
    
    private readonly string fontResourceName;

    public EmbeddedFontResolver(string resourceName)
    {
        fontResourceName = resourceName;
    }

    public string DefaultFontName
    {
        get { return "Roboto"; }
    }

    public byte[] GetFont(string faceName)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();

        using Stream stream = assembly.GetManifestResourceStream("myproject.Fonts.Roboto-Regular.ttf");

        if (stream == null)
            throw new Exception("Embedded font not found: " + fontResourceName + "Avaiable resources: " + string.Join(", ", assembly.GetManifestResourceNames()));

        using MemoryStream ms = new MemoryStream();
        stream.CopyTo(ms);

        return ms.ToArray();
    }

    public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
    {
        return new FontResolverInfo("Roboto-Regular", true, true);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions