Autofit - row not fit in linux hosted environment

Hi Aspose Support,

I got an issue when develop an export feature using aspose on .net core platform. The issue is that some of the rows data exported as PDF is being cut-off.

I tried on my local environment (windows) there are no issue. The result is as good as I expected.

But when I publish the code into host environment (linux) the result seems off.

My local result (windows)
local.png (24.5 KB)

My hosted result (linux)
hosted.png (21.3 KB)

Here is piece of code that I am using to generate the file

public FileData ExportToFile(DataTable data, string format, string query)
{
    var workbook = new Workbook();
    var style = workbook.CreateStyle();
    workbook.DefaultStyle = style;

    var sheet = workbook.Worksheets[0];

    AutoFitterOptions oAutoFitterOptions = new AutoFitterOptions { AutoFitMergedCells = true, OnlyAuto = true };

    data = _RemoveFormat(data); // Parse all data to String
    
    /**/
    
    sheet.PageSetup.Orientation = PageOrientationType.Landscape;
    
    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
    pdfSaveOptions.AllColumnsInOnePagePerSheet = true;
    sheet.AutoFitRows(oAutoFitterOptions);
    sheet.AutoFitColumns(oAutoFitterOptions);
    
    workbook.Save(stream, pdfSaveOptions);
    
    /**/
}

@cuppyzh,

It seems your issue is related to missing fonts (in hosted environment) on your end. Please note, Aspose.Cells requires font files (i.e., .ttf files that should be installed on the machine) for the features like, Excel to PDF, Sheet to image and auto-fit rows/cols operations, etc. Please make sure the underlying fonts (used in the workbook) should be installed on the linux machine first and then try your scenario/case with Aspose.Cells.

Moreover, you may copy the underlying fonts used in the workbook in some folder and then specify your fonts folder (containing the required fonts (.ttf files) in it) at the start of the program (in code), this is compulsory, see the document for your reference:
Configuring Fonts for Rendering Spreadsheets|Documentation [.NET]
Configuring Fonts for Rendering Spreadsheets|Documentation [Java]

Moreover, after installing the required fonts and setting the required fonts folder in code, your issue should be fixed. Moreover, please also make sure the APIs should be able to access your fonts folder seamlessly, there should not be any rights issue.

Let us know if you still have any issue or confusion.

Hi @Amjad_Sahi,

Thanks for the reply. I tried the suggestion but the issue still occurs. This is what I tried.

I copy font Arial.ttf into the project folder and try to load it in Startup.cs

Startup.cs

        string path = Directory.GetCurrentDirectory();
        string fontFolder = path + "/Fonts";
        string fontFile = fontFolder + "/arial.ttf";

        FontConfigs.SetFontFolder(fontFolder, true);
        FolderFontSource sourceFolder = new FolderFontSource(fontFolder, true); 
        FileFontSource sourceFile = new FileFontSource(fontFile);
        MemoryFontSource sourceMemory = new MemoryFontSource(System.IO.File.ReadAllBytes(fontFile));
        FontConfigs.SetFontSources(new FontSourceBase[] { sourceFolder, sourceFile, sourceMemory });

DataExporter.cs

var workbook = new Workbook();
var style = workbook.CreateStyle();
style.Font.Name = "Arial"; // New line
workbook.DefaultStyle = style;

var sheet = workbook.Worksheets[0];

AutoFitterOptions oAutoFitterOptions = new AutoFitterOptions { AutoFitMergedCells = true, OnlyAuto = true };

data = _RemoveFormat(data); // Parse all data to String

/**/

sheet.PageSetup.Orientation = PageOrientationType.Landscape;

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.DefaultFont = "Arial"; //new line
pdfSaveOptions.CheckWorkbookDefaultFont = false; //new line
pdfSaveOptions.AllColumnsInOnePagePerSheet = true;
sheet.AutoFitRows(oAutoFitterOptions);
sheet.AutoFitColumns(oAutoFitterOptions);

workbook.Save(stream, pdfSaveOptions);

foreach (var font in fonts) // New line
{
	_logger.LogDebug("Font Used:" + font);
}

From the log above I got this info

Font Used:Aspose.Cells.Font [ Arial; 10; Regular; Color [A=255, R=0, G=0, B=0] ]
Font Used:Aspose.Cells.Font [ Arial; 10; Bold; Color [A=255, R=255, G=255, B=255] ]
Font Used:Aspose.Cells.Font [ Arial; 10; Bold; Color [A=255, R=0, G=0, B=0] ]

I also verified there are no runtime error so I guess the font is load properly.

May I have suggestion maybe I miss something on the steps that was provided?

Thanks in advance

@cuppyzh,

Please add a line to save to XLSX file format before your line of code:
i.e.,

workbook.Save(stream, pdfSaveOptions);

Now zip both XLSX and output PDF file and attach here, we will check it soon.

Hi @Amjad_Sahi,

It’s the requested file Hosted output file.zip (20.9 KB)

@cuppyzh,

Thanks for the files.

We found in your shared PDF file, the “Arial” font is not included at all. Please try the following code before Workbook instance initialization for confirmation (just use java IO APIs to get the details):
e.g
Sample code:

string path = Directory.GetCurrentDirectory();
string fontFolder = path + "/Fonts";

//check font file is exist or not
if (Directory.Exists(fontFolder))
{
string[] files = Directory.GetFiles(fontFolder);
foreach (string file in files)
{
//log file full name
Console.WriteLine(file);
//try to read file and log data length
byte[] fileData = File.ReadAllBytes(file);
Console.WriteLine(fileData.Length);
}
}
else
{
//add your exception here.
Exception...
}

Moreover, there is no need to set font source again and again. The below line will be enough one time (but first make sure Arial font is there in the fonts folder (using the above code segment)):
FontConfigs.SetFontFolder(fontFolder, true);// it is just to make sure that the "Arial" font file is available in the code.

Hi @Amjad_Sahi,
I’ve managed resolve the issue. I was recheck the the system and found out that the I install the font (using this as reference) in the wrong place. My project are using kubernetes to host the app, and I dont realize that I should install the font in the pod level

Adding this 2 line in docker file resolve my issue

RUN echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections
RUN apt-get update \ && apt-get install -y --allow-unauthenticated \ fontconfig \ ttf-mscorefonts-installer \

Thanks for the assistance, have a nice day!

@cuppyzh,

Good to know that you are able to resolve your issue and you are up and running again. Feel free to contact us any time if you need further help or have some other issue or queries.