ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.05k stars 234 forks source link

libfontconfig.so.1.so: cannot open shared object file #370

Closed martinRocks closed 1 year ago

martinRocks commented 1 year ago

I'm playing around with a asp.net 7.0.8 web api running on a linux docker container. Using the example code works great when I debug it on my development box, but when I run it in the container I get the below stack trace. It seems to me that I'm missing fonts. How do I install them if that is the case? What am I missing?

Here is my code in the web api `[HttpGet] public IActionResult GetPdfSharp() { GlobalFontSettings.FontResolver = new FontResolver();

    var document = new PdfDocument();
    var page = document.AddPage();

    var gfx = XGraphics.FromPdfPage(page);
    var font = new XFont("Arial", 20, XFontStyle.Bold);
    var layout = new XRect(20, 20, page.Width, page.Height);

    gfx.DrawString("Hello World!", font, XBrushes.Black, layout, XStringFormats.Center);
    byte[]? fileContents = null;
    using (var stream = new MemoryStream())
    {
        document.Save(stream, true);
        fileContents = stream.ToArray();
    }
    return File(fileContents, "application/pdf");
}`

Here is the stack trace. System.DllNotFoundException: Unable to load shared library 'libfontconfig.so.1' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: /usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.8/libfontconfig.so.1.so: cannot open shared object file: No such file or directory /app/libfontconfig.so.1.so: cannot open shared object file: No such file or directory /usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.8/liblibfontconfig.so.1.so: cannot open shared object file: No such file or directory /app/liblibfontconfig.so.1.so: cannot open shared object file: No such file or directory /usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.8/libfontconfig.so.1: cannot open shared object file: No such file or directory /app/libfontconfig.so.1: cannot open shared object file: No such file or directory /usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.8/liblibfontconfig.so.1: cannot open shared object file: No such file or directory /app/liblibfontconfig.so.1: cannot open shared object file: No such file or directory

at PdfSharpCore.Utils.LinuxSystemFontResolver.FcInitLoadConfigAndFonts() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() at System.Lazy1.get_Value() at PdfSharpCore.Utils.LinuxSystemFontResolver.ResolveFontConfig()+MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator1.ToArray() at PdfSharpCore.Utils.LinuxSystemFontResolver.Resolve() Could not find a part of the path '/etc/fonts/fonts.conf'. at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError) at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode, Func4 createOpenException) at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, UnixFileMode openPermissions, Int64& fileLength, UnixFileMode& filePermissions, Func4 createOpenException) at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable1 unixCreateMode) at System.IO.File.OpenRead(String path) at PdfSharpCore.Utils.LinuxSystemFontResolver.SearchPaths()

martinRocks commented 1 year ago

I forgot to say, my docker image is the standard mcr.microsoft.com/dotnet/aspnet:7.0 image.

martinRocks commented 1 year ago

I found the fix. From https://stackoverflow.com/questions/60934639/install-fonts-in-linux-container-for-asp-net-core, copy these lines to the docker file. RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig