Aspose.Pdf.FontNotFoundException: Font Arial was not found on Linux App Service

Hi,
I’m adding html to pdf pages using HTML fragment and setting Carlito as embedded font but still exception occurs font Arial was not found locally it’s working but on Linux App Service it’s not working

FontRepository.Sources.Add(new FolderFontSource(fontFilePath));
Font fontRepo = FontRepository.OpenFont(fontPath);
var font = FontRepository.FindFont(fontRepo.FontName, true);
font.IsEmbedded = true;
var fontSubstitution = new SimpleFontSubstitution(originalFontName: “Arial”, substitutionFontName: font.FontName);
document.FontSubstitution += (oldFont, newFont) =>
{
if (oldFont.FontName == fontSubstitution.OriginalFontName)
{
newFont = FontRepository.FindFont(fontSubstitution.SubstitutionFontName);
}
};
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
document.Pages.Accept(textFragmentAbsorber);
foreach (Page page in document.Pages)
{

foreach (TextFragment fragment in textFragmentAbsorber.TextFragments)
{
    if (fragment.TextState.Font.FontName == "Arial" || fragment.TextState.Font.FontName.Contains("Arial"))
    {
        fragment.TextState.Font = font;
    }
}

}

return font;

I tried using above code to resolve this issue. but it’s not working, could you please assist us to resolve this issue.

@vishalbh

It seems that you are encountering a FontNotFoundException for the Arial font while using Aspose.PDF on a Linux App Service. This issue typically arises when the specified font is not available on the server environment.

To resolve this, you can implement font substitution to ensure that when Arial is not found, it is replaced with an available font, such as Carlito, which is a common substitute for Arial. Here’s a refined approach based on your existing code:

  1. Add the Font Source: Ensure that you are adding the folder containing your embedded font correctly.
  2. Handle Font Substitution: Use the FontRepository.Substitutions to define a substitution for Arial.

Here’s an example of how you can modify your code:

// Add the folder containing the font
FontRepository.Sources.Add(new FolderFontSource(fontFilePath));

// Attempt to find the original font
Font originalFont = FontRepository.FindFont("Arial", true);
if (originalFont == null)
{
    // If Arial is not found, add a substitution
    FontRepository.Substitutions.Add(new SimpleFontSubstitution("Arial", "Carlito"));
}

// Load your document
Document document = new Document("input.pdf");

// Process the document to replace fonts
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
document.Pages.Accept(textFragmentAbsorber);

foreach (Page page in document.Pages)
{
    foreach (TextFragment fragment in textFragmentAbsorber.TextFragments)
    {
        if (fragment.TextState.Font.FontName == "Arial")
        {
            // Replace with the available font
            fragment.TextState.Font = FontRepository.FindFont("Carlito");
        }
    }
}

// Save the modified document
document.Save("output.pdf");

Key Points:

  • Ensure that the font file for Carlito is correctly placed in the specified folder.
  • The FontRepository.Substitutions allows you to define what to use when Arial is not found.
  • Always check if the font is available before attempting to use it.

This approach should help you avoid the FontNotFoundException by substituting Arial with Carlito when it is not available on your Linux App Service. If you continue to face issues, ensure that the font files are correctly deployed and accessible in your application environment.

If you need further assistance, feel free to ask!

I’ve tried using the suggested solution, but I’m still encountering the same error. Could you please assist us further with this?

@vishalbh

Can you please try with Aspose.Pdf.Drawing instead of Aspose.PDF for .NET? Please try uninstalling Aspose.PDF for .NET and install Aspose.Pdf.Drawing as it is published specifically for Linux like platforms.

Also, please try to install msttcorefonts package in your system and let us know if issue still persists.

I’m currently using Aspose.Pdf.Drawing on a Linux-based Azure App Service, but I’m still encountering the same error. Are there any additional configurations or changes required to resolve this issue?

@vishalbh

Have you checked by placing the Arial font in the directory? Was this error still occurring after this step? Can you please share some sample source file(s) with us to investigate the issue so that we can log an investigation ticket and share the ID with you?

We are encountering a critical font rendering issue in our production environment, which is hosted on a Linux App Service in Azure.

Although Arial font is not used anywhere in our codebase, we are consistently receiving errors related to it during PDF generation. This issue began after we migrated from IronPdf to Aspose to support advanced data mapping in both Word and PDF document generation. Previously, the same implementation functioned without any issues using IronPdf.

This is now impacting live users and disrupting document generation workflows. We urgently request your support in performing a root cause analysis and providing an immediate resolution.

For reference, we have attached a ZIP file containing the class implementation where the problem is most likely originating.

Please treat this issue with high priority, and advise if any additional system logs or diagnostics from our Linux App Service are required to assist with your investigation.

Looking forward to your prompt response.
PDFModificationHelper.zip (8.9 KB)

@vishalbh

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-59762

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

We currently have an Aspose license. Could you kindly confirm whether paid support is included with our license, or if it needs to be purchased separately?

@vishalbh

Please post this inquiry in our purchase forum where you can get required information about your account.

I’m following up to check if there are any updates on the tickets opened earlier. Please let us know if anything is needed from our side.

@vishalbh

We are afraid that the earlier logged ticket hasn’t been reviewed yet. It will be investigated and resolved on first come first serve basis. As soon as we make some progress towards its resolution, we will inform you via this forum thread. We will surely ask if we need something from your side. Please be patient and spare us some time.

We are sorry for the inconvenience.