PDFsharp & MigraDoc Forum https://forum.pdfsharp.net/ |
|
Trouble with fonts https://forum.pdfsharp.net/viewtopic.php?f=2&t=4804 |
Page 1 of 1 |
Author: | Morhem [ Thu Mar 06, 2025 10:26 am ] |
Post subject: | Trouble with fonts |
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.. |
Author: | !!! [ Mon Mar 10, 2025 8:17 am ] |
Post subject: | Re: Trouble with fonts |
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 |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |