Saving to PDF with private fonts on Linux

Hello,

I try to use private fonts on Linux. On Windows this code works, but not on Linux.

FontSettings.FontsBaseDirectory = "./Fonts";

var doc = DocumentModel.Load(templateStream);
Console.Write($"Saving {filename}...");

try
{
    doc.Save($"Output_{Guid.NewGuid()}.pdf", new PdfSaveOptions());
    Console.WriteLine("Success");
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex}");
}

image

I get this exception:

System doesn’t contain any font files, use private or embedded fonts.\nSee: Private Fonts | GemBox.Document Example

What am I doing wrong?

Regards,
Robert

Additionally, I’m running in Kubernetes. My base image (ASP.NET Core 3.1) probably does not have any fonts installed, but one of the fonts used is available.

Hi Robert,

After much investigation, it was concluded that even though the input Word document used only “Univers” font in its text content (which was provided with private font files), the problem occurred because the document’s default font was “Calibri”.

This default was coming from the used “Office” theme:

office-theme-in-word

One way to resolve this is to change the default font, like the following:

var doc = DocumentModel.Load(templateStream);
doc.DefaultCharacterFormat.FontName = "Univers";
// ...

Or add “Calibri” font files as private fonts as well because as you suspected, those base image (aspnet:3.1-buster, aspnet:3.1-alpine, etc.) don’t have any fonts installed.

Or you can install ttf-mscorefonts-installer package on those images using “Dockerfile”.

Regards,
Mario

Hi, I’ve been experiencing the same. In my case, I created a docker based lambda and installed mscorefonts.
The initial issue was that documents using Calibri were defaulting to webdings (curiously the last font in alphabetical order).
To (partially) mitigate the issue, I installed google fonts. After that the webdings were converted to wings (again, the last font in the list).
I’ve tried several things with the doc.DefaultCharacterFormat.FontName = “Arial” (either specifying where the fonts are located in the image or without specifying it), and nothing seems to work. My Calibri text is still defaulting to wings. Is there a chance that in linux based systems the doc.DefaultCharacterFormat.FontName is not being taken into account or for some reason not being able to find the font specified?
As a workaround, I could install specifically the Calibri ttf but then the problem would happen with whatever other font I don’t have installed.

Hi Augusto,

Can you send us a sample project that reproduces this issue?
It is odd that “Webdings” is being used as a fallback font, we’ll need to investigate this.

Regards,
Mario

Hi, I’ve been experiencing the same. In my case it is excel file so i can’t use this

doc.DefaultCharacterFormat.FontName = "Univers";

can you please help me?

You can set the font name on the “Normal” style, like this:

ExcelFile workbook = ...
workbook.Styles.Normal.Font.Name = "Univers";

Make sure that you have the “Univers” font file(s) on your Linux.

Regards,
Mario