Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when running/initializing QuestPDF on docker container

I have a .net core application, which uses QuestPDF for generating PDFs, inside a linux docker container. If the container starts, QuestPDF does a dependency check, which fails with following error:

Unhandled exception. System.TypeInitializationException: The type initializer for 'QuestPDF.Settings' threw an exception.
 ---> QuestPDF.Drawing.Exceptions.InitializationException: The QuestPDF library has encountered an issue while loading one of its dependencies. This type of error often occurs when the current runtime is missing necessary NuGet packages.

Please ensure the following NuGet packages are added to your project and has matching versions:
- SkiaSharp.NativeAssets.Linux.NoDependencies
- HarfBuzzSharp.NativeAssets.Linux

For a detailed understanding, please examine the inner exception and consult the official SkiaSharp library documentation.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'asset')
   at SkiaSharp.HarfBuzz.BlobExtensions.ToHarfBuzzBlob(SKStreamAsset asset)
   at SkiaSharp.HarfBuzz.SKShaper..ctor(SKTypeface typeface)
   at QuestPDF.Helpers.NativeDependencyCompatibilityChecker.CheckIfExceptionIsThrownWhenLoadingNativeDependencies()
   --- End of inner exception stack trace ---
   at QuestPDF.Helpers.NativeDependencyCompatibilityChecker.Test()
   at QuestPDF.Settings..cctor()
   --- End of inner exception stack trace ---
   at QuestPDF.Settings.set_License(Nullable`1 value)

The relevant part of the dockerfile looks like

FROM mcr.microsoft.com/dotnet/aspnet:6.0-bullseye-slim

RUN apt update \
    && apt install -y musl-dev \
    && ln -s /usr/lib/x86_64-linux-musl/libc.so /lib/libc.musl-x86_64.so.1

Does anybody have s solution to solve this problem?

I've already searched for a solution, but found nothing.

like image 562
Björn Sturzrehm Avatar asked Sep 13 '25 03:09

Björn Sturzrehm


1 Answers

I faced the same problem. As the error message describe, for linux distribution some components are missing in the runtime, so you must include additional packages (see Support for custom environments (cloud / linux) on QuestPDF docs from old source code on github):

  • SkiaSharp.NativeAssets.Linux.NoDependencies
  • HarfBuzzSharp.NativeAssets.Linux

Additional tip: I also received errors about font.
If that happen to you you must to include fonts in your assembly as embedded resources and register them at the startup of the program. In my case Calibri fonts was missing so I copied files from Windows folder in my project and provided the following code to be called in the startup code:

public static class PDFHelpers {
    public static void EnsureFonts() {
        if (_fontsAlreadyRegistered) return;
        var assembly = typeof(PDFHelpers).Assembly;
        var @namespace = typeof(PDFHelpers).Namespace;
        var fonts = new (string Filename, string FontName)[] {
            ( "calibri.ttf", "Calibri" ),
            ( "calibrib.ttf", "Calibri Bold" ),
            ( "calibrii.ttf", "Calibri Italic" ),
            ( "calibril.ttf", "Calibri Light" ),
            ( "calibrili.ttf", "Calibri Light Italic" ),
            ( "calibriz.ttf", "Calibri Bold Italic" )
        };
        foreach (var font in fonts) { 
            var resource = $"{@namespace}.{font.Filename}";
            using var fontStream = assembly.GetManifestResourceStream(resource);
            if (fontStream != null) {
                FontManager.RegisterFontWithCustomName(font.FontName, fontStream);
            }
        }
        _fontsAlreadyRegistered = true;
    }
}
like image 165
Roberto Ferraris Avatar answered Sep 14 '25 17:09

Roberto Ferraris