question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v0.97 : ToAdjustContents() crash on Azure pipeline.

See original GitHub issue

Read and complete the full issue template

Do not randomly delete sections. They are here for a reason.

Do you want to request a feature or report a bug?

  • Bug
  • Feature
  • Question

Did you test against the latest CI build?

  • Yes
  • No

If you answered No, please test with the latest development build first.

Version of ClosedXML

0.97.0

What is the current behavior?

Hi, I have a unit test executing using ToAdjustContents method. This unit test is OK when I run it on local. When deployed to azure pipeline, I have the following error:

Error Message: System.ArgumentException : Unable to find font Calibri or fallback font Microsoft Sans Serif. Install missing fonts or specify a different fallback font through 'LoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine("Fallback font name")'. Stack Trace: at ClosedXML.Graphics.DefaultGraphicEngine.LoadFont(MetricId metricId) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at ClosedXML.Graphics.DefaultGraphicEngine.GetFont(MetricId metricId) at ClosedXML.Graphics.DefaultGraphicEngine.GetFont(IXLFontBase fontBase) at ClosedXML.Graphics.DefaultGraphicEngine.GetTextWidth(String text, IXLFontBase fontBase, Double dpiX) at ClosedXML.Excel.XLColumn.GetWidthInCharacters(String text, IXLFontBase font) at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow, Double minWidth, Double maxWidth) at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow) at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow) at ClosedXML.Excel.XLColumn.AdjustToContents() at ClosedXML.Excel.XLColumns.<>c.<AdjustToContents>b__13_0(XLColumn c) at ClosedXML.Excel.EnumerableExtensions.ForEach[T](IEnumerable1 source, Action1 action) at ClosedXML.Excel.XLColumns.AdjustToContents()

The issue appeared on release 0.97. It works fine on release 0.96.

What is the expected behavior or new feature?

Same behaviour as previous release.

Is this a regression from the previous version?

Yes.

Reproducibility

This is an important section. Read it carefully. Failure to do so will cause a ‘RTFM’ comment. Code to reproduce problem: The code looks like this:

public void Main()
{
`            var workbook = new XLWorkbook();
            IXLWorksheet worksheet = workbook.AddWorksheet(ExcelExportSheetName);

            IXLRow headerRow = worksheet.Row(1);
            row.Cell("A").SetValue("data1");
            row.Cell("B").SetValue("data2");
            ...
            row.Cell("AB").SetValue("dataN");

            worksheet.Columns("Z").AdjustToContents();

            for (int i = 0; i < orders.Count; i++)
            {
                var rowNumber = i + HeaderRowNumber + ExcelIndexOffset;
                IXLRow xlRow = worksheet.Row(rowNumber);
                xlRow.Cell("A").SetValue("some data");
                ...
                xlRow.Cell("AB").SetValue("some data");
            }
            worksheet.Columns("A:Y").AdjustToContents();
            worksheet.Columns("AA:AB").AdjustToContents();
`
}

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jHabjanMXPcommented, Dec 9, 2022

If someone else is checking why this crashes on Linux distros. I did it like this:

/// <summary>
/// If runtime platform is not Windows, we have to set graphics engine fallback font in order
/// for AdjustToContents to work.
/// We're setting preferred font to DejaVu Sans, which is available on most linux distros. If not, we're
/// falling back to first available font.
/// </summary>
private static void UpdateGraphicsEngineFonts()
{
    bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
    if (isWindows) return;

    // https://github.com/ClosedXML/ClosedXML/releases/tag/0.97.0
    const string preferredFont = "DejaVu Sans";
    List<string> fonts = SystemFonts.Collection.Families.Select(f => f.Name).ToList();

    // If the preferred font is not available, use the first available font
    var fontName = fonts.Contains(preferredFont) ? preferredFont : fonts.First();

    // All workbooks created later will use the engine with a fallback font DejaVu Sans
    // (or the first available font)
    LoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine(fontName);
}

And call UpdateGraphicsEngineFonts() BEFORE creating a workbook with using XLWorkbook workbook = new();.

1reaction
etiennejycommented, Nov 30, 2022

Thanks !

It works by adding: LoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine("DejaVu Sans");

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot pipeline runs - Azure DevOps
This topic provides guidance on the common reasons that pipelines fail to trigger, get an agent and start, or complete. For instructions on ......
Read more >
Azure App Service manage v0 task
Start, stop, restart, slot swap, slot delete, install site extensions or enable continuous monitoring for an Azure App Service.
Read more >
Crash after Azure DevOps CI build succeeded
APKs must be signed to run on a device. So after your apk is generated. you need to use Android Signing task to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found