PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 18, 2025 10:25 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Trouble with fonts
PostPosted: Thu Mar 06, 2025 10:26 am 
Offline

Joined: Thu Mar 06, 2025 10:18 am
Posts: 1
Or lack of fonts. Hello, I'm new to PDFSharp, and I'm struggling to make even basic "Hello world" migradoc sample working.
My project runs on a docker container with Debian GNU/Linux, apparently with no fonts installed. I am forbidden to install it.
I tried to start with implementing a FontResolver (since error message says I must), but only info online I can find says "You must implement the two methods of the interface", with no mention about how. Link to the sample on codeplex does not work.
What can I do? I don't care which font, as long as something works

I added arial.ttf to project assets, and made a FontResolver like this:

Code:
public class CustomFontResolver : IFontResolver
{
    public static readonly CustomFontResolver Instance = new CustomFontResolver();

    public byte[] GetFont(string faceName)
    {
        string filePath = Path.Combine(AppContext.BaseDirectory, "src", "Admin", "Common", "Assets", "Arial", "arial.ttf");
        if (faceName == "Arial")
        {
            return File.ReadAllBytes(filePath);
        }
        throw new System.NotImplementedException();
    }

    public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
    {
        if (familyName == "Arial")
        {
            return new FontResolverInfo("Arial");
        }
        return null;
    }
}

Then, I try to generate PDF like this:
Code:
string testString = "Here be PDF";
        GlobalFontSettings.FontResolver = CustomFontResolver.Instance;
        var myFont = new XFont("Arial", 10, XFontStyleEx.Regular);
        var document = new MigraDoc.DocumentObjectModel.Document();
        var section = document.AddSection();
        section.AddParagraph(testString);
        var pdfRenderer = new MigraDoc.Rendering.PdfDocumentRenderer();
        pdfRenderer.Document = document;
        pdfRenderer.RenderDocument();

Running the code, I receive an error:
"System.InvalidOperationException: The font 'Courier New' cannot be resolved for predefined error font. Use another font name or fix your font resolver.

I am stumped, why does it search for Courier New? And what is Error Font..


Top
 Profile  
Reply with quote  
 Post subject: Re: Trouble with fonts
PostPosted: Mon Mar 10, 2025 8:17 am 
Offline
User avatar

Joined: Thu Mar 06, 2025 10:43 am
Posts: 1
Morhem wrote:
What can I do? I don't care which font, as long as something works

If you really don't care about the font, then two lines of code will do the trick:
Code:
if (Capabilities.Build.IsCoreBuild)
    GlobalFontSettings.FontResolver = new FailsafeFontResolver();

Code came from here:
https://docs.pdfsharp.net/PDFsharp/Topi ... t-PDF.html

Later you may find the EZFontResolver helpful:
viewtopic.php?f=8&t=3244
viewtopic.php?f=8&t=3073

More on Font Resolvers:
https://docs.pdfsharp.net/PDFsharp/Topi ... lvers.html

More on error font:
https://docs.pdfsharp.net/MigraDoc/DOM/ ... tings.html


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group