Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding fonts in server core 2019ltsc container image

We have an app (GrapeCity ActiveReports) that generates pdf reports running inside a container built on the following image: microsoft/dotnet-framework:4.7.2-sdk

This image is based on the 2019ltsc release, which is where the issue comes in. The pdf generating app is (attempting) to use the Arial font for these reports, but it is not being found in the Windows font directory.

Per this Microsoft blog (under the Container Improvements section), the 2019ltsc release stripped out all fonts except Arial, and it apparently prevents installing additional fonts. https://blogs.windows.com/windowsexperience/2018/05/29/announcing-windows-server-2019-insider-preview-build-17677/

Launching the un-modified SDK image interactively and browsing to C:\Windows\Fonts only shows the lucon.ttf font present and nothing else. We have also attempted the install fonts method outlined in this doc from Microsoft with no change. The font itself installs fine but the generating program fails to use it. https://learn.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-windows-containers-custom-fonts

When run it returns the following exception:

System.InvalidOperationException: Cannot read the file for font: Arial

UPDATE: Recieved a reply from MS support, looks like there is probably no resolution at this time.

After a few days research, I haven’t got much progress about why the only font in mcr.microsoft.com.windows/servercore:ltsc2019 based image is lucon.ttf, and seems there’s no published method to add additional fonts to windows server core 2019 image. Based on the situation, I have sent emails to windows server 2019 product team to consult this issue. However, please understand, due to permission limit, I couldn’t guarantee I can get feedback from Product Team. I will keep researching and monitor on the product team, if I get any progress, I will get back to you as soon as possible.

like image 596
stevenmiller Avatar asked Jan 25 '19 13:01

stevenmiller


People also ask

Is it possible to add additional fonts to Windows Server Core 2019?

After a few days research, I haven’t got much progress about why the only font in mcr.microsoft.com.windows/servercore:ltsc2019 based image is lucon.ttf, and seems there’s no published method to add additional fonts to windows server core 2019 image.

What is the default font used in Server Core containers?

Use the full windows base image. For Server Core containers, only the default font, Arial, is supported; no other fonts are supported, and no others can be installed. Thanks for contributing an answer to Server Fault!

What font is being used for 2019ltsc reports?

This image is based on the 2019ltsc release, which is where the issue comes in. The pdf generating app is (attempting) to use the Arial font for these reports, but it is not being found in the Windows font directory.

How do I add fonts to a Windows container?

Most importantly register the font & fontLInk carefully. Copy required fonts either from your local machine (C:\Windows\Fonts) or from mcr.microsoft.com/windows container image (you can use volume binding while running the container to copy fonts) Register the copied fonts. Register the FontLink for the copied fonts.


3 Answers

I was able to get it to work in ltsc2019 by installing it like suggested before:

COPY calibri.ttf c:/windows/fonts/calibri.TTF
RUN powershell.exe -NoProfile -Command New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' -Name 'Calibri (TrueType)' -PropertyType String -Value calibri.ttf

and then calling

[DllImport("gdi32.dll")] static extern int AddFontResource(string lpFilename);

before using the font. Calling AddFontResource while building the image didn't help.

As we start our application from powershell we call the LoadFonts.ps1 from C:\Users\ContainerAdministrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

RUN echo "C:\tools\LoadFonts.ps1" | Out-File -FilePath $profile

LoadFonts.ps1:

$fontCSharpCode = @'
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace FontResource
{
    public class AddRemoveFonts
    {
        [DllImport("gdi32.dll")]
        static extern int AddFontResource(string lpFilename);
        public static int AddFont(string fontFilePath) {
            try 
            {
                return AddFontResource(fontFilePath);
            }
            catch
            {
                return 0;
            }
        }
    }
}
'@

Add-Type $fontCSharpCode

foreach($font in $(gci C:\Windows\Fonts))
{
    if(!$font.FullName.EndsWith("lucon.ttf"))
    {
        Write-Output "Loading $($font.FullName)"
        [FontResource.AddRemoveFonts]::AddFont($font.FullName) | Out-Null
    }
}
like image 166
Daniel Romero Avatar answered Oct 12 '22 18:10

Daniel Romero


I was having same issue with "mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019". Fonts got installed but the application failed to use them.

I installed the fonts by copying them to "c:\windows\fonts" folder and then calling "AddFontResource" and then also adding a registry entry to "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts". But no luck.

But then I ran the below command to see if the fonts were written to the registry:

Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"

and noticed that the Arial font was missing. So I ran the below command through the shell:

Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" -name "Arial (TrueType)" -value "arial.ttf" -type STRING

And then re ran the below command to confirm the font Arial was not listed:

Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"

After that when I ran my application it was able to load the Arial font.

like image 32
code2real Avatar answered Oct 12 '22 17:10

code2real


I got same problem when hosted ASP.Net app which rendered some images and docs and I managed to solve with 2 steps:

I used the following in Dockerfile

FROM mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019

...

# Take font from host machine, exclude lucon.ttf, as it exists in image already
COPY Fonts/* /Windows/Fonts/

# Take FontReg.exe FROM http://code.kliu.org/misc/fontreg/, extract proper version
COPY FontReg.exe /    

# Described below
COPY run.bat /

ENTRYPOINT ["cmd.exe", "/C run.bat"]

And here is the custom entry point run.bat file with the following content:

C:\FontReg.exe
C:\ServiceMonitor.exe w3svc
like image 38
Tony Avatar answered Oct 12 '22 18:10

Tony