Creating pdf (in docker container) from text works but from HTML fails (missing glyphs)

Hi, I am doing a small test:

I have a Docker container with a .net Core application
In the code-block below you find 2 functions, While debugging I see Succeeding pdf generator uses Helvetica as font.
But when trying to use the Failing method (html to pdf) it fails with a message saying Glyph Helvetica is not found.

What I want:
I want Html to be converted to a PDF using a .net core application hosted in a docker container

At forehand thanx!

Gijsbert

public static class DocumentCreator
{
    public static byte[] Working(string html)
    {
        using MemoryStream memoryStream = new MemoryStream();

        GemBox.Pdf.ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = new PdfDocument())
        {
            // Add a page.
            var page = document.Pages.Add();

            // Write a text.
            using (var formattedText = new PdfFormattedText()
            {
                FontWeight = PdfFontWeight.Bold,
            })
            {
                formattedText.Append("Tick");
                page.Content.DrawText(formattedText, new PdfPoint(100, 700));
            }

            document.Save(memoryStream);
        }

        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();

        return bytes;
    }

    public static byte[] Fails(string html)
    {
        using MemoryStream memoryStream = new MemoryStream();

        GemBox.Document.ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var htmlLoadOptions = new HtmlLoadOptions();
        using (var htmlStream = new MemoryStream(htmlLoadOptions.Encoding.GetBytes(html)))
        {
            var document = DocumentModel.Load(htmlStream, htmlLoadOptions);
            document.DefaultCharacterFormat.FontName = "Helvetica";
            document.Save(memoryStream, new GemBox.Document.PdfSaveOptions());
        }

        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();

        return bytes;
    }
}

Hi Gijsbert,

In short, you need to have font files to generate a PDF, please check our Docker example.

For example, here is a Dockerfile that installs a “ttf-mscorefonts-installer” package:

# Update package sources to include supplemental packages (contrib archive area).
RUN sed -i 's/main/main contrib/g' /etc/apt/sources.list.d/debian.sources

# Downloads the package lists from the repositories.
RUN apt-get update

# Install font configuration.
RUN apt-get install -y fontconfig

# Install Microsoft TrueType core fonts.
RUN apt-get install -y ttf-mscorefonts-installer

Now regarding the GemBox.Pdf, when there are no font files it can fall back to one of 14 standard fonts (the default base fonts). These fonts don’t have to be present on the machine that is executing the code.

GemBox.Document currently doesn’t support this, but it will in the future. Nevertheless, installing some fonts on your Docker container will still be a good idea.

I hope this helps, let me know if you need anything else.

Regards,
Mario

Thanx, this does work