7

I'm trying to install a font in a docker windows container based on 4.8-windowsservercore-ltsc2019 image. I found some solutions but none of them worked for me.

I created a github project that reproduces my problem.

https://github.com/AndySchmitt/docker-font-windows-ltsc2019

This is a simple C# Api project that uses wkhtmltopdf to create a PDF file. When you run without docker, it generates a pdf file with Arial font. When you run within docker, it generates the same pdf, but with another font.

In this windows container, Microsoft deleted all fonts from the fonts folder, and I'm having problem installing new fonts to this image.

My question is: How can I generate the pdf using Arial font inside docker container? Or, how can I install Arial font in this windows image and make wkhtmltopdf use this recently intalled font?

Andy Schmitt
  • 441
  • 1
  • 6
  • 23
  • Microsoft did explain how to do that on their website here: [Migrate custom software to Azure App Service using a custom container](https://learn.microsoft.com/en-us/azure/app-service/tutorial-custom-container?pivots=container-windows#configure-windows-container), you only have to change Azure container for Docker container. Basically it is just copying some files from Windows to your container (in te correct location). – Luuk Feb 16 '22 at 19:28
  • @AndySchmitt you can check this [post](https://stackoverflow.com/questions/59779075/how-to-add-fonts-to-windows-docker-container-image) – Chandan Feb 17 '22 at 03:55
  • @Luuk This solution didnt worked. Running powershell to install fonts simply not works. I tryed many ways of make this work and I get the same results aways: 1) The command dont run in dockerfile, but runs inside the container. 2) The command run in dockerfile but nothing happens in the image. – Andy Schmitt Feb 21 '22 at 21:11
  • @Chandan This post is not complete. In this post the fonts are copied to image, but they are not installed. Without installation, I cant generate PDF with them. – Andy Schmitt Feb 21 '22 at 21:12
  • I pushed another version of my project to Github. I included the font files, FontInstall.ps1 as suggested in the first comment and updated Dockerfile to run this script. Feel free to try and reproduce the problem! – Andy Schmitt Feb 21 '22 at 21:14
  • 1
    Does the script itself do its job properly? Did you execute it within the container from an interactive shell session? – mu88 Feb 22 '22 at 07:38
  • @mu88 yes. If I run powershell inside the container, everything works. If I run throw Dockerfile, nothing happens. – Andy Schmitt Feb 22 '22 at 14:44
  • Can you try to call the script via `Process.Start()` in `Application_Start()` (`Global.asax.cs`)? – mu88 Feb 22 '22 at 15:13
  • @mu88 This idea can work. I'll try. – Andy Schmitt Feb 22 '22 at 19:25
  • it has been a while, @AndySchmitt, but I wonder if you found a solution. I'm basically having the same issue with `mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019` - installing a font (registry key via *.reg or ps's New-ItemProperty/Set-ItemProperty) using RUN command does not work but the same approach in it works. – aderesh Aug 15 '23 at 03:58
  • @aderesh I solved this problem creating a "Fonts" folder inside my project containing all fonts of interest and copying it to container throw Docker. => COPY /bin/Fonts/ c:/windows/fonts/ Then, when I need to use it, I convert the font to Base64 and reference it inside CSS. Its not a cool solution, but it was the only one that worked for me. – Andy Schmitt Aug 25 '23 at 11:14

1 Answers1

0

In your Dockerfile is this:

...
COPY /bin/Resources/FontInstall.ps1 c:/windows/fonts/
ARG CACHEBUSTER=7
WORKDIR /Windows/Fonts

RUN ["powershell.exe", "C:\\Inetpub\\wwwroot\\Fonts\\FontInstall.ps1"]

When you copy your script to c:/windows/fonts/, should it not also been run from this directory (last line)?

I think it needs to be changed to:

RUN ["powershell.exe", "C:\\Windows\\Fonts\\FontInstall.ps1"]
Luuk
  • 12,245
  • 5
  • 22
  • 33
  • I tryed many things. The powershell executes during docker build, but wkhtmltopdf fail to use the fonts. If, after start container, I execute the ps1 script, everything starts working. But nothing happens when I run script throw Dockerfile. – Andy Schmitt Feb 22 '22 at 14:47
  • The reason for no copies of the fonts is the `foreach ($objFile in $objFolder.items())` in your `FontInstall.ps1`. Maybe you should look for another way of registering the fonts, like import a .reg file ? – Luuk Feb 23 '22 at 16:47