Document creation fails with fontNotFound exception after upgrade from 24.7.0 to 24.11.0+

Hello,

We have a system which uses Aspose.PDF to generate some reports for our users. After updating from 24.5 to 24.7, we experienced issue #PDFNET-58388, in which our report elements getting auto-wrapped would unexpectedly become underlined.

This was apparently fixed in 24.11.0, but, unfortunately, THAT build leaves us with an previously unseen error about Arial font:

Aspose.Pdf.FontNotFoundException: Font Arial was not found

Updating to 24.12.0 does not resolve this issue. In the meanwhile, we’ve rolled back to 24.10, choosing to live with the underlines, rather than be unable to generate reports.

I’ve checked our code, and we are never indicating by code that font Arial should be used. I don’t understand where this error is coming from. My best guess is that 24.11 introduced something else which is internally referencing Arial, so that, when our application is deployed to its alpine linux container, the font is unavailable and throws the above exception.

We would, of course, love to update to 24.12 and beyond, but we will not be able to until this issue has been resolved.

@backermann

It seems that after upgrading to version 24.11.0 of Aspose.PDF, you are encountering a FontNotFoundException for the Arial font, which was not an issue in previous versions. This could be due to internal changes in the library that now reference Arial, which is not available in your Alpine Linux container.

To address this issue, you can implement a font substitution mechanism in your code. This allows you to specify an alternative font to use when the desired font is not found. Here’s a code example that demonstrates how to replace missing fonts:

using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

namespace Aspose.Pdf.Examples.CSharp.AsposePDF.DocumentConversion
{
    public class ReplaceMissingFonts
    {
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

            Aspose.Pdf.Text.Font originalFont = null;
            try
            {
                originalFont = FontRepository.FindFont("Arial");
            }
            catch (Exception)
            {
                // Font is missing on destination machine
                FontRepository.Substitutions.Add(new SimpleFontSubstitution("Arial", "Helvetica")); // Substitute with Helvetica or another available font
            }

            var fileNew = new FileInfo(dataDir + "newfile_out.pdf");
            var pdf = new Document(dataDir + "input.pdf");
            pdf.Convert(dataDir + "log.xml", PdfFormat.PDF_A_1B, ConvertErrorAction.Delete);
            pdf.Save(fileNew.FullName);
        }
    }
}

In this example, if the Arial font is not found, it substitutes it with Helvetica. You can replace “Helvetica” with any other font that is available in your environment.

This approach should help you continue using the newer versions of Aspose.PDF while avoiding the font-related exceptions. If you still face issues, consider checking the documentation or community forums for any updates or similar cases.

If you need further assistance, feel free to ask!

Sources:
[1]: ReplaceMissingFonts.cs

Thanks, but … is this really the case? Is there really an internal dependency on the Arial font now? And, internally, while requiring this font now, you’re not internally performing the same fontSubstitution recommended?

We certainly CAN either make the font available on Linux, or perform the substitution, but (based on the instantaneous nature of the above reply), I don’t necessarily trust the AI generated responses accuracy in this matter.

Can you confirm?

@backermann

Aspose.PDF relies on MS Fonts primarily while processing PDF documents. Dependency on Arial Font specifically was never introduced in the API and font substitution is still intact like in the previous versions. We always advise and recommend installing Windows fonts (msttcorefonts package) in Linux like systems because API finds suitable font in the system for substitution.

The error you are mentioning suggests that there can be a particular file for which fonts can be substituted with Arial font and API is not able to find that in the system. We request you please try and install msttcorefonts package and see if issue still persists. Also, please try using Aspose.Pdf.Drawing instead of Aspose.PDF for .NET.

Thanks. We are using Aspose.Pdf.Drawing already.

What I’m not clear on is where this breaking change occurred. Our code is functional (on linux) with 24.7.0, but fails (crashing out with the mentioned exception) on 24.11.0. I understand that I can take steps to cover for this issue, but why has it become an issue in the first place?

Thanks!

@backermann

We will surely investigate the issue in details. Can you please share your sample PDF and docker file as well as sample code snippet? We will log an investigation ticket and share the ID with you.